joi 8.4.2 → 9.0.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.github/CONTRIBUTING.md +14 -0
- package/.github/ISSUE_TEMPLATE.md +19 -0
- package/.npmignore +21 -0
- package/.travis.yml +8 -0
- package/API.md +1690 -0
- package/README.md +1 -1
- package/examples/conditionalRequire.js +52 -0
- package/examples/customMessage.js +31 -0
- package/examples/multipleWhen.js +26 -0
- package/examples/timestamps.js +22 -0
- package/generate-readme-toc.js +49 -0
- package/images/joi.png +0 -0
- package/images/validation.png +0 -0
- package/lib/any.js +74 -35
- package/lib/array.js +11 -44
- package/lib/boolean.js +9 -2
- package/lib/date.js +0 -3
- package/lib/errors.js +21 -22
- package/lib/index.js +142 -3
- package/lib/language.js +2 -6
- package/lib/object.js +36 -33
- package/lib/string.js +0 -17
- package/package.json +5 -5
- package/test/alternatives.js +490 -0
- package/test/any.js +1751 -0
- package/test/array.js +1049 -0
- package/test/binary.js +190 -0
- package/test/boolean.js +163 -0
- package/test/date.js +441 -0
- package/test/errors.js +512 -0
- package/test/function.js +224 -0
- package/test/helper.js +65 -0
- package/test/index.js +2450 -0
- package/test/number.js +836 -0
- package/test/object.js +1318 -0
- package/test/ref.js +470 -0
- package/test/string.js +3206 -0
- package/lib/lazy.js +0 -51
- package/lib/schemas.js +0 -25
package/test/binary.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Load modules
|
|
4
|
+
|
|
5
|
+
const Lab = require('lab');
|
|
6
|
+
const Code = require('code');
|
|
7
|
+
const Joi = require('../lib');
|
|
8
|
+
const Helper = require('./helper');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
// Declare internals
|
|
12
|
+
|
|
13
|
+
const internals = {};
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// Test shortcuts
|
|
17
|
+
|
|
18
|
+
const lab = exports.lab = Lab.script();
|
|
19
|
+
const describe = lab.describe;
|
|
20
|
+
const it = lab.it;
|
|
21
|
+
const expect = Code.expect;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
describe('binary', () => {
|
|
25
|
+
|
|
26
|
+
it('converts a string to a buffer', (done) => {
|
|
27
|
+
|
|
28
|
+
Joi.binary().validate('test', (err, value) => {
|
|
29
|
+
|
|
30
|
+
expect(err).to.not.exist();
|
|
31
|
+
expect(value instanceof Buffer).to.equal(true);
|
|
32
|
+
expect(value.length).to.equal(4);
|
|
33
|
+
expect(value.toString('utf8')).to.equal('test');
|
|
34
|
+
done();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('validates allowed buffer content', (done) => {
|
|
39
|
+
|
|
40
|
+
const hello = new Buffer('hello');
|
|
41
|
+
const schema = Joi.binary().valid(hello);
|
|
42
|
+
|
|
43
|
+
Helper.validate(schema, [
|
|
44
|
+
['hello', true],
|
|
45
|
+
[hello, true],
|
|
46
|
+
[new Buffer('hello'), true],
|
|
47
|
+
['goodbye', false],
|
|
48
|
+
[new Buffer('goodbye'), false],
|
|
49
|
+
[new Buffer('HELLO'), false]
|
|
50
|
+
], done);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('validate()', () => {
|
|
54
|
+
|
|
55
|
+
it('returns an error when a non-buffer or non-string is used', (done) => {
|
|
56
|
+
|
|
57
|
+
Joi.binary().validate(5, (err, value) => {
|
|
58
|
+
|
|
59
|
+
expect(err).to.exist();
|
|
60
|
+
expect(err.message).to.equal('"value" must be a buffer or a string');
|
|
61
|
+
done();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('accepts a buffer object', (done) => {
|
|
66
|
+
|
|
67
|
+
Joi.binary().validate(new Buffer('hello world'), (err, value) => {
|
|
68
|
+
|
|
69
|
+
expect(err).to.not.exist();
|
|
70
|
+
expect(value.toString('utf8')).to.equal('hello world');
|
|
71
|
+
done();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('encoding()', () => {
|
|
77
|
+
|
|
78
|
+
it('applies encoding', (done) => {
|
|
79
|
+
|
|
80
|
+
const schema = Joi.binary().encoding('base64');
|
|
81
|
+
const input = new Buffer('abcdef');
|
|
82
|
+
schema.validate(input.toString('base64'), (err, value) => {
|
|
83
|
+
|
|
84
|
+
expect(err).to.not.exist();
|
|
85
|
+
expect(value instanceof Buffer).to.equal(true);
|
|
86
|
+
expect(value.toString()).to.equal('abcdef');
|
|
87
|
+
done();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('throws when encoding is invalid', (done) => {
|
|
92
|
+
|
|
93
|
+
expect(() => {
|
|
94
|
+
|
|
95
|
+
Joi.binary().encoding('base6');
|
|
96
|
+
}).to.throw('Invalid encoding: base6');
|
|
97
|
+
done();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('min()', () => {
|
|
102
|
+
|
|
103
|
+
it('validates buffer size', (done) => {
|
|
104
|
+
|
|
105
|
+
const schema = Joi.binary().min(5);
|
|
106
|
+
Helper.validate(schema, [
|
|
107
|
+
[new Buffer('testing'), true],
|
|
108
|
+
[new Buffer('test'), false]
|
|
109
|
+
], done);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('throws when min is not a number', (done) => {
|
|
113
|
+
|
|
114
|
+
expect(() => {
|
|
115
|
+
|
|
116
|
+
Joi.binary().min('a');
|
|
117
|
+
}).to.throw('limit must be a positive integer');
|
|
118
|
+
done();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('throws when min is not an integer', (done) => {
|
|
122
|
+
|
|
123
|
+
expect(() => {
|
|
124
|
+
|
|
125
|
+
Joi.binary().min(1.2);
|
|
126
|
+
}).to.throw('limit must be a positive integer');
|
|
127
|
+
done();
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('max()', () => {
|
|
132
|
+
|
|
133
|
+
it('validates buffer size', (done) => {
|
|
134
|
+
|
|
135
|
+
const schema = Joi.binary().max(5);
|
|
136
|
+
Helper.validate(schema, [
|
|
137
|
+
[new Buffer('testing'), false],
|
|
138
|
+
[new Buffer('test'), true]
|
|
139
|
+
], done);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('throws when max is not a number', (done) => {
|
|
143
|
+
|
|
144
|
+
expect(() => {
|
|
145
|
+
|
|
146
|
+
Joi.binary().max('a');
|
|
147
|
+
}).to.throw('limit must be a positive integer');
|
|
148
|
+
done();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('throws when max is not an integer', (done) => {
|
|
152
|
+
|
|
153
|
+
expect(() => {
|
|
154
|
+
|
|
155
|
+
Joi.binary().max(1.2);
|
|
156
|
+
}).to.throw('limit must be a positive integer');
|
|
157
|
+
done();
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('length()', () => {
|
|
162
|
+
|
|
163
|
+
it('validates buffer size', (done) => {
|
|
164
|
+
|
|
165
|
+
const schema = Joi.binary().length(4);
|
|
166
|
+
Helper.validate(schema, [
|
|
167
|
+
[new Buffer('test'), true],
|
|
168
|
+
[new Buffer('testing'), false]
|
|
169
|
+
], done);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('throws when length is not a number', (done) => {
|
|
173
|
+
|
|
174
|
+
expect(() => {
|
|
175
|
+
|
|
176
|
+
Joi.binary().length('a');
|
|
177
|
+
}).to.throw('limit must be a positive integer');
|
|
178
|
+
done();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('throws when length is not an integer', (done) => {
|
|
182
|
+
|
|
183
|
+
expect(() => {
|
|
184
|
+
|
|
185
|
+
Joi.binary().length(1.2);
|
|
186
|
+
}).to.throw('limit must be a positive integer');
|
|
187
|
+
done();
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
package/test/boolean.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Load modules
|
|
4
|
+
|
|
5
|
+
const Lab = require('lab');
|
|
6
|
+
const Code = require('code');
|
|
7
|
+
const Joi = require('../lib');
|
|
8
|
+
const Helper = require('./helper');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
// Declare internals
|
|
12
|
+
|
|
13
|
+
const internals = {};
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// Test shortcuts
|
|
17
|
+
|
|
18
|
+
const lab = exports.lab = Lab.script();
|
|
19
|
+
const describe = lab.describe;
|
|
20
|
+
const it = lab.it;
|
|
21
|
+
const expect = Code.expect;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
describe('boolean', () => {
|
|
25
|
+
|
|
26
|
+
it('converts a string to a boolean', (done) => {
|
|
27
|
+
|
|
28
|
+
Joi.boolean().validate('true', (err, value) => {
|
|
29
|
+
|
|
30
|
+
expect(err).to.not.exist();
|
|
31
|
+
expect(value).to.equal(true);
|
|
32
|
+
done();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('errors on a number not 0 or 1', (done) => {
|
|
37
|
+
|
|
38
|
+
Joi.boolean().validate(2, (err, value) => {
|
|
39
|
+
|
|
40
|
+
expect(err).to.exist();
|
|
41
|
+
expect(value).to.equal(2);
|
|
42
|
+
done();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('validate()', () => {
|
|
47
|
+
|
|
48
|
+
it('converts string values and validates', (done) => {
|
|
49
|
+
|
|
50
|
+
const rule = Joi.boolean();
|
|
51
|
+
Helper.validate(rule, [
|
|
52
|
+
['1234', false],
|
|
53
|
+
[false, true],
|
|
54
|
+
[true, true],
|
|
55
|
+
[null, false],
|
|
56
|
+
['on', true],
|
|
57
|
+
['off', true],
|
|
58
|
+
['true', true],
|
|
59
|
+
['false', true],
|
|
60
|
+
['yes', true],
|
|
61
|
+
['no', true],
|
|
62
|
+
['1', true],
|
|
63
|
+
['0', true]
|
|
64
|
+
], done);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('converts number values and validates', (done) => {
|
|
68
|
+
|
|
69
|
+
const rule = Joi.boolean();
|
|
70
|
+
Helper.validate(rule, [
|
|
71
|
+
[1234, false],
|
|
72
|
+
[1, true],
|
|
73
|
+
[0, true]
|
|
74
|
+
], done);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('converts 1 to true', (done) => {
|
|
78
|
+
|
|
79
|
+
Joi.boolean().validate(1, (err, value) => {
|
|
80
|
+
|
|
81
|
+
expect(err).to.not.exist();
|
|
82
|
+
expect(value).to.equal(true);
|
|
83
|
+
done();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('converts 0 to false', (done) => {
|
|
88
|
+
|
|
89
|
+
Joi.boolean().validate(0, (err, value) => {
|
|
90
|
+
|
|
91
|
+
expect(err).to.not.exist();
|
|
92
|
+
expect(value).to.equal(false);
|
|
93
|
+
done();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should handle work with required', (done) => {
|
|
98
|
+
|
|
99
|
+
const rule = Joi.boolean().required();
|
|
100
|
+
Helper.validate(rule, [
|
|
101
|
+
['1234', false],
|
|
102
|
+
['true', true],
|
|
103
|
+
[false, true],
|
|
104
|
+
[true, true],
|
|
105
|
+
[null, false]
|
|
106
|
+
], done);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should handle work with allow', (done) => {
|
|
110
|
+
|
|
111
|
+
const rule = Joi.boolean().allow(false);
|
|
112
|
+
Helper.validate(rule, [
|
|
113
|
+
['1234', false],
|
|
114
|
+
[false, true],
|
|
115
|
+
[null, false]
|
|
116
|
+
], done);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should handle work with invalid', (done) => {
|
|
120
|
+
|
|
121
|
+
const rule = Joi.boolean().invalid(false);
|
|
122
|
+
Helper.validate(rule, [
|
|
123
|
+
['1234', false],
|
|
124
|
+
[false, false],
|
|
125
|
+
[true, true],
|
|
126
|
+
[null, false]
|
|
127
|
+
], done);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should handle work with invalid and null allowed', (done) => {
|
|
131
|
+
|
|
132
|
+
const rule = Joi.boolean().invalid(false).allow(null);
|
|
133
|
+
Helper.validate(rule, [
|
|
134
|
+
['1234', false],
|
|
135
|
+
[false, false],
|
|
136
|
+
[true, true],
|
|
137
|
+
[null, true]
|
|
138
|
+
], done);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should handle work with allow and invalid', (done) => {
|
|
142
|
+
|
|
143
|
+
const rule = Joi.boolean().invalid(true).allow(false);
|
|
144
|
+
Helper.validate(rule, [
|
|
145
|
+
['1234', false],
|
|
146
|
+
[false, true],
|
|
147
|
+
[true, false],
|
|
148
|
+
[null, false]
|
|
149
|
+
], done);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should handle work with allow, invalid, and null allowed', (done) => {
|
|
153
|
+
|
|
154
|
+
const rule = Joi.boolean().invalid(true).allow(false).allow(null);
|
|
155
|
+
Helper.validate(rule, [
|
|
156
|
+
['1234', false],
|
|
157
|
+
[false, true],
|
|
158
|
+
[true, false],
|
|
159
|
+
[null, true]
|
|
160
|
+
], done);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
});
|