-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathschema.js
More file actions
374 lines (328 loc) · 12.1 KB
/
schema.js
File metadata and controls
374 lines (328 loc) · 12.1 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import Schema from '../src/schema';
import Property from '../src/property';
import ValidationError from '../src/error';
import Messages from '../src/messages';
describe('Schema', () => {
describe('when given an object', () => {
test('should create properties', () => {
const schema = new Schema({ a: { type: String } });
expect(schema.props).toHaveProperty(['a']);
});
});
describe('.path()', () => {
test('should create properties', () => {
const schema = new Schema();
schema.path('a', { type: String });
expect(schema.props).toHaveProperty(['a']);
});
test('should allow type shorthand', () => {
const schema = new Schema();
schema.path('a', 'string');
schema.path('b', String);
expect(schema.props.a._type).toBe('string');
expect(schema.props.b._type).toBe(String);
});
test('should accept implicit arrays', () => {
const schema = new Schema();
schema.path('a', [String]);
schema.path('b', [String, Number]);
const errors = schema.validate({ a: ['a', 1], b: ['a', 'b'] });
expect(errors.length).toBe(2);
expect(errors[0].path).toBe('a.1');
expect(errors[1].path).toBe('b.1');
});
test('should create properties for all subpaths', () => {
const schema = new Schema();
schema.path('hello.planet.earth');
expect(schema.props).toHaveProperty(['hello']);
expect(schema.props).toHaveProperty(['hello.planet']);
expect(schema.props).toHaveProperty(['hello.planet.earth']);
});
test('should support nested properties', () => {
const schema = new Schema();
schema.path('a', { b: { type: String } });
expect(schema.props).toHaveProperty(['a.b']);
});
test('should support implicit object type', () => {
const schema = new Schema();
schema.path('a', { b: { type: String } });
expect(schema.props.a._type).toBe(Object);
});
test('should register validators', () => {
const schema = new Schema();
schema.path('a', { b: { required: true } });
expect(schema.validate({})).toHaveLength(1);
});
test('should return a Property', () => {
const schema = new Schema();
expect(schema.path('a')).toBeInstanceOf(Property);
});
test('should work with nested schemas', () => {
const schema1 = new Schema();
const schema2 = new Schema();
schema2.path('hello', { required: true });
schema1.path('schema2', schema2).required(true);
expect(schema1.props).toHaveProperty(['schema2.hello']);
expect(schema1.validate({})).toHaveLength(2);
expect(schema1.validate({ schema2: { hello: null } })).toHaveLength(1);
expect(schema1.validate({ schema2: { hello: 'world' } })).toHaveLength(0);
});
test('should propagate new props from nested schema', () => {
const schema1 = new Schema();
const schema2 = new Schema();
schema1.path('schema2', schema2);
schema2.path('hello', { required: true });
schema2.path('hello.world', { required: true });
expect(schema1.props).toHaveProperty(['schema2.hello']);
expect(schema1.props).toHaveProperty(['schema2.hello.world']);
});
describe('when given a path ending with $', () => {
test('should set `property.type` to array', () => {
const schema = new Schema();
schema.path('hello.$');
expect(schema.props.hello._type).toBe(Array);
});
test('should apply rules to each element in the array', () => {
const schema = new Schema();
schema.path('hello.$').type(Number);
expect(schema.props.hello._type).toBe(Array);
expect(schema.props['hello.$']._type).toBe(Number);
});
});
describe('when given a path ending with *', () => {
test('should set `property.type` to object', () => {
const schema = new Schema();
schema.path('hello.*');
expect(schema.props.hello._type).toBe(Object);
});
test('should apply rules to each property of the object', () => {
const schema = new Schema();
schema.path('hello.*').type(Number);
expect(schema.props.hello._type).toBe(Object);
expect(schema.props['hello.*']._type).toBe(Number);
});
});
});
describe('.strip()', () => {
test('should delete all keys not in the schema', () => {
const schema = new Schema({
a: { type: Number },
b: [{ a: { type: Number } }],
c: { a: { type: Number } }
});
const obj = { a: 1, b: [{ a: 1, b: 1 }, { a: 1 }], c: { a: 1, b: 1 }, d: 1 };
schema.strip(obj);
expect(obj).toEqual({ a: 1, b: [{ a: 1 }, { a: 1 }], c: { a: 1 } });
});
});
describe('.validate()', () => {
test('should return an array of errors', () => {
const schema = new Schema({ name: { type: String } });
const res = schema.validate({ name: 123 });
expect(res).toBeInstanceOf(Array);
expect(res).toHaveLength(1);
});
test('should set the correct paths on the error objects', () => {
const schema = new Schema({ things: [{ type: String }] });
const res = schema.validate({ things: ['car', 1, 3] });
const [err1, err2] = res;
expect(res).toHaveLength(2);
expect(err1.path).toBe('things.1');
expect(err1.message).toMatch(/things\.1/);
expect(err2.path).toBe('things.2');
expect(err2.message).toMatch(/things\.2/);
});
test('should work with $ a placeholder for array indices', () => {
const schema = new Schema();
schema.path('a.$.b').required();
schema.path('a.$.b.$').type(String);
schema.path('a.$.c.$.$').type(String);
const res = schema.validate({
a: [
{ b: ['hello', 'world'] },
{ b: ['hello', 1] },
{ c: [['hello', 'world'], ['hello', 2]] }
]
});
expect(res).toHaveLength(3);
});
test('should work with * a placeholder for object keys', () => {
const schema = new Schema();
schema.path('a.*.c').required();
schema.path('a.*.b.*').type(String);
schema.path('a.*.b.*.*').type(String);
const res = schema.validate({
a: {
x: {
b: { z: { m: 'hello' } },
c: {
k: {
l: ['hello', 2]
}
}
},
y: {
b: { z: ['hello', 1] }
}
}
});
expect(res).toHaveLength(3);
});
test('should strip by default', () => {
const schema = new Schema({ a: { type: Number } });
const obj = { a: 1, b: 1 };
schema.validate(obj);
expect(obj).toEqual({ a: 1 });
});
test('should not strip array elements', () => {
const schema = new Schema({ a: { type: Array } });
const obj = { a: [1, 2, 3] };
schema.validate(obj);
expect(obj).toEqual({ a: [1, 2, 3] });
});
describe('with strip disabled', () => {
test('should not delete any keys', () => {
const obj = { name: 'name', age: 23 };
const schema = new Schema({ name: { type: String } });
schema.validate(obj, { strip: false });
expect(obj).toHaveProperty('age', 23);
});
});
describe('with typecasting enabled', () => {
test('should typecast before validation', () => {
const schema = new Schema({ name: { type: String } });
const res = schema.validate({ name: 123 }, { typecast: true });
expect(res).toHaveLength(0);
});
test('should typecast arrays and elements within arrays', () => {
const schema = new Schema();
schema.path('a.$.b').required();
schema.path('a.$.b.$').type(String);
schema.path('a.$.c.$.$').type(String);
schema.path('b.$').type(Number);
const obj = {
a: [{ b: ['a', 'b'] }, { b: 1, c: [['a', 'b'], ['a', 2]] }],
b: '1,2,3,4,5'
};
const res = schema.validate(obj, { typecast: true });
expect(res).toHaveLength(0);
expect(obj).toEqual({
a: [{ b: ['a', 'b'] }, { b: ['1'], c: [['a', 'b'], ['a', '2']] }],
b: [1, 2, 3, 4, 5]
});
});
test('should typecast objects and properties within objects', () => {
const schema = new Schema();
schema.path('a.*.b').required();
schema.path('a.*.b.*').type(String);
schema.path('a.*.c.*.*').type(String);
schema.path('b.*').type(Number);
const obj = {
a: { y: { b: { z: 1 } }, x: { b: { j: 'hello' }, c: { m: { l: { o: 2056 } } } } },
b: { n: '1', t: '2' }
};
const res = schema.validate(obj, { typecast: true });
expect(res).toHaveLength(0);
expect(obj).toEqual({
a: { y: { b: { z: '1' } }, x: { b: { j: 'hello' }, c: { m: { l: '[object Object]' } } } },
b: { n: 1, t: 2 }
});
});
test('should not typecast undefined', () => {
const schema = new Schema({ name: { type: String } });
const wrap = () => schema.validate({}, { typecast: true });
expect(wrap).not.toThrowError();
});
});
describe('with strict mode enabled', () => {
test('should generate errors for properties not in schema', () => {
const schema = new Schema({
a: Number,
b: [{ a: Number }],
c: { a: Number },
d: [[{ a: Number }]]
});
const obj = {
a: 1,
b: [{ a: 1, b: 1 }, { a: 1, c: { d: 1 } }],
c: { a: 1, b: 1 },
d: [[{ a: 1 }, { a: 1, b: 1 }]],
e: 1
};
const errors = schema.validate(obj, { strict: true });
const messages = errors.map(e => e.message);
const paths = errors.map(e => e.path);
expect(messages).toStrictEqual([
Messages.illegal('b.0.b'),
Messages.illegal('b.1.c'),
Messages.illegal('c.b'),
Messages.illegal('d.0.1.b'),
Messages.illegal('e')
]);
expect(paths).toStrictEqual([
'b.0.b',
'b.1.c',
'c.b',
'd.0.1.b',
'e'
]);
});
});
});
describe('.assert()', () => {
test('should throw if validation fails', () => {
const schema = new Schema({ name: { type: String } });
const wrap = () => schema.assert({ name: 123 });
expect(wrap).toThrowError();
});
});
describe('.message()', () => {
test('should set default messages', () => {
const schema = new Schema({ name: { required: true } });
schema.message('required', 'test');
const [error] = schema.validate({});
expect(error.message).toBe('test');
});
test('should accept an object of name-message pairs', () => {
const schema = new Schema({ name: { required: true } });
schema.message({ required: 'test' });
const [error] = schema.validate({});
expect(error.message).toBe('test');
});
});
describe('.validator()', () => {
test('should set default validators', () => {
const schema = new Schema({ name: { required: true } });
schema.validator('required', () => false);
const [error] = schema.validate({ name: 'hello' });
expect(error.message).toBe('name is required.');
});
test('should accept an object of name-function pairs', () => {
const schema = new Schema({ name: { required: true } });
schema.validator({ required: () => false });
const [error] = schema.validate({ name: 'hello' });
expect(error.message).toBe('name is required.');
});
});
describe('.typecaster()', () => {
test('should set default typecasters', () => {
const obj = { name: 123 };
const schema = new Schema({ name: { type: 'hello' } });
schema.typecaster('hello', val => val.toString());
schema.typecast(obj);
expect(obj.name).toBe('123');
});
test('should set default typecasters', () => {
const obj = { name: 123 };
const schema = new Schema({ name: { type: 'hello' } });
schema.typecaster({ hello: val => val.toString() });
schema.typecast(obj);
expect(obj.name).toBe('123');
});
});
describe('.ValidationError', () => {
test('should expose ValidationError', () => {
expect(Schema.ValidationError).toBe(ValidationError);
});
});
});