forked from jquense/yup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.js
161 lines (144 loc) · 3.88 KB
/
date.js
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
import { ref, date } from '../src';
function isValidDate(date) {
return date instanceof Date && !isNaN(date.getTime());
}
describe('Date types', () => {
it('should CAST correctly', () => {
var inst = date();
inst.cast(new Date()).should.be.a('date');
inst.cast('jan 15 2014').should.eql(new Date(2014, 0, 15));
inst.cast('2014-09-23T19:25:25Z').should.eql(new Date(1411500325000));
// Leading-zero milliseconds
inst.cast('2016-08-10T11:32:19.012Z').should.eql(new Date(1470828739012));
// Microsecond precision
inst.cast('2016-08-10T11:32:19.2125Z').should.eql(new Date(1470828739212));
});
it('should return invalid date for failed casts', function() {
var inst = date();
inst.cast(null, { assert: false }).should.not.satisfy(isValidDate);
inst.cast('', { assert: false }).should.not.satisfy(isValidDate);
});
it('should type check', () => {
var inst = date();
inst.isType(new Date()).should.equal(true);
inst.isType(false).should.equal(false);
inst.isType(null).should.equal(false);
inst.isType(NaN).should.equal(false);
inst
.nullable()
.isType(new Date())
.should.equal(true);
});
it('should VALIDATE correctly', () => {
var inst = date()
.required()
.max(new Date(2014, 5, 15));
return Promise.all([
date()
.isValid(null)
.should.eventually()
.equal(false),
date()
.nullable()
.isValid(null)
.should.eventually()
.equal(true),
inst
.isValid(new Date(2014, 0, 15))
.should.eventually()
.equal(true),
inst
.isValid(new Date(2014, 7, 15))
.should.eventually()
.equal(false),
inst
.isValid('5')
.should.eventually()
.equal(true),
inst
.validate()
.should.be.rejected()
.then(err => {
err.errors.length.should.equal(1);
err.errors[0].should.contain('required');
}),
]);
});
it('should check MIN correctly', () => {
var min = new Date(2014, 3, 15),
invalid = new Date(2014, 1, 15),
valid = new Date(2014, 5, 15);
(function() {
date().max('hello');
}.should.throw(TypeError));
(function() {
date().max(ref('$foo'));
}.should.not.throw());
return Promise.all([
date()
.min(min)
.isValid(valid)
.should.eventually()
.equal(true),
date()
.min(min)
.isValid(invalid)
.should.eventually()
.equal(false),
date()
.min(min)
.isValid(null)
.should.eventually()
.equal(false),
date()
.min(ref('$foo'))
.isValid(valid, { context: { foo: min } })
.should.eventually()
.equal(true),
date()
.min(ref('$foo'))
.isValid(invalid, { context: { foo: min } })
.should.eventually()
.equal(false),
]);
});
it('should check MAX correctly', () => {
var max = new Date(2014, 7, 15),
invalid = new Date(2014, 9, 15),
valid = new Date(2014, 5, 15);
(function() {
date().max('hello');
}.should.throw(TypeError));
(function() {
date().max(ref('$foo'));
}.should.not.throw());
return Promise.all([
date()
.max(max)
.isValid(valid)
.should.eventually()
.equal(true),
date()
.max(max)
.isValid(invalid)
.should.eventually()
.equal(false),
date()
.max(max)
.nullable(true)
.isValid(null)
.should.eventually()
.equal(true),
date()
.max(ref('$foo'))
.isValid(valid, { context: { foo: max } })
.should.eventually()
.equal(true),
date()
.max(ref('$foo'))
.isValid(invalid, { context: { foo: max } })
.should.eventually()
.equal(false),
]);
});
});