date.js
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Load modules
var Lab = require('lab');
var Code = require('code');
var Joi = require('../lib');
var Helper = require('./helper');
// Declare internals
var internals = {};
// Test shortcuts
var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;
describe('date', function () {
it('fails on boolean', function (done) {
var schema = Joi.date();
Helper.validate(schema, [
[true, false],
[false, false]
], done);
});
it('matches specific date', function (done) {
var now = Date.now();
Joi.date().valid(new Date(now)).validate(new Date(now), function (err, value) {
expect(err).to.not.exist();
done();
});
});
it('errors on invalid input and convert disabled', function (done) {
Joi.date().options({ convert: false }).validate('1-1-2013 UTC', function (err, value) {
expect(err).to.exist();
expect(err.message).to.equal('"value" must be a number of milliseconds or valid date string');
done();
});
});
it('validates date', function (done) {
Joi.date().validate(new Date(), function (err, value) {
expect(err).to.not.exist();
done();
});
});
it('validates millisecond date as a string', function (done) {
var now = new Date();
var mili = now.getTime();
Joi.date().validate(mili.toString(), function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal(now);
done();
});
});
describe('#validate', function () {
describe('min', function () {
it('validates min', function (done) {
Helper.validate(Joi.date().min('1-1-2000 UTC'), [
['1-1-2001 UTC', true],
['1-1-2000 UTC', true],
[0, false],
['0', false],
['-1', false],
['1-1-1999 UTC', false]
], done);
});
it('accepts "now" as the min date', function (done) {
var future = new Date(Date.now() + 1000000);
Joi.date().min('now').validate(future, function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal(future);
done();
});
});
it('errors if .min("now") is used with a past date', function (done) {
var past = new Date(Date.now() - 1000000);
Joi.date().min('now').validate(past, function (err, value) {
expect(err).to.exist();
done();
});
});
it('accepts references as min date', function (done) {
var schema = Joi.object({ a: Joi.date(), b: Joi.date().min(Joi.ref('a')) });
var now = Date.now();
Helper.validate(schema, [
[{ a: now, b: now }, true],
[{ a: now, b: now + 1e3 }, true],
[{ a: now, b: now - 1e3 }, false]
], done);
});
it('accepts context references as min date', function (done) {
var schema = Joi.object({ b: Joi.date().min(Joi.ref('$a')) });
var now = Date.now();
Helper.validate(schema, [
[{ b: now }, true, { context: { a: now } }],
[{ b: now + 1e3 }, true, { context: { a: now } }],
[{ b: now - 1e3 }, false, { context: { a: now } }]
], done);
});
it('errors if reference is not a date', function (done) {
var schema = Joi.object({ a: Joi.string(), b: Joi.date().min(Joi.ref('a')) });
var now = Date.now();
Helper.validate(schema, [
[{ a: 'abc', b: now }, false, null, 'child "b" fails because ["b" references "a" which is not a date]'],
[{ a: '123', b: now }, true],
[{ a: (now + 1e3).toString(), b: now }, false, null, /^child "b" fails because \["b" must be larger than or equal to/]
], done);
});
it('errors if context reference is not a date', function (done) {
var schema = Joi.object({ b: Joi.date().min(Joi.ref('$a')) });
var now = Date.now();
Helper.validate(schema, [
[{ b: now }, false, { context: { a: 'abc' } }, 'child "b" fails because ["b" references "a" which is not a date]'],
[{ b: now }, false, { context: { a: (now + 1e3).toString() } }, /^child "b" fails because \["b" must be larger than or equal to/]
], done);
});
});
describe('max', function () {
it('validates max', function (done) {
Helper.validate(Joi.date().max('1-1-1970 UTC'), [
['1-1-1971 UTC', false],
['1-1-1970 UTC', true],
[0, true],
[1, false],
['0', true],
['-1', true],
['1-1-2014 UTC', false]
], done);
});
it('accepts "now" as the max date', function (done) {
var past = new Date(Date.now() - 1000000);
Joi.date().max('now').validate(past, function (err, value) {
expect(err).to.not.exist();
expect(value).to.deep.equal(past);
done();
});
});
it('errors if .max("now") is used with a future date', function (done) {
var future = new Date(Date.now() + 1000000);
Joi.date().max('now').validate(future, function (err, value) {
expect(err).to.exist();
done();
});
});
it('accepts references as max date', function (done) {
var schema = Joi.object({ a: Joi.date(), b: Joi.date().max(Joi.ref('a')) });
var now = Date.now();
Helper.validate(schema, [
[{ a: now, b: now }, true],
[{ a: now, b: now + 1e3 }, false],
[{ a: now, b: now - 1e3 }, true]
], done);
});
it('accepts references as max date', function (done) {
var schema = Joi.object({ b: Joi.date().max(Joi.ref('$a')) });
var now = Date.now();
Helper.validate(schema, [
[{ b: now }, true, { context: { a: now } }],
[{ b: now + 1e3 }, false, { context: { a: now } }],
[{ b: now - 1e3 }, true, { context: { a: now } }]
], done);
});
it('errors if reference is not a date', function (done) {
var schema = Joi.object({ a: Joi.string(), b: Joi.date().max(Joi.ref('a')) });
var now = Date.now();
Helper.validate(schema, [
[{ a: 'abc', b: new Date() }, false, null, 'child "b" fails because ["b" references "a" which is not a date]'],
[{ a: '100000000000000', b: now }, true],
[{ a: (now - 1e3).toString(), b: now }, false, null, /^child "b" fails because \["b" must be less than or equal to/]
], done);
});
it('errors if context reference is not a date', function (done) {
var schema = Joi.object({ b: Joi.date().max(Joi.ref('$a')) });
var now = Date.now();
Helper.validate(schema, [
[{ b: now }, false, { context: { a: 'abc' } }, 'child "b" fails because ["b" references "a" which is not a date]'],
[{ b: now }, true, { context: { a: '100000000000000' } }],
[{ b: now }, false, { context: { a: (now - 1e3).toString() } }, /^child "b" fails because \["b" must be less than or equal to/]
], done);
});
});
it('validates only valid dates', function (done) {
Helper.validate(Joi.date(), [
['1-1-2013 UTC', true],
['not a valid date', false],
[new Date('not a valid date'), false]
], done);
});
describe('#iso', function () {
it('validates isoDate', function (done) {
Helper.validate(Joi.date().iso(), [
['2013-06-07T14:21:46.295Z', true],
['2013-06-07T14:21:46.295Z0', false],
['2013-06-07T14:21:46.295+07:00', true],
['2013-06-07T14:21:46.295+07:000', false],
['2013-06-07T14:21:46.295-07:00', true],
['2013-06-07T14:21:46Z', true],
['2013-06-07T14:21:46Z0', false],
['2013-06-07T14:21:46+07:00', true],
['2013-06-07T14:21:46-07:00', true],
['2013-06-07T14:21Z', true],
['2013-06-07T14:21+07:00', true],
['2013-06-07T14:21+07:000', false],
['2013-06-07T14:21-07:00', true],
['2013-06-07T14:21Z+7:00', false],
['2013-06-07', true],
['2013-06-07T', false],
['2013-06-07T14:21', true],
['1-1-2013', false]
], done);
});
it('validates isoDate with a friendly error message', function (done) {
var schema = { item: Joi.date().iso() };
Joi.compile(schema).validate({ item: 'something' }, function (err, value) {
expect(err.message).to.contain('must be a valid ISO 8601 date');
done();
});
});
it('validates isoDate after clone', function (done) {
var schema = { item: Joi.date().iso().clone() };
Joi.compile(schema).validate({ item: '2013-06-07T14:21:46.295Z' }, function (err, value) {
expect(err).to.not.exist();
done();
});
});
});
describe('#format', function () {
it('validates custom format', function (done) {
Helper.validate(Joi.date().format('DD#YYYY$MM'), [
['07#2013$06', true],
['2013-06-07', false]
], done);
});
it('validates several custom formats', function (done) {
Helper.validate(Joi.date().format(['DD#YYYY$MM', 'YY|DD|MM']), [
['13|07|06', true],
['2013-06-07', false]
], done);
});
it('fails with bad formats', function (done) {
expect(function () {
Joi.date().format(true);
}).to.throw('Invalid format.');
expect(function () {
Joi.date().format(['YYYYMMDD', true]);
}).to.throw('Invalid format.');
done();
});
});
});
});