joi 4.6.2 → 4.9.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/.c9/.nakignore +17 -0
- package/.c9/metadata/tab0 +1 -0
- package/.c9/metadata/workspace/.c9/project.settings +1 -0
- package/.c9/metadata/workspace/.travis.yml +1 -0
- package/.c9/metadata/workspace/Makefile +1 -0
- package/.c9/metadata/workspace/README.md +1 -0
- package/.c9/metadata/workspace/lib/any.js +1 -0
- package/.c9/metadata/workspace/lib/array.js +1 -0
- package/.c9/metadata/workspace/lib/binary.js +1 -0
- package/.c9/metadata/workspace/lib/boolean.js +1 -0
- package/.c9/metadata/workspace/lib/cast.js +1 -0
- package/.c9/metadata/workspace/lib/date.js +1 -0
- package/.c9/metadata/workspace/lib/errors.js +1 -0
- package/.c9/metadata/workspace/lib/function.js +1 -0
- package/.c9/metadata/workspace/lib/index.js +1 -0
- package/.c9/metadata/workspace/lib/language.js +1 -0
- package/.c9/metadata/workspace/lib/number.js +1 -0
- package/.c9/metadata/workspace/lib/object.js +1 -0
- package/.c9/metadata/workspace/lib/string.js +1 -0
- package/.c9/metadata/workspace/package.json +1 -0
- package/.c9/metadata/workspace/test/alternatives.js +1 -0
- package/.c9/metadata/workspace/test/any.js +1 -0
- package/.c9/metadata/workspace/test/array.js +1 -0
- package/.c9/metadata/workspace/test/binary.js +1 -0
- package/.c9/metadata/workspace/test/boolean.js +1 -0
- package/.c9/metadata/workspace/test/date.js +1 -0
- package/.c9/metadata/workspace/test/errors.js +1 -0
- package/.c9/metadata/workspace/test/function.js +1 -0
- package/.c9/metadata/workspace/test/helper.js +1 -0
- package/.c9/metadata/workspace/test/index.js +1 -0
- package/.c9/metadata/workspace/test/number.js +1 -0
- package/.c9/metadata/workspace/test/object.js +1 -0
- package/.c9/metadata/workspace/test/ref.js +1 -0
- package/.c9/metadata/workspace/test/string.js +1 -0
- package/.c9/project.settings +34 -0
- package/.travis.yml +1 -0
- package/Makefile +3 -3
- package/README.md +162 -31
- package/examples/conditionalRequire.js +43 -0
- package/examples/customMessage.js +22 -0
- package/examples/multipleWhen.js +17 -0
- package/lib/any.js +35 -24
- package/lib/array.js +25 -0
- package/lib/date.js +64 -9
- package/lib/errors.js +18 -2
- package/lib/index.js +3 -2
- package/lib/language.js +17 -6
- package/lib/number.js +50 -0
- package/lib/object.js +69 -5
- package/lib/string.js +27 -6
- package/package.json +5 -3
- package/test/alternatives.js +12 -10
- package/test/any.js +64 -15
- package/test/array.js +55 -16
- package/test/binary.js +13 -11
- package/test/boolean.js +9 -7
- package/test/date.js +143 -20
- package/test/errors.js +26 -15
- package/test/function.js +7 -5
- package/test/helper.js +7 -5
- package/test/index.js +116 -73
- package/test/number.js +125 -12
- package/test/object.js +192 -43
- package/test/ref.js +22 -20
- package/test/string.js +255 -106
- package/AUTHORS +0 -3
package/test/any.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Load modules
|
|
2
2
|
|
|
3
3
|
var Lab = require('lab');
|
|
4
|
+
var Code = require('code');
|
|
4
5
|
var Joi = require('../lib');
|
|
5
6
|
var Helper = require('./helper');
|
|
6
7
|
|
|
@@ -12,11 +13,12 @@ var internals = {};
|
|
|
12
13
|
|
|
13
14
|
// Test shortcuts
|
|
14
15
|
|
|
15
|
-
var
|
|
16
|
-
var before =
|
|
17
|
-
var after =
|
|
18
|
-
var describe =
|
|
19
|
-
var it =
|
|
16
|
+
var lab = exports.lab = Lab.script();
|
|
17
|
+
var before = lab.before;
|
|
18
|
+
var after = lab.after;
|
|
19
|
+
var describe = lab.describe;
|
|
20
|
+
var it = lab.it;
|
|
21
|
+
var expect = Code.expect;
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
describe('any', function () {
|
|
@@ -79,13 +81,37 @@ describe('any', function () {
|
|
|
79
81
|
var input = { b: '2' };
|
|
80
82
|
schema.validate(input, function (err, value) {
|
|
81
83
|
|
|
82
|
-
expect(err).to.not.exist;
|
|
84
|
+
expect(err).to.not.exist();
|
|
83
85
|
expect(value.b).to.equal(2);
|
|
84
86
|
done();
|
|
85
87
|
});
|
|
86
88
|
});
|
|
87
89
|
});
|
|
88
90
|
|
|
91
|
+
describe('#label', function () {
|
|
92
|
+
|
|
93
|
+
it('adds to existing options', function (done) {
|
|
94
|
+
|
|
95
|
+
var schema = Joi.object({ b: Joi.string().email().label('Custom label') });
|
|
96
|
+
var input = { b: 'not_a_valid_email' };
|
|
97
|
+
schema.validate(input, function (err, value) {
|
|
98
|
+
|
|
99
|
+
expect(err).to.exist();
|
|
100
|
+
expect(err.details[0].message).to.equal('Custom label must be a valid email');
|
|
101
|
+
done();
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('throws when label is missing', function (done) {
|
|
106
|
+
|
|
107
|
+
expect(function () {
|
|
108
|
+
|
|
109
|
+
Joi.label();
|
|
110
|
+
}).to.throw('Label name must be a non-empty string');
|
|
111
|
+
done();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
89
115
|
describe('#strict', function () {
|
|
90
116
|
|
|
91
117
|
it('adds to existing options', function (done) {
|
|
@@ -94,7 +120,7 @@ describe('any', function () {
|
|
|
94
120
|
var input = { b: '2' };
|
|
95
121
|
schema.validate(input, function (err, value) {
|
|
96
122
|
|
|
97
|
-
expect(err).to.exist;
|
|
123
|
+
expect(err).to.exist();
|
|
98
124
|
expect(value.b).to.equal('2');
|
|
99
125
|
done();
|
|
100
126
|
});
|
|
@@ -110,7 +136,7 @@ describe('any', function () {
|
|
|
110
136
|
|
|
111
137
|
schema.validate(input, function (err, value) {
|
|
112
138
|
|
|
113
|
-
expect(err).to.not.exist;
|
|
139
|
+
expect(err).to.not.exist();
|
|
114
140
|
expect(value.foo).to.equal('test');
|
|
115
141
|
done();
|
|
116
142
|
});
|
|
@@ -123,13 +149,13 @@ describe('any', function () {
|
|
|
123
149
|
|
|
124
150
|
schema.validate(input, function (err, value) {
|
|
125
151
|
|
|
126
|
-
expect(err).to.not.exist;
|
|
152
|
+
expect(err).to.not.exist();
|
|
127
153
|
expect(value.foo).to.equal('test');
|
|
128
154
|
done();
|
|
129
155
|
});
|
|
130
156
|
});
|
|
131
157
|
|
|
132
|
-
it('sets value based on condition (
|
|
158
|
+
it('sets value based on condition (outer)', function (done) {
|
|
133
159
|
|
|
134
160
|
var schema = Joi.object({
|
|
135
161
|
a: Joi.boolean(),
|
|
@@ -138,7 +164,7 @@ describe('any', function () {
|
|
|
138
164
|
|
|
139
165
|
schema.validate({ a: false }, function (err, value) {
|
|
140
166
|
|
|
141
|
-
expect(err).to.not.exist;
|
|
167
|
+
expect(err).to.not.exist();
|
|
142
168
|
expect(value.b).to.equal(false);
|
|
143
169
|
done();
|
|
144
170
|
});
|
|
@@ -153,13 +179,36 @@ describe('any', function () {
|
|
|
153
179
|
|
|
154
180
|
schema.validate({ a: true }, function (err, value) {
|
|
155
181
|
|
|
156
|
-
expect(err).to.not.exist;
|
|
182
|
+
expect(err).to.not.exist();
|
|
157
183
|
expect(value.b).to.equal(false);
|
|
158
184
|
done();
|
|
159
185
|
});
|
|
160
186
|
});
|
|
161
187
|
});
|
|
162
188
|
|
|
189
|
+
describe('#optional', function () {
|
|
190
|
+
|
|
191
|
+
it('validates optional with default required', function (done) {
|
|
192
|
+
|
|
193
|
+
var schema = Joi.object({
|
|
194
|
+
a: Joi.any(),
|
|
195
|
+
b: Joi.any(),
|
|
196
|
+
c: {
|
|
197
|
+
d: Joi.any()
|
|
198
|
+
}
|
|
199
|
+
}).options({ presence: 'required' });
|
|
200
|
+
|
|
201
|
+
Helper.validate(schema, [
|
|
202
|
+
[{ a: 5 }, false],
|
|
203
|
+
[{ a: 5, b: 6 }, false],
|
|
204
|
+
[{ a: 5, b: 6, c: {} }, false],
|
|
205
|
+
[{ a: 5, b: 6, c: { d: 7 } }, true],
|
|
206
|
+
[{}, false],
|
|
207
|
+
[{ b: 5 }, false]
|
|
208
|
+
], done);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
163
212
|
describe('#forbidden', function () {
|
|
164
213
|
|
|
165
214
|
it('validates forbidden', function (done) {
|
|
@@ -339,7 +388,7 @@ describe('any', function () {
|
|
|
339
388
|
var schema = Joi.number().invalid(2);
|
|
340
389
|
Joi.validate('2', schema, { abortEarly: false }, function (err, value) {
|
|
341
390
|
|
|
342
|
-
expect(err).to.exist;
|
|
391
|
+
expect(err).to.exist();
|
|
343
392
|
done();
|
|
344
393
|
});
|
|
345
394
|
});
|
|
@@ -569,7 +618,7 @@ describe('any', function () {
|
|
|
569
618
|
|
|
570
619
|
a.concat(b).validate({ c: 1, d: 2 }, function (err, value) {
|
|
571
620
|
|
|
572
|
-
expect(err).to.not.exist;
|
|
621
|
+
expect(err).to.not.exist();
|
|
573
622
|
expect(value).to.deep.equal({ a: 1, b: 2 });
|
|
574
623
|
done();
|
|
575
624
|
});
|
|
@@ -582,7 +631,7 @@ describe('any', function () {
|
|
|
582
631
|
|
|
583
632
|
a.concat(b).validate({ a: 1, b: 2 }, function (err, value) {
|
|
584
633
|
|
|
585
|
-
expect(err).to.not.exist;
|
|
634
|
+
expect(err).to.not.exist();
|
|
586
635
|
done();
|
|
587
636
|
});
|
|
588
637
|
});
|
package/test/array.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Load modules
|
|
2
2
|
|
|
3
3
|
var Lab = require('lab');
|
|
4
|
+
var Code = require('code');
|
|
4
5
|
var Joi = require('../lib');
|
|
5
6
|
var Helper = require('./helper');
|
|
6
7
|
|
|
@@ -12,11 +13,12 @@ var internals = {};
|
|
|
12
13
|
|
|
13
14
|
// Test shortcuts
|
|
14
15
|
|
|
15
|
-
var
|
|
16
|
-
var before =
|
|
17
|
-
var after =
|
|
18
|
-
var describe =
|
|
19
|
-
var it =
|
|
16
|
+
var lab = exports.lab = Lab.script();
|
|
17
|
+
var before = lab.before;
|
|
18
|
+
var after = lab.after;
|
|
19
|
+
var describe = lab.describe;
|
|
20
|
+
var it = lab.it;
|
|
21
|
+
var expect = Code.expect;
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
describe('array', function () {
|
|
@@ -25,7 +27,7 @@ describe('array', function () {
|
|
|
25
27
|
|
|
26
28
|
Joi.array().validate('[1,2,3]', function (err, value) {
|
|
27
29
|
|
|
28
|
-
expect(err).to.not.exist;
|
|
30
|
+
expect(err).to.not.exist();
|
|
29
31
|
expect(value.length).to.equal(3);
|
|
30
32
|
done();
|
|
31
33
|
});
|
|
@@ -35,7 +37,7 @@ describe('array', function () {
|
|
|
35
37
|
|
|
36
38
|
Joi.array().validate('{ "something": false }', function (err, value) {
|
|
37
39
|
|
|
38
|
-
expect(err).to.exist;
|
|
40
|
+
expect(err).to.exist();
|
|
39
41
|
expect(err.message).to.equal('value must be an array');
|
|
40
42
|
done();
|
|
41
43
|
});
|
|
@@ -45,7 +47,7 @@ describe('array', function () {
|
|
|
45
47
|
|
|
46
48
|
Joi.array().validate(3, function (err, value) {
|
|
47
49
|
|
|
48
|
-
expect(err).to.exist;
|
|
50
|
+
expect(err).to.exist();
|
|
49
51
|
expect(value).to.equal(3);
|
|
50
52
|
done();
|
|
51
53
|
});
|
|
@@ -55,7 +57,7 @@ describe('array', function () {
|
|
|
55
57
|
|
|
56
58
|
Joi.array().validate('3', function (err, value) {
|
|
57
59
|
|
|
58
|
-
expect(err).to.exist;
|
|
60
|
+
expect(err).to.exist();
|
|
59
61
|
expect(value).to.equal('3');
|
|
60
62
|
done();
|
|
61
63
|
});
|
|
@@ -65,7 +67,7 @@ describe('array', function () {
|
|
|
65
67
|
|
|
66
68
|
Joi.array().validate('asdf', function (err, value) {
|
|
67
69
|
|
|
68
|
-
expect(err).to.exist;
|
|
70
|
+
expect(err).to.exist();
|
|
69
71
|
expect(value).to.equal('asdf');
|
|
70
72
|
done();
|
|
71
73
|
});
|
|
@@ -79,7 +81,7 @@ describe('array', function () {
|
|
|
79
81
|
var input = ['1', '2', '3'];
|
|
80
82
|
schema.validate(input, function (err, value) {
|
|
81
83
|
|
|
82
|
-
expect(err).to.not.exist;
|
|
84
|
+
expect(err).to.not.exist();
|
|
83
85
|
expect(value).to.deep.equal([1, 2, 3]);
|
|
84
86
|
done();
|
|
85
87
|
});
|
|
@@ -96,7 +98,7 @@ describe('array', function () {
|
|
|
96
98
|
|
|
97
99
|
schema.validate(input, function (err, value) {
|
|
98
100
|
|
|
99
|
-
expect(err).to.not.exist;
|
|
101
|
+
expect(err).to.not.exist();
|
|
100
102
|
done();
|
|
101
103
|
});
|
|
102
104
|
});
|
|
@@ -263,12 +265,12 @@ describe('array', function () {
|
|
|
263
265
|
var n = [1, 2, 'hippo'];
|
|
264
266
|
schema.validate(n, function (err, value) {
|
|
265
267
|
|
|
266
|
-
expect(err).to.exist;
|
|
268
|
+
expect(err).to.exist();
|
|
267
269
|
|
|
268
270
|
var m = ['x', 'y', 'z'];
|
|
269
271
|
schema.validate(m, function (err2, value) {
|
|
270
272
|
|
|
271
|
-
expect(err2).to.not.exist;
|
|
273
|
+
expect(err2).to.not.exist();
|
|
272
274
|
done();
|
|
273
275
|
});
|
|
274
276
|
});
|
|
@@ -320,7 +322,7 @@ describe('array', function () {
|
|
|
320
322
|
var input = { arr: [1, 2, 2.1] };
|
|
321
323
|
schema.validate(input, function (err, value) {
|
|
322
324
|
|
|
323
|
-
expect(err).to.exist;
|
|
325
|
+
expect(err).to.exist();
|
|
324
326
|
expect(err.message).to.equal('arr position 2 fails because 2 must be an integer');
|
|
325
327
|
done();
|
|
326
328
|
});
|
|
@@ -356,7 +358,7 @@ describe('array', function () {
|
|
|
356
358
|
|
|
357
359
|
var schema = Joi.array().includes().max(5);
|
|
358
360
|
var desc = schema.describe();
|
|
359
|
-
expect(desc.includes).to.not.exist;
|
|
361
|
+
expect(desc.includes).to.not.exist();
|
|
360
362
|
done();
|
|
361
363
|
});
|
|
362
364
|
|
|
@@ -376,4 +378,41 @@ describe('array', function () {
|
|
|
376
378
|
});
|
|
377
379
|
});
|
|
378
380
|
});
|
|
381
|
+
|
|
382
|
+
describe('#unique', function() {
|
|
383
|
+
|
|
384
|
+
it('errors if duplicate numbers or string', function(done) {
|
|
385
|
+
var schema = Joi.array().unique();
|
|
386
|
+
|
|
387
|
+
Helper.validate(schema, [
|
|
388
|
+
[[2, 2], false],
|
|
389
|
+
[[02, 2], false],
|
|
390
|
+
[[0x2, 2], false],
|
|
391
|
+
[['duplicate', 'duplicate'], false]
|
|
392
|
+
], done);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('ignores duplicates if they are of different types', function(done) {
|
|
396
|
+
var schema = Joi.array().unique();
|
|
397
|
+
|
|
398
|
+
Helper.validate(schema, [
|
|
399
|
+
[[2, '2'], true]
|
|
400
|
+
], done);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it('ignores duplicates objects, binaries, functions, dates and booleans', function(done) {
|
|
404
|
+
var buffer = new Buffer('hello world');
|
|
405
|
+
var func = function() {};
|
|
406
|
+
var now = new Date();
|
|
407
|
+
var schema = Joi.array().unique();
|
|
408
|
+
|
|
409
|
+
Helper.validate(schema, [
|
|
410
|
+
[[{ a: 'b' }, { a: 'b' }], true],
|
|
411
|
+
[[buffer, buffer], true],
|
|
412
|
+
[[func, func], true],
|
|
413
|
+
[[now, now], true],
|
|
414
|
+
[[true, true], true]
|
|
415
|
+
], done);
|
|
416
|
+
});
|
|
417
|
+
});
|
|
379
418
|
});
|
package/test/binary.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Load modules
|
|
2
2
|
|
|
3
3
|
var Lab = require('lab');
|
|
4
|
+
var Code = require('code');
|
|
4
5
|
var Joi = require('../lib');
|
|
5
6
|
var Helper = require('./helper');
|
|
6
7
|
|
|
@@ -12,11 +13,12 @@ var internals = {};
|
|
|
12
13
|
|
|
13
14
|
// Test shortcuts
|
|
14
15
|
|
|
15
|
-
var
|
|
16
|
-
var before =
|
|
17
|
-
var after =
|
|
18
|
-
var describe =
|
|
19
|
-
var it =
|
|
16
|
+
var lab = exports.lab = Lab.script();
|
|
17
|
+
var before = lab.before;
|
|
18
|
+
var after = lab.after;
|
|
19
|
+
var describe = lab.describe;
|
|
20
|
+
var it = lab.it;
|
|
21
|
+
var expect = Code.expect;
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
describe('binary', function () {
|
|
@@ -25,7 +27,7 @@ describe('binary', function () {
|
|
|
25
27
|
|
|
26
28
|
Joi.binary().validate('test', function (err, value) {
|
|
27
29
|
|
|
28
|
-
expect(err).to.not.exist;
|
|
30
|
+
expect(err).to.not.exist();
|
|
29
31
|
expect(value instanceof Buffer).to.equal(true);
|
|
30
32
|
expect(value.length).to.equal(4);
|
|
31
33
|
expect(value.toString('utf8')).to.equal('test');
|
|
@@ -54,7 +56,7 @@ describe('binary', function () {
|
|
|
54
56
|
|
|
55
57
|
Joi.binary().validate(5, function (err, value) {
|
|
56
58
|
|
|
57
|
-
expect(err).to.exist;
|
|
59
|
+
expect(err).to.exist();
|
|
58
60
|
expect(err.message).to.equal('value must be a buffer or a string');
|
|
59
61
|
done();
|
|
60
62
|
});
|
|
@@ -68,7 +70,7 @@ describe('binary', function () {
|
|
|
68
70
|
|
|
69
71
|
Joi.binary().validate(new Buffer('hello world'), function (err, value) {
|
|
70
72
|
|
|
71
|
-
expect(err).to.not.exist;
|
|
73
|
+
expect(err).to.not.exist();
|
|
72
74
|
expect(value.toString('utf8')).to.equal('hello world');
|
|
73
75
|
done();
|
|
74
76
|
});
|
|
@@ -83,17 +85,17 @@ describe('binary', function () {
|
|
|
83
85
|
var input = new Buffer('abcdef');
|
|
84
86
|
schema.validate(input.toString('base64'), function (err, value) {
|
|
85
87
|
|
|
86
|
-
expect(err).to.not.exist;
|
|
88
|
+
expect(err).to.not.exist();
|
|
87
89
|
expect(value instanceof Buffer).to.equal(true);
|
|
88
90
|
expect(value.toString()).to.equal('abcdef');
|
|
89
91
|
done();
|
|
90
92
|
});
|
|
91
93
|
});
|
|
92
|
-
|
|
94
|
+
|
|
93
95
|
it('throws when encoding is invalid', function (done) {
|
|
94
96
|
|
|
95
97
|
expect(function () {
|
|
96
|
-
|
|
98
|
+
|
|
97
99
|
Joi.binary().encoding('base6');
|
|
98
100
|
}).to.throw('Invalid encoding: base6');
|
|
99
101
|
done();
|
package/test/boolean.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Load modules
|
|
2
2
|
|
|
3
3
|
var Lab = require('lab');
|
|
4
|
+
var Code = require('code');
|
|
4
5
|
var Joi = require('../lib');
|
|
5
6
|
var Helper = require('./helper');
|
|
6
7
|
|
|
@@ -12,11 +13,12 @@ var internals = {};
|
|
|
12
13
|
|
|
13
14
|
// Test shortcuts
|
|
14
15
|
|
|
15
|
-
var
|
|
16
|
-
var before =
|
|
17
|
-
var after =
|
|
18
|
-
var describe =
|
|
19
|
-
var it =
|
|
16
|
+
var lab = exports.lab = Lab.script();
|
|
17
|
+
var before = lab.before;
|
|
18
|
+
var after = lab.after;
|
|
19
|
+
var describe = lab.describe;
|
|
20
|
+
var it = lab.it;
|
|
21
|
+
var expect = Code.expect;
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
describe('boolean', function () {
|
|
@@ -25,7 +27,7 @@ describe('boolean', function () {
|
|
|
25
27
|
|
|
26
28
|
Joi.boolean().validate('true', function (err, value) {
|
|
27
29
|
|
|
28
|
-
expect(err).to.not.exist;
|
|
30
|
+
expect(err).to.not.exist();
|
|
29
31
|
expect(value).to.equal(true);
|
|
30
32
|
done();
|
|
31
33
|
});
|
|
@@ -35,7 +37,7 @@ describe('boolean', function () {
|
|
|
35
37
|
|
|
36
38
|
Joi.boolean().validate(1, function (err, value) {
|
|
37
39
|
|
|
38
|
-
expect(err).to.exist;
|
|
40
|
+
expect(err).to.exist();
|
|
39
41
|
expect(value).to.equal(1);
|
|
40
42
|
done();
|
|
41
43
|
});
|
package/test/date.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Load modules
|
|
2
2
|
|
|
3
3
|
var Lab = require('lab');
|
|
4
|
+
var Code = require('code');
|
|
4
5
|
var Joi = require('../lib');
|
|
5
6
|
var Helper = require('./helper');
|
|
6
7
|
|
|
@@ -12,11 +13,12 @@ var internals = {};
|
|
|
12
13
|
|
|
13
14
|
// Test shortcuts
|
|
14
15
|
|
|
15
|
-
var
|
|
16
|
-
var before =
|
|
17
|
-
var after =
|
|
18
|
-
var describe =
|
|
19
|
-
var it =
|
|
16
|
+
var lab = exports.lab = Lab.script();
|
|
17
|
+
var before = lab.before;
|
|
18
|
+
var after = lab.after;
|
|
19
|
+
var describe = lab.describe;
|
|
20
|
+
var it = lab.it;
|
|
21
|
+
var expect = Code.expect;
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
describe('date', function () {
|
|
@@ -35,16 +37,16 @@ describe('date', function () {
|
|
|
35
37
|
var now = Date.now();
|
|
36
38
|
Joi.date().valid(new Date(now)).validate(new Date(now), function (err, value) {
|
|
37
39
|
|
|
38
|
-
expect(err).to.not.exist;
|
|
40
|
+
expect(err).to.not.exist();
|
|
39
41
|
done();
|
|
40
42
|
});
|
|
41
43
|
});
|
|
42
44
|
|
|
43
45
|
it('errors on invalid input and convert disabled', function (done) {
|
|
44
46
|
|
|
45
|
-
Joi.date().options({ convert: false }).validate('1-1-2013', function (err, value) {
|
|
47
|
+
Joi.date().options({ convert: false }).validate('1-1-2013 UTC', function (err, value) {
|
|
46
48
|
|
|
47
|
-
expect(err).to.exist;
|
|
49
|
+
expect(err).to.exist();
|
|
48
50
|
expect(err.message).to.equal('value must be a number of milliseconds or valid date string');
|
|
49
51
|
done();
|
|
50
52
|
});
|
|
@@ -54,7 +56,7 @@ describe('date', function () {
|
|
|
54
56
|
|
|
55
57
|
Joi.date().validate(new Date(), function (err, value) {
|
|
56
58
|
|
|
57
|
-
expect(err).to.not.exist;
|
|
59
|
+
expect(err).to.not.exist();
|
|
58
60
|
done();
|
|
59
61
|
});
|
|
60
62
|
});
|
|
@@ -66,41 +68,162 @@ describe('date', function () {
|
|
|
66
68
|
|
|
67
69
|
Joi.date().validate(mili.toString(), function (err, value) {
|
|
68
70
|
|
|
69
|
-
expect(err).to.not.exist;
|
|
70
|
-
expect(value).to.
|
|
71
|
+
expect(err).to.not.exist();
|
|
72
|
+
expect(value).to.deep.equal(now);
|
|
71
73
|
done();
|
|
72
74
|
});
|
|
73
75
|
});
|
|
74
76
|
|
|
77
|
+
it('accepts "now" as the min date', function(done) {
|
|
78
|
+
|
|
79
|
+
var future = new Date(Date.now() + 1000000);
|
|
80
|
+
|
|
81
|
+
Joi.date().min('now').validate(future, function (err, value) {
|
|
82
|
+
|
|
83
|
+
expect(err).to.not.exist();
|
|
84
|
+
expect(value).to.deep.equal(future);
|
|
85
|
+
done();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('errors if .min("now") is used with a past date', function(done) {
|
|
90
|
+
|
|
91
|
+
var past = new Date(Date.now() - 1000000);
|
|
92
|
+
|
|
93
|
+
Joi.date().min('now').validate(past, function (err, value) {
|
|
94
|
+
|
|
95
|
+
expect(err).to.exist();
|
|
96
|
+
done();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('accepts "now" as the max date', function(done) {
|
|
101
|
+
|
|
102
|
+
var past = new Date(Date.now() - 1000000);
|
|
103
|
+
|
|
104
|
+
Joi.date().max('now').validate(past, function (err, value) {
|
|
105
|
+
|
|
106
|
+
expect(err).to.not.exist();
|
|
107
|
+
expect(value).to.deep.equal(past);
|
|
108
|
+
done();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('errors if .max("now") is used with a future date', function(done) {
|
|
113
|
+
|
|
114
|
+
var future = new Date(Date.now() + 1000000);
|
|
115
|
+
|
|
116
|
+
Joi.date().max('now').validate(future, function (err, value) {
|
|
117
|
+
|
|
118
|
+
expect(err).to.exist();
|
|
119
|
+
done();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
75
123
|
describe('#validate', function () {
|
|
76
124
|
|
|
77
125
|
it('validates min', function (done) {
|
|
78
126
|
|
|
79
|
-
Helper.validate(Joi.date().min('1-1-
|
|
80
|
-
['1-1-
|
|
81
|
-
['1-1-
|
|
127
|
+
Helper.validate(Joi.date().min('1-1-2000 UTC'), [
|
|
128
|
+
['1-1-2001 UTC', true],
|
|
129
|
+
['1-1-2000 UTC', true],
|
|
82
130
|
[0, false],
|
|
83
|
-
[
|
|
131
|
+
["0", false],
|
|
132
|
+
["-1", false],
|
|
133
|
+
['1-1-1999 UTC', false]
|
|
84
134
|
], done);
|
|
85
135
|
});
|
|
86
136
|
|
|
87
137
|
it('validates max', function (done) {
|
|
88
138
|
|
|
89
|
-
Helper.validate(Joi.date().max('1-1-
|
|
90
|
-
['1-1-
|
|
91
|
-
['1-1-
|
|
139
|
+
Helper.validate(Joi.date().max('1-1-1970 UTC'), [
|
|
140
|
+
['1-1-1971 UTC', false],
|
|
141
|
+
['1-1-1970 UTC', true],
|
|
92
142
|
[0, true],
|
|
93
|
-
[
|
|
143
|
+
[1, false],
|
|
144
|
+
["0", true],
|
|
145
|
+
["-1", true],
|
|
146
|
+
['1-1-2014 UTC', false]
|
|
94
147
|
], done);
|
|
95
148
|
});
|
|
96
149
|
|
|
97
150
|
it('validates only valid dates', function (done) {
|
|
98
151
|
|
|
99
152
|
Helper.validate(Joi.date(), [
|
|
100
|
-
['1-1-2013', true],
|
|
153
|
+
['1-1-2013 UTC', true],
|
|
101
154
|
['not a valid date', false],
|
|
102
155
|
[new Date('not a valid date'), false]
|
|
103
156
|
], done);
|
|
104
157
|
});
|
|
158
|
+
|
|
159
|
+
describe('#iso', function() {
|
|
160
|
+
|
|
161
|
+
it('validates isoDate', function (done) {
|
|
162
|
+
|
|
163
|
+
Helper.validate(Joi.date().iso(), [
|
|
164
|
+
['2013-06-07T14:21:46.295Z', true],
|
|
165
|
+
['2013-06-07T14:21:46.295Z0', false],
|
|
166
|
+
['2013-06-07T14:21:46.295+07:00', true],
|
|
167
|
+
['2013-06-07T14:21:46.295+07:000', false],
|
|
168
|
+
['2013-06-07T14:21:46.295-07:00', true],
|
|
169
|
+
['2013-06-07T14:21:46Z', true],
|
|
170
|
+
['2013-06-07T14:21:46Z0', false],
|
|
171
|
+
['2013-06-07T14:21:46+07:00', true],
|
|
172
|
+
['2013-06-07T14:21:46-07:00', true],
|
|
173
|
+
['2013-06-07T14:21Z', true],
|
|
174
|
+
['2013-06-07T14:21+07:00', true],
|
|
175
|
+
['2013-06-07T14:21+07:000', false],
|
|
176
|
+
['2013-06-07T14:21-07:00', true],
|
|
177
|
+
['2013-06-07T14:21Z+7:00', false],
|
|
178
|
+
['2013-06-07', true],
|
|
179
|
+
['2013-06-07T', false],
|
|
180
|
+
['2013-06-07T14:21', false],
|
|
181
|
+
['1-1-2013', false]
|
|
182
|
+
], done);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('validates isoDate with a friendly error message', function (done) {
|
|
186
|
+
|
|
187
|
+
var schema = { item: Joi.date().iso() };
|
|
188
|
+
Joi.compile(schema).validate({ item: 'something' }, function (err, value) {
|
|
189
|
+
|
|
190
|
+
expect(err.message).to.contain('must be a valid ISO 8601 date');
|
|
191
|
+
done();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('#format', function () {
|
|
197
|
+
|
|
198
|
+
it('validates custom format', function (done) {
|
|
199
|
+
|
|
200
|
+
Helper.validate(Joi.date().format('DD#YYYY$MM'), [
|
|
201
|
+
['07#2013$06', true],
|
|
202
|
+
['2013-06-07', false]
|
|
203
|
+
], done);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('validates several custom formats', function (done) {
|
|
207
|
+
|
|
208
|
+
Helper.validate(Joi.date().format(['DD#YYYY$MM', 'YY|DD|MM']), [
|
|
209
|
+
['13|07|06', true],
|
|
210
|
+
['2013-06-07', false]
|
|
211
|
+
], done);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('fails with bad formats', function (done) {
|
|
215
|
+
|
|
216
|
+
expect(function () {
|
|
217
|
+
|
|
218
|
+
Joi.date().format(true);
|
|
219
|
+
}).to.throw('Invalid format.');
|
|
220
|
+
|
|
221
|
+
expect(function () {
|
|
222
|
+
|
|
223
|
+
Joi.date().format(['YYYYMMDD', true]);
|
|
224
|
+
}).to.throw('Invalid format.');
|
|
225
|
+
done();
|
|
226
|
+
});
|
|
227
|
+
});
|
|
105
228
|
});
|
|
106
229
|
});
|