forked from jquense/yup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.js
193 lines (154 loc) · 4.51 KB
/
array.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
import string from '../src/string';
import number from '../src/number';
import object from '../src/object';
import array from '../src/array';
describe('Array types', () => {
describe('casting', () => {
it('should parse json strings', () => {
array()
.cast('[2,3,5,6]')
.should.eql([2, 3, 5, 6]);
});
it('should return null for failed casts', () => {
expect(array().cast('asfasf', { assert: false })).to.equal(null);
expect(array().cast(null, { assert: false })).to.equal(null);
});
it('should recursively cast fields', () => {
array()
.of(number())
.cast(['4', '5'])
.should.eql([4, 5]);
array()
.of(string())
.cast(['4', 5, false])
.should.eql(['4', '5', 'false']);
});
});
it('should handle DEFAULT', () => {
expect(array().default()).to.equal(undefined);
array()
.default(() => [1, 2, 3])
.default()
.should.eql([1, 2, 3]);
});
it('should type check', () => {
var inst = array();
inst.isType([]).should.equal(true);
inst.isType({}).should.equal(false);
inst.isType('true').should.equal(false);
inst.isType(NaN).should.equal(false);
inst.isType(34545).should.equal(false);
expect(inst.isType(null)).to.equal(false);
inst
.nullable()
.isType(null)
.should.equal(true);
});
it('should cast children', () => {
array()
.of(number())
.cast(['1', '3'])
.should.eql([1, 3]);
});
it('should concat subType correctly', () => {
expect(
array()
.of(number())
.concat(array())._subType,
).to.exist();
expect(
array()
.of(number())
.concat(array().of(false))._subType,
).to.equal(false);
});
it('should pass options to children', () => {
array(object({ name: string() }))
.cast([{ id: 1, name: 'john' }], { stripUnknown: true })
.should.eql([{ name: 'john' }]);
});
describe('validation', () => {
it('should allow undefined', async () => {
await array()
.of(number().max(5))
.isValid()
.should.become(true);
});
it('should not allow null when not nullable', async () => {
await array()
.isValid(null)
.should.become(false);
await array()
.nullable()
.isValid(null)
.should.become(true);
});
it('should respect subtype validations', async () => {
var inst = array().of(number().max(5));
await inst.isValid(['gg', 3]).should.become(false);
await inst.isValid([7, 3]).should.become(false);
let value = await inst.validate(['4', 3]);
value.should.eql([4, 3]);
});
it('should prevent recursive casting', async () => {
let castSpy = sinon.spy(string.prototype, '_cast');
let value = await array(string()).validate([5]);
value[0].should.equal('5');
castSpy.should.have.been.calledOnce();
string.prototype._cast.restore();
});
});
it('should respect abortEarly', () => {
var inst = array()
.of(object({ str: string().required() }))
.test('name', 'oops', () => false);
return Promise.all([
inst
.validate([{ str: '' }])
.should.be.rejected()
.then(err => {
err.value.should.eql([{ str: '' }]);
err.errors.length.should.equal(1);
err.errors.should.eql(['oops']);
}),
inst
.validate([{ str: '' }], { abortEarly: false })
.should.be.rejected()
.then(err => {
err.value.should.eql([{ str: '' }]);
err.errors.length.should.equal(2);
err.errors.should.eql(['[0].str is a required field', 'oops']);
}),
]);
});
it('should compact arrays', () => {
var arr = ['', 1, 0, 4, false, null],
inst = array();
inst
.compact()
.cast(arr)
.should.eql([1, 4]);
inst
.compact(v => v == null)
.cast(arr)
.should.eql(['', 1, 0, 4, false]);
});
it('should ensure arrays', () => {
var inst = array().ensure();
const a = [1, 4];
inst.cast(a).should.equal(a);
inst.cast(null).should.eql([]);
});
it('should pass resolved path to descendants', async () => {
let value = ['2', '3'];
let expectedPaths = ['[0]', '[1]'];
let itemSchema = string().when([], function(_, context) {
let path = context.path || '';
path.should.be.oneOf(expectedPaths);
return string().required();
});
await array()
.of(itemSchema)
.validate(value);
});
});