Repository navigation
Expand file tree
/
Copy pathyup.js
More file actions
244 lines (207 loc) · 6.17 KB
/
Copy pathyup.js
File metadata and controls
244 lines (207 loc) · 6.17 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
import { describe, it, expect, test } from 'vitest';
import reach, { getIn } from '../src/util/reach';
import {
addMethod,
object,
array,
string,
lazy,
number,
boolean,
date,
Schema,
ObjectSchema,
ArraySchema,
StringSchema,
NumberSchema,
BooleanSchema,
DateSchema,
mixed,
MixedSchema,
tuple,
} from '../src';
describe('Yup', function () {
it('cast should not assert on undefined', () => {
expect(() => string().cast(undefined)).not.toThrowError();
});
it('cast should assert on undefined cast results', () => {
expect(() =>
string()
.defined()
.transform(() => undefined)
.cast('foo'),
).toThrowError(
'The value of field could not be cast to a value that satisfies the schema type: "string".',
);
});
it('cast should respect assert option', () => {
expect(() => string().cast(null)).toThrowError();
expect(() => string().cast(null, { assert: false })).not.toThrowError();
});
it('should getIn correctly', async () => {
let num = number();
let shape = object({ 'num-1': num });
let inst = object({
num: number().max(4),
nested: object({
arr: array().of(shape),
}),
});
const value = { nested: { arr: [{}, { 'num-1': 2 }] } };
let { schema, parent, parentPath } = getIn(
inst,
'nested.arr[1].num-1',
value,
);
expect(schema).toBe(num);
expect(parentPath).toBe('num-1');
expect(parent).toBe(value.nested.arr[1]);
});
it('should getIn array correctly', async () => {
let num = number();
let shape = object({ 'num-1': num });
let inst = object({
num: number().max(4),
nested: object({
arr: array().of(shape),
}),
});
const value = {
nested: {
arr: [{}, { 'num-1': 2 }],
},
};
const { schema, parent, parentPath } = getIn(inst, 'nested.arr[1]', value);
expect(schema).toBe(shape);
expect(parentPath).toBe('1');
expect(parent).toBe(value.nested.arr);
});
it('should REACH correctly', async () => {
let num = number();
let shape = object({ num });
let inst = object({
num: number().max(4),
nested: tuple([
string(),
object({
arr: array().of(shape),
}),
]),
});
expect(reach(inst, '')).toBe(inst);
expect(reach(inst, 'nested[1].arr[0].num')).toBe(num);
expect(reach(inst, 'nested[1].arr[].num')).toBe(num);
expect(reach(inst, 'nested[1].arr.num')).toBe(num);
expect(reach(inst, 'nested[1].arr[1].num')).toBe(num);
expect(reach(inst, 'nested[1].arr[1]')).toBe(shape);
expect(() => reach(inst, 'nested.arr[1].num')).toThrowError(
'Yup.reach cannot implicitly index into a tuple type. the path part ".nested" must contain an index to the tuple element, e.g. ".nested[0]"',
);
await expect(reach(inst, 'nested[1].arr[0].num').isValid(5)).resolves.toBe(
true,
);
});
it('should REACH conditionally correctly', async function () {
let num = number().oneOf([4]),
inst = object().shape({
num: number().max(4),
nested: object().shape({
arr: array().when('$bar', function ([bar]) {
return bar !== 3
? array().of(number())
: array().of(
object().shape({
foo: number(),
num: number().when('foo', ([foo]) => {
if (foo === 5) return num;
}),
}),
);
}),
}),
});
let context = { bar: 3 };
let value = {
bar: 3,
nested: {
arr: [{ foo: 5 }, { foo: 3 }],
},
};
let options = {};
options.parent = value.nested.arr[0];
options.value = options.parent.num;
expect(reach(inst, 'nested.arr.num', value).resolve(options)).toBe(num);
expect(reach(inst, 'nested.arr[].num', value).resolve(options)).toBe(num);
options.context = context;
expect(reach(inst, 'nested.arr.num', value, context).resolve(options)).toBe(
num,
);
expect(
reach(inst, 'nested.arr[].num', value, context).resolve(options),
).toBe(num);
expect(
reach(inst, 'nested.arr[0].num', value, context).resolve(options),
).toBe(num);
// // should fail b/c item[1] is used to resolve the schema
options.parent = value.nested.arr[1];
options.value = options.parent.num;
expect(
reach(inst, 'nested["arr"][1].num', value, context).resolve(options),
).not.toBe(num);
let reached = reach(inst, 'nested.arr[].num', value, context);
await expect(
reached.validate(5, { context, parent: { foo: 4 } }),
).resolves.toBeDefined();
await expect(
reached.validate(5, { context, parent: { foo: 5 } }),
).rejects.toThrowError(/one of the following/);
});
it('should reach through lazy', async () => {
let types = {
1: object({ foo: string() }),
2: object({ foo: number() }),
};
await expect(
object({
x: array(lazy((val) => types[val.type])),
})
.strict()
.validate({
x: [
{ type: 1, foo: '4' },
{ type: 2, foo: '5' },
],
}),
).rejects.toThrowError(/must be a `number` type/);
});
describe('addMethod', () => {
it('extending Schema should make method accessible everywhere', () => {
addMethod(Schema, 'foo', () => 'here');
expect(string().foo()).toBe('here');
});
test.each([
['mixed', mixed],
['object', object],
['array', array],
['string', string],
['number', number],
['boolean', boolean],
['date', date],
])('should work with factories: %s', (_msg, factory) => {
addMethod(factory, 'foo', () => 'here');
expect(factory().foo()).toBe('here');
});
test.each([
['mixed', MixedSchema],
['object', ObjectSchema],
['array', ArraySchema],
['string', StringSchema],
['number', NumberSchema],
['boolean', BooleanSchema],
['date', DateSchema],
])('should work with classes: %s', (_msg, ctor) => {
addMethod(ctor, 'foo', () => 'here');
expect(new ctor().foo()).toBe('here');
});
});
});