forked from jquense/yup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.js
263 lines (223 loc) · 5.84 KB
/
number.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
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
import * as TestHelpers from './helpers';
import number from '../src/number';
describe('Number types', function() {
it('is newable', () => {
let schema = new number();
schema.integer().required();
});
it('is extensible', () => {
class MyNumber extends number {
foo() {
return this;
}
}
new MyNumber()
.foo()
.integer()
.required();
});
describe('casting', () => {
let schema = number();
TestHelpers.castAll(schema, {
valid: [
['5', 5],
[3, 3],
//[new Number(5), 5],
[' 5.656 ', 5.656],
],
invalid: ['', false, true, new Date(), new Number('foo')],
});
it('should round', () => {
schema
.round('floor')
.cast(45.99999)
.should.equal(45);
schema
.round('ceIl')
.cast(45.1111)
.should.equal(46);
schema
.round()
.cast(45.444444)
.should.equal(45);
expect(
schema
.nullable()
.integer()
.round()
.cast(null),
).to.equal(null);
(function() {
schema.round('fasf');
}.should.throw(TypeError));
});
it('should truncate', () => {
schema
.truncate()
.cast(45.55)
.should.equal(45);
});
it('should return NaN for failed casts', () => {
expect(number().cast('asfasf', { assert: false })).to.eql(NaN);
expect(number().cast(null, { assert: false })).to.eql(NaN);
});
});
it('should handle DEFAULT', function() {
var inst = number().default(0);
inst.default().should.equal(0);
inst
.default(5)
.required()
.default()
.should.equal(5);
});
it('should type check', function() {
var inst = number();
inst.isType(5).should.equal(true);
inst.isType(new Number(5)).should.equal(true);
inst.isType(new Number('foo')).should.equal(false);
inst.isType(false).should.equal(false);
inst.isType(null).should.equal(false);
inst.isType(NaN).should.equal(false);
inst
.nullable()
.isType(null)
.should.equal(true);
});
it('should VALIDATE correctly', function() {
var inst = number()
.required()
.min(4);
return Promise.all([
number()
.isValid(null)
.should.eventually()
.equal(false),
number()
.nullable()
.isValid(null)
.should.eventually()
.equal(true),
number()
.isValid(' ')
.should.eventually()
.equal(false),
number()
.isValid('12abc')
.should.eventually()
.equal(false),
number()
.isValid(0xff)
.should.eventually.equal(true),
number()
.isValid('0xff')
.should.eventually.equal(true),
inst
.isValid(5)
.should.eventually()
.equal(true),
inst
.isValid(2)
.should.eventually()
.equal(false),
inst
.validate()
.should.be.rejected()
.then(function(err) {
err.errors.length.should.equal(1);
err.errors[0].should.contain('required');
}),
]);
});
describe('min', () => {
var schema = number().min(5);
TestHelpers.validateAll(schema, {
valid: [7, 35738787838, [null, schema.nullable()]],
invalid: [2, null, [14, schema.min(10).min(15)]],
});
});
describe('max', () => {
var schema = number().max(5);
TestHelpers.validateAll(schema, {
valid: [4, -5222, [null, schema.nullable()]],
invalid: [10, null, [16, schema.max(20).max(15)]],
});
});
describe('lessThan', () => {
var schema = number().lessThan(5);
TestHelpers.validateAll(schema, {
valid: [4, -10, [null, schema.nullable()]],
invalid: [5, 7, null, [14, schema.lessThan(10).lessThan(14)]],
});
it('should return default message', () => {
return schema
.validate(6)
.should.be.rejected.and.eventually.have.property('errors')
.that.contain('this must be less than 5');
});
});
describe('moreThan', () => {
var schema = number().moreThan(5);
TestHelpers.validateAll(schema, {
valid: [6, 56445435, [null, schema.nullable()]],
invalid: [5, -10, null, [64, schema.moreThan(52).moreThan(74)]],
});
it('should return default message', () => {
return schema
.validate(4)
.should.be.rejected.and.eventually.have.property('errors')
.that.contain('this must be greater than 5');
});
});
describe('integer', () => {
var schema = number().integer();
TestHelpers.validateAll(schema, {
valid: [4, -5222, 3.12312e51],
invalid: [10.53, 0.1 * 0.2, -34512535.626, new Date()],
});
it('should return default message', () => {
return schema
.validate(10.53)
.should.be.rejected.and.eventually.have.property('errors')
.that.contain('this must be an integer');
});
});
it('should check POSITIVE correctly', function() {
var v = number().positive();
return Promise.all([
v
.isValid(7)
.should.eventually()
.equal(true),
v
.isValid(0)
.should.eventually()
.equal(false),
v
.validate(0)
.should.be.rejected()
.then(null, function(err) {
err.errors[0].should.contain('this must be a positive number');
}),
]);
});
it('should check NEGATIVE correctly', function() {
var v = number().negative();
return Promise.all([
v
.isValid(-4)
.should.eventually()
.equal(true),
v
.isValid(0)
.should.eventually()
.equal(false),
v
.validate(10)
.should.be.rejected()
.then(null, function(err) {
err.errors[0].should.contain('this must be a negative number');
}),
]);
});
});