@cleverbrush/schema 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +279 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +9 -0
- package/dist/schemaValidator.d.ts +14 -0
- package/dist/schemaValidator.js +170 -0
- package/dist/validators/validateArray.d.ts +2 -0
- package/dist/validators/validateArray.js +49 -0
- package/dist/validators/validateNumber.d.ts +2 -0
- package/dist/validators/validateNumber.js +69 -0
- package/dist/validators/validateObject.d.ts +2 -0
- package/dist/validators/validateObject.js +44 -0
- package/dist/validators/validateString.d.ts +2 -0
- package/dist/validators/validateString.js +50 -0
- package/package.json +20 -0
- package/src/index.ts +137 -0
- package/src/schemaValidator.test.ts +887 -0
- package/src/schemaValidator.ts +256 -0
- package/src/validators/validateArray.ts +66 -0
- package/src/validators/validateNumber.ts +87 -0
- package/src/validators/validateObject.ts +65 -0
- package/src/validators/validateString.ts +64 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,887 @@
|
|
|
1
|
+
import { deepEqual, deepExtend } from '@cleverbrush/deep';
|
|
2
|
+
import { ValidationResult } from '../dist/index.js';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
NumberSchemaDefinition,
|
|
6
|
+
ObjectSchemaDefinitionParam,
|
|
7
|
+
Schema
|
|
8
|
+
} from './index';
|
|
9
|
+
import SchemaValidator from './schemaValidator';
|
|
10
|
+
|
|
11
|
+
type User = {
|
|
12
|
+
id: number;
|
|
13
|
+
name: {
|
|
14
|
+
first: string;
|
|
15
|
+
last: string;
|
|
16
|
+
};
|
|
17
|
+
bornAt: Date;
|
|
18
|
+
diedAt?: Date;
|
|
19
|
+
incomePerMonth: number;
|
|
20
|
+
aliases: string[];
|
|
21
|
+
spouse?: User;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const getUserSchema = (): ObjectSchemaDefinitionParam<User> => ({
|
|
25
|
+
properties: {
|
|
26
|
+
id: 'number',
|
|
27
|
+
name: {
|
|
28
|
+
type: 'object',
|
|
29
|
+
isNullable: false,
|
|
30
|
+
isRequired: true,
|
|
31
|
+
properties: {
|
|
32
|
+
first: {
|
|
33
|
+
type: 'string',
|
|
34
|
+
isNullable: false,
|
|
35
|
+
isRequired: true,
|
|
36
|
+
minLength: 1,
|
|
37
|
+
maxLength: 100
|
|
38
|
+
},
|
|
39
|
+
last: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
isNullable: false,
|
|
42
|
+
isRequired: true,
|
|
43
|
+
minLength: 1,
|
|
44
|
+
maxLength: 100
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
incomePerMonth: {
|
|
49
|
+
type: 'number',
|
|
50
|
+
min: 0
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('Can add schema', () => {
|
|
56
|
+
const validator = new SchemaValidator().addSchemaType(
|
|
57
|
+
'something',
|
|
58
|
+
getUserSchema()
|
|
59
|
+
);
|
|
60
|
+
expect(validator.schemas.something).toBeDefined();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('Error thrown on adding a schema with duplicate name', () => {
|
|
64
|
+
expect(() =>
|
|
65
|
+
new SchemaValidator()
|
|
66
|
+
.addSchemaType('smth', {})
|
|
67
|
+
.addSchemaType('smth', {})
|
|
68
|
+
).toThrow();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('Receiving the same schema after add', () => {
|
|
72
|
+
const schema: ObjectSchemaDefinitionParam<{ name: string }> = {
|
|
73
|
+
properties: {
|
|
74
|
+
name: 'string'
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const validator = new SchemaValidator().addSchemaType('something', schema);
|
|
78
|
+
|
|
79
|
+
expect(
|
|
80
|
+
deepEqual(
|
|
81
|
+
{ ...schema, type: 'object' },
|
|
82
|
+
validator.schemas.something.schema
|
|
83
|
+
)
|
|
84
|
+
).toEqual(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('Schemas property is cached when reading', () => {
|
|
88
|
+
const validator = new SchemaValidator().addSchemaType('something', {});
|
|
89
|
+
expect(validator.schemas).toEqual(validator.schemas);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('Schemas property is updated after adding a new schema', () => {
|
|
93
|
+
let validator = new SchemaValidator().addSchemaType('something', {});
|
|
94
|
+
const s = validator.schemas;
|
|
95
|
+
validator = validator.addSchemaType('another', {});
|
|
96
|
+
expect(s).not.toEqual(validator.schemas);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('Trows when trying to add a schema with name = "number"', () => {
|
|
100
|
+
let validator = new SchemaValidator();
|
|
101
|
+
expect(() => validator.addSchemaType('number', {})).toThrow();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('Trows when trying to add a schema with name = "array"', () => {
|
|
105
|
+
let validator = new SchemaValidator();
|
|
106
|
+
expect(() => validator.addSchemaType('array', {})).toThrow();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('Trows when trying to add a schema with name = "date"', () => {
|
|
110
|
+
let validator = new SchemaValidator();
|
|
111
|
+
expect(() => validator.addSchemaType('date', {})).toThrow();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('Trows when trying to add a schema with name = "string"', () => {
|
|
115
|
+
let validator = new SchemaValidator();
|
|
116
|
+
expect(() => validator.addSchemaType('string', {})).toThrow();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('Throws if schema name is empty', () => {
|
|
120
|
+
let validator = new SchemaValidator();
|
|
121
|
+
expect(() => validator.addSchemaType('', {})).toThrow();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('Throws if schema name is not a string', () => {
|
|
125
|
+
let validator = new SchemaValidator();
|
|
126
|
+
expect(() =>
|
|
127
|
+
validator.addSchemaType(new Date() as any as string, {})
|
|
128
|
+
).toThrow();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('Throws if schema is not an object', () => {
|
|
132
|
+
let validator = new SchemaValidator();
|
|
133
|
+
expect(() =>
|
|
134
|
+
validator.addSchemaType(
|
|
135
|
+
'string',
|
|
136
|
+
'string' as any as ObjectSchemaDefinitionParam<any>
|
|
137
|
+
)
|
|
138
|
+
).toThrow();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('Validate - no schema', async () => {
|
|
142
|
+
const validator = new SchemaValidator();
|
|
143
|
+
const cth = jest.fn();
|
|
144
|
+
validator
|
|
145
|
+
.validate(null, 10)
|
|
146
|
+
.catch(cth)
|
|
147
|
+
.then(() => {
|
|
148
|
+
expect(cth).toBeCalled();
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('Validate - no schema type', async () => {
|
|
153
|
+
const validator = new SchemaValidator();
|
|
154
|
+
const cth = jest.fn();
|
|
155
|
+
validator
|
|
156
|
+
.validate(
|
|
157
|
+
{
|
|
158
|
+
isRequired: true
|
|
159
|
+
} as Schema<any>,
|
|
160
|
+
10
|
|
161
|
+
)
|
|
162
|
+
.catch(cth)
|
|
163
|
+
.then(() => {
|
|
164
|
+
expect(cth).toBeCalled();
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('Validate - number by value', async () => {
|
|
169
|
+
const validator = new SchemaValidator();
|
|
170
|
+
const result = await validator.validate(10, 0);
|
|
171
|
+
expect(result).toHaveProperty('valid', false);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('Validate - number by value - 2', async () => {
|
|
175
|
+
const validator = new SchemaValidator();
|
|
176
|
+
const result = await validator.validate(10, 10);
|
|
177
|
+
expect(result).toHaveProperty('valid', true);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('Validate - number by value - 3', async () => {
|
|
181
|
+
const validator = new SchemaValidator();
|
|
182
|
+
const result = await validator.validate(10, {});
|
|
183
|
+
expect(result).toHaveProperty('valid', false);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test('Validate - number by value - 4', async () => {
|
|
187
|
+
const validator = new SchemaValidator();
|
|
188
|
+
const result = await validator.validate(10, 15);
|
|
189
|
+
expect(result).toHaveProperty('valid', false);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('Validate - number by name: object', async () => {
|
|
193
|
+
const validator = new SchemaValidator();
|
|
194
|
+
const result = await validator.validate('number', {});
|
|
195
|
+
expect(result).toHaveProperty('valid', false);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('Validate - number by name: string', async () => {
|
|
199
|
+
const validator = new SchemaValidator();
|
|
200
|
+
const result = await validator.validate('number', '10');
|
|
201
|
+
expect(result).toHaveProperty('valid', false);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('Validate - number by name - correct', async () => {
|
|
205
|
+
const validator = new SchemaValidator();
|
|
206
|
+
const result = await validator.validate('number', 10);
|
|
207
|
+
expect(result).toHaveProperty('valid', true);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('Validate - number by schema', async () => {
|
|
211
|
+
const validator = new SchemaValidator();
|
|
212
|
+
const result = await validator.validate(
|
|
213
|
+
{
|
|
214
|
+
type: 'number'
|
|
215
|
+
},
|
|
216
|
+
10
|
|
217
|
+
);
|
|
218
|
+
expect(result).toHaveProperty('valid', true);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('Validate - number by schema - not number passed', async () => {
|
|
222
|
+
const validator = new SchemaValidator();
|
|
223
|
+
const result = await validator.validate(
|
|
224
|
+
{
|
|
225
|
+
type: 'number'
|
|
226
|
+
},
|
|
227
|
+
'str'
|
|
228
|
+
);
|
|
229
|
+
expect(result).toHaveProperty('valid', false);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('Validate - number by schema - min', async () => {
|
|
233
|
+
const validator = new SchemaValidator();
|
|
234
|
+
const result = await validator.validate(
|
|
235
|
+
{
|
|
236
|
+
type: 'number',
|
|
237
|
+
min: 1000
|
|
238
|
+
},
|
|
239
|
+
10
|
|
240
|
+
);
|
|
241
|
+
expect(result).toHaveProperty('valid', false);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('Validate - number by schema - min 2', async () => {
|
|
245
|
+
const validator = new SchemaValidator();
|
|
246
|
+
const result = await validator.validate(
|
|
247
|
+
{
|
|
248
|
+
type: 'number',
|
|
249
|
+
min: 1000
|
|
250
|
+
},
|
|
251
|
+
10000
|
|
252
|
+
);
|
|
253
|
+
expect(result).toEqual({
|
|
254
|
+
valid: true
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test('Validate - number by schema - min 3', async () => {
|
|
259
|
+
const validator = new SchemaValidator();
|
|
260
|
+
const result = await validator.validate(
|
|
261
|
+
{
|
|
262
|
+
type: 'number',
|
|
263
|
+
min: 1000
|
|
264
|
+
},
|
|
265
|
+
1000
|
|
266
|
+
);
|
|
267
|
+
expect(result).toHaveProperty('valid', true);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('Validate - number by schema - min 4', async () => {
|
|
271
|
+
const validator = new SchemaValidator();
|
|
272
|
+
const mk = jest.fn();
|
|
273
|
+
validator
|
|
274
|
+
.validate(
|
|
275
|
+
{
|
|
276
|
+
type: 'number',
|
|
277
|
+
min: 'string' as any as number
|
|
278
|
+
},
|
|
279
|
+
10
|
|
280
|
+
)
|
|
281
|
+
.catch(mk)
|
|
282
|
+
.then(() => {
|
|
283
|
+
expect(mk).toBeCalled();
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('Validate - number by schema - max', async () => {
|
|
288
|
+
const validator = new SchemaValidator();
|
|
289
|
+
const result = await validator.validate(
|
|
290
|
+
{
|
|
291
|
+
type: 'number',
|
|
292
|
+
max: 1000
|
|
293
|
+
},
|
|
294
|
+
20000
|
|
295
|
+
);
|
|
296
|
+
expect(result).toHaveProperty('valid', false);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('Validate - number by schema - max 2', async () => {
|
|
300
|
+
const validator = new SchemaValidator();
|
|
301
|
+
const result = await validator.validate(
|
|
302
|
+
{
|
|
303
|
+
type: 'number',
|
|
304
|
+
max: 40000
|
|
305
|
+
},
|
|
306
|
+
10000
|
|
307
|
+
);
|
|
308
|
+
expect(result).toHaveProperty('valid', true);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('Validate - number by schema - max 3', async () => {
|
|
312
|
+
const validator = new SchemaValidator();
|
|
313
|
+
const result = await validator.validate(
|
|
314
|
+
{
|
|
315
|
+
type: 'number',
|
|
316
|
+
max: 1000
|
|
317
|
+
},
|
|
318
|
+
1000
|
|
319
|
+
);
|
|
320
|
+
expect(result).toHaveProperty('valid', true);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('Validate - number by schema - max 4', async () => {
|
|
324
|
+
const validator = new SchemaValidator();
|
|
325
|
+
const mk = jest.fn();
|
|
326
|
+
validator
|
|
327
|
+
.validate(
|
|
328
|
+
{
|
|
329
|
+
type: 'number',
|
|
330
|
+
max: 'string' as any as number
|
|
331
|
+
},
|
|
332
|
+
10
|
|
333
|
+
)
|
|
334
|
+
.catch(mk)
|
|
335
|
+
.then(() => {
|
|
336
|
+
expect(mk).toBeCalled();
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('Validate - number by schema - range', async () => {
|
|
341
|
+
const validator = new SchemaValidator();
|
|
342
|
+
const result = await validator.validate(
|
|
343
|
+
{
|
|
344
|
+
type: 'number',
|
|
345
|
+
min: 1,
|
|
346
|
+
max: 100
|
|
347
|
+
},
|
|
348
|
+
10
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
expect(result).toHaveProperty('valid', true);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test('Validate - number by schema - range - 2', async () => {
|
|
355
|
+
const validator = new SchemaValidator();
|
|
356
|
+
const result = await validator.validate(
|
|
357
|
+
{
|
|
358
|
+
type: 'number',
|
|
359
|
+
min: -100,
|
|
360
|
+
max: 100
|
|
361
|
+
},
|
|
362
|
+
-210
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
expect(result).toHaveProperty('valid', false);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test('Validate - number by schema - range - 3', async () => {
|
|
369
|
+
const validator = new SchemaValidator();
|
|
370
|
+
const result = await validator.validate(
|
|
371
|
+
{
|
|
372
|
+
type: 'number',
|
|
373
|
+
min: -100,
|
|
374
|
+
max: 100
|
|
375
|
+
},
|
|
376
|
+
-100
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
expect(result).toHaveProperty('valid', true);
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test('Validate - number by schema - range - 4', async () => {
|
|
383
|
+
const validator = new SchemaValidator();
|
|
384
|
+
const result = await validator.validate(
|
|
385
|
+
{
|
|
386
|
+
type: 'number',
|
|
387
|
+
min: -100,
|
|
388
|
+
max: 100
|
|
389
|
+
},
|
|
390
|
+
100
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
expect(result).toHaveProperty('valid', true);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test('Validate - number by schema - range - 5', async () => {
|
|
397
|
+
const validator = new SchemaValidator();
|
|
398
|
+
const result = await validator.validate(
|
|
399
|
+
{
|
|
400
|
+
type: 'number',
|
|
401
|
+
min: -100,
|
|
402
|
+
max: 100
|
|
403
|
+
},
|
|
404
|
+
200
|
|
405
|
+
);
|
|
406
|
+
|
|
407
|
+
expect(result).toHaveProperty('valid', false);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
test('Validate - number by schema - range - 6', async () => {
|
|
411
|
+
const validator = new SchemaValidator();
|
|
412
|
+
const result = await validator.validate(
|
|
413
|
+
{
|
|
414
|
+
type: 'number',
|
|
415
|
+
min: -100,
|
|
416
|
+
max: 100
|
|
417
|
+
},
|
|
418
|
+
200
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
expect(result).toHaveProperty('valid', false);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
test('Validate - number by schema - NaN', async () => {
|
|
425
|
+
const validator = new SchemaValidator();
|
|
426
|
+
const result = await validator.validate(
|
|
427
|
+
{
|
|
428
|
+
type: 'number',
|
|
429
|
+
min: -100,
|
|
430
|
+
max: 100
|
|
431
|
+
},
|
|
432
|
+
0 / 0
|
|
433
|
+
);
|
|
434
|
+
expect(result).toHaveProperty('valid', false);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
test('Validate - number by schema - NaN - 2', async () => {
|
|
438
|
+
const validator = new SchemaValidator();
|
|
439
|
+
const result = await validator.validate(
|
|
440
|
+
{
|
|
441
|
+
type: 'number',
|
|
442
|
+
ensureNotNaN: false
|
|
443
|
+
},
|
|
444
|
+
0 / 0
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
expect(result).toHaveProperty('valid', true);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
test('Validate - number by schema - Infinity', async () => {
|
|
451
|
+
const validator = new SchemaValidator();
|
|
452
|
+
const result = await validator.validate(
|
|
453
|
+
{
|
|
454
|
+
type: 'number'
|
|
455
|
+
},
|
|
456
|
+
100 / 0
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
expect(result).toHaveProperty('valid', false);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test('Validate - number by schema - Infinity - 2', async () => {
|
|
463
|
+
const validator = new SchemaValidator();
|
|
464
|
+
const result = await validator.validate(
|
|
465
|
+
{
|
|
466
|
+
type: 'number',
|
|
467
|
+
ensureIsFinite: false
|
|
468
|
+
},
|
|
469
|
+
100 / 0
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
expect(result).toHaveProperty('valid', true);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
test('Validate - number by schema - custom validators - 1', async () => {
|
|
476
|
+
const validator = new SchemaValidator();
|
|
477
|
+
const schema: NumberSchemaDefinition<any> = {
|
|
478
|
+
type: 'number',
|
|
479
|
+
validators: [
|
|
480
|
+
async (value) => {
|
|
481
|
+
if ((value & 0xb01) === 0)
|
|
482
|
+
return {
|
|
483
|
+
valid: false,
|
|
484
|
+
errors: ['value should be odd!']
|
|
485
|
+
};
|
|
486
|
+
return {
|
|
487
|
+
valid: true
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
]
|
|
491
|
+
};
|
|
492
|
+
let result = await validator.validate(schema, 101);
|
|
493
|
+
|
|
494
|
+
expect(result).toHaveProperty('valid', true);
|
|
495
|
+
|
|
496
|
+
result = await validator.validate(schema, 100);
|
|
497
|
+
expect(result).toHaveProperty('valid', false);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
test('Validate - number by schema - custom validators - 2', async () => {
|
|
501
|
+
const validator = new SchemaValidator();
|
|
502
|
+
const schema: NumberSchemaDefinition<any> = {
|
|
503
|
+
type: 'number',
|
|
504
|
+
validators: [
|
|
505
|
+
(value) => {
|
|
506
|
+
if ((value & 0xb01) === 0)
|
|
507
|
+
return {
|
|
508
|
+
valid: false,
|
|
509
|
+
errors: ['value should be odd!']
|
|
510
|
+
};
|
|
511
|
+
return {
|
|
512
|
+
valid: true
|
|
513
|
+
};
|
|
514
|
+
},
|
|
515
|
+
(value) => ({ valid: value > 100 })
|
|
516
|
+
]
|
|
517
|
+
};
|
|
518
|
+
let result = await validator.validate(schema, 101);
|
|
519
|
+
|
|
520
|
+
expect(result).toHaveProperty('valid', true);
|
|
521
|
+
|
|
522
|
+
result = await validator.validate(schema, 99);
|
|
523
|
+
expect(result).toHaveProperty('valid', false);
|
|
524
|
+
|
|
525
|
+
result = await validator.validate(schema, 100);
|
|
526
|
+
expect(result).toHaveProperty('valid', false);
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
test('Validate - string - 1', async () => {
|
|
530
|
+
const validator = new SchemaValidator();
|
|
531
|
+
const result = await validator.validate('string', '12345');
|
|
532
|
+
expect(result).toHaveProperty('valid', true);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test('Validate - string - 2', async () => {
|
|
536
|
+
const validator = new SchemaValidator();
|
|
537
|
+
const result = await validator.validate('string', 12345);
|
|
538
|
+
expect(result).toHaveProperty('valid', false);
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
test('Validate - string - 3', async () => {
|
|
542
|
+
const validator = new SchemaValidator();
|
|
543
|
+
const result = await validator.validate('12345', '12345');
|
|
544
|
+
expect(result).toHaveProperty('valid', true);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
test('Validate - string - 4', async () => {
|
|
548
|
+
const validator = new SchemaValidator();
|
|
549
|
+
const result = await validator.validate('12345', '123456');
|
|
550
|
+
expect(result).toHaveProperty('valid', false);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
test('Validate - string - 5', async () => {
|
|
554
|
+
const validator = new SchemaValidator();
|
|
555
|
+
const result = await validator.validate('12345', 123456);
|
|
556
|
+
expect(result).toHaveProperty('valid', false);
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
test('Validate - string - schema object - 1', async () => {
|
|
560
|
+
const validator = new SchemaValidator();
|
|
561
|
+
const result = await validator.validate(
|
|
562
|
+
{
|
|
563
|
+
type: 'string',
|
|
564
|
+
equals: '123456'
|
|
565
|
+
},
|
|
566
|
+
'123456'
|
|
567
|
+
);
|
|
568
|
+
expect(result).toHaveProperty('valid', true);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
test('Validate - string - schema object - 2', async () => {
|
|
572
|
+
const validator = new SchemaValidator();
|
|
573
|
+
const result = await validator.validate(
|
|
574
|
+
{
|
|
575
|
+
type: 'string',
|
|
576
|
+
equals: '123456'
|
|
577
|
+
},
|
|
578
|
+
'12345'
|
|
579
|
+
);
|
|
580
|
+
expect(result).toHaveProperty('valid', false);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
test('Validate - string - schema object - 3', async () => {
|
|
584
|
+
const validator = new SchemaValidator();
|
|
585
|
+
const result = await validator.validate(
|
|
586
|
+
{
|
|
587
|
+
type: 'string',
|
|
588
|
+
equals: '123456'
|
|
589
|
+
},
|
|
590
|
+
1234
|
|
591
|
+
);
|
|
592
|
+
expect(result).toHaveProperty('valid', false);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
test('Validate - string - length control - 1', async () => {
|
|
596
|
+
const validator = new SchemaValidator();
|
|
597
|
+
let result = await validator.validate(
|
|
598
|
+
{
|
|
599
|
+
type: 'string',
|
|
600
|
+
minLength: 2,
|
|
601
|
+
maxLength: 3
|
|
602
|
+
},
|
|
603
|
+
'U'
|
|
604
|
+
);
|
|
605
|
+
expect(result).toHaveProperty('valid', false);
|
|
606
|
+
|
|
607
|
+
result = await validator.validate(
|
|
608
|
+
{
|
|
609
|
+
type: 'string',
|
|
610
|
+
minLength: 2,
|
|
611
|
+
maxLength: 3
|
|
612
|
+
},
|
|
613
|
+
'US'
|
|
614
|
+
);
|
|
615
|
+
expect(result).toHaveProperty('valid', true);
|
|
616
|
+
|
|
617
|
+
result = await validator.validate(
|
|
618
|
+
{
|
|
619
|
+
type: 'string',
|
|
620
|
+
minLength: 2,
|
|
621
|
+
maxLength: 3
|
|
622
|
+
},
|
|
623
|
+
'USA'
|
|
624
|
+
);
|
|
625
|
+
expect(result).toHaveProperty('valid', true);
|
|
626
|
+
|
|
627
|
+
result = await validator.validate(
|
|
628
|
+
{
|
|
629
|
+
type: 'string',
|
|
630
|
+
minLength: 2,
|
|
631
|
+
maxLength: 3
|
|
632
|
+
},
|
|
633
|
+
'USA'
|
|
634
|
+
);
|
|
635
|
+
expect(result).toHaveProperty('valid', true);
|
|
636
|
+
|
|
637
|
+
result = await validator.validate(
|
|
638
|
+
{
|
|
639
|
+
type: 'string',
|
|
640
|
+
minLength: 2,
|
|
641
|
+
maxLength: 3
|
|
642
|
+
},
|
|
643
|
+
'United States of America'
|
|
644
|
+
);
|
|
645
|
+
expect(result).toHaveProperty('valid', false);
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
test('Validate - array - 1', async () => {
|
|
649
|
+
const validator = new SchemaValidator();
|
|
650
|
+
let result = await validator.validate('array', []);
|
|
651
|
+
expect(result).toHaveProperty('valid', true);
|
|
652
|
+
|
|
653
|
+
result = await validator.validate('array', 123);
|
|
654
|
+
expect(result).toHaveProperty('valid', false);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
test('Validate - array - size control - 1', async () => {
|
|
658
|
+
const validator = new SchemaValidator();
|
|
659
|
+
let result = await validator.validate(
|
|
660
|
+
{
|
|
661
|
+
type: 'array',
|
|
662
|
+
minLength: 1,
|
|
663
|
+
maxLength: 3
|
|
664
|
+
},
|
|
665
|
+
[]
|
|
666
|
+
);
|
|
667
|
+
expect(result).toHaveProperty('valid', false);
|
|
668
|
+
|
|
669
|
+
result = await validator.validate(
|
|
670
|
+
{
|
|
671
|
+
type: 'array',
|
|
672
|
+
minLength: 1,
|
|
673
|
+
maxLength: 3
|
|
674
|
+
},
|
|
675
|
+
[1]
|
|
676
|
+
);
|
|
677
|
+
expect(result).toHaveProperty('valid', true);
|
|
678
|
+
|
|
679
|
+
result = await validator.validate(
|
|
680
|
+
{
|
|
681
|
+
type: 'array',
|
|
682
|
+
minLength: 1,
|
|
683
|
+
maxLength: 3
|
|
684
|
+
},
|
|
685
|
+
[1, 2]
|
|
686
|
+
);
|
|
687
|
+
expect(result).toHaveProperty('valid', true);
|
|
688
|
+
|
|
689
|
+
result = await validator.validate(
|
|
690
|
+
{
|
|
691
|
+
type: 'array',
|
|
692
|
+
minLength: 1,
|
|
693
|
+
maxLength: 3
|
|
694
|
+
},
|
|
695
|
+
[1, 2, 3]
|
|
696
|
+
);
|
|
697
|
+
expect(result).toHaveProperty('valid', true);
|
|
698
|
+
|
|
699
|
+
result = await validator.validate(
|
|
700
|
+
{
|
|
701
|
+
type: 'array',
|
|
702
|
+
minLength: 1,
|
|
703
|
+
maxLength: 3
|
|
704
|
+
},
|
|
705
|
+
[1, 2, 3, 4]
|
|
706
|
+
);
|
|
707
|
+
expect(result).toHaveProperty('valid', false);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
test('Validate - array - ofType - 1', async () => {
|
|
711
|
+
const validator = new SchemaValidator();
|
|
712
|
+
let result = await validator.validate(
|
|
713
|
+
{
|
|
714
|
+
type: 'array',
|
|
715
|
+
ofType: 'number'
|
|
716
|
+
},
|
|
717
|
+
['1', 2, 3]
|
|
718
|
+
);
|
|
719
|
+
expect(result).toHaveProperty('valid', false);
|
|
720
|
+
|
|
721
|
+
result = await validator.validate(
|
|
722
|
+
{
|
|
723
|
+
type: 'array',
|
|
724
|
+
ofType: 'number'
|
|
725
|
+
},
|
|
726
|
+
[1, 2, 3]
|
|
727
|
+
);
|
|
728
|
+
expect(result).toHaveProperty('valid', true);
|
|
729
|
+
|
|
730
|
+
result = await validator.validate(
|
|
731
|
+
{
|
|
732
|
+
type: 'array',
|
|
733
|
+
ofType: {
|
|
734
|
+
type: 'number',
|
|
735
|
+
min: 10
|
|
736
|
+
}
|
|
737
|
+
},
|
|
738
|
+
[-1, 12, 13]
|
|
739
|
+
);
|
|
740
|
+
expect(result).toHaveProperty('valid', false);
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
test('Validate - object - 1', async () => {
|
|
744
|
+
let validator = new SchemaValidator().addSchemaType(
|
|
745
|
+
'user',
|
|
746
|
+
getUserSchema()
|
|
747
|
+
);
|
|
748
|
+
const user: User = {
|
|
749
|
+
id: 1,
|
|
750
|
+
name: {
|
|
751
|
+
first: 'Andrew',
|
|
752
|
+
last: 'Zolotukhin'
|
|
753
|
+
},
|
|
754
|
+
bornAt: new Date(1986, 4, 30),
|
|
755
|
+
incomePerMonth: 134234,
|
|
756
|
+
aliases: []
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
let result = await validator.validate('user', user);
|
|
760
|
+
|
|
761
|
+
expect(result).toHaveProperty('valid', true);
|
|
762
|
+
|
|
763
|
+
result = await validator.validate('user', 10);
|
|
764
|
+
|
|
765
|
+
expect(result).toHaveProperty('valid', false);
|
|
766
|
+
|
|
767
|
+
result = await validator.validate(
|
|
768
|
+
{
|
|
769
|
+
type: 'object',
|
|
770
|
+
properties: {
|
|
771
|
+
name: {
|
|
772
|
+
type: 'string',
|
|
773
|
+
maxLength: 5
|
|
774
|
+
},
|
|
775
|
+
address: {
|
|
776
|
+
type: 'object',
|
|
777
|
+
properties: {
|
|
778
|
+
street: 'string',
|
|
779
|
+
house: {
|
|
780
|
+
type: 'number',
|
|
781
|
+
isRequired: false
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
},
|
|
787
|
+
{
|
|
788
|
+
name: 'Andr',
|
|
789
|
+
address: {
|
|
790
|
+
street: 'something'
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
);
|
|
794
|
+
|
|
795
|
+
expect(result).toHaveProperty('valid', true);
|
|
796
|
+
|
|
797
|
+
result = await validator.validate(
|
|
798
|
+
{
|
|
799
|
+
type: 'object',
|
|
800
|
+
properties: {
|
|
801
|
+
a: 'number',
|
|
802
|
+
b: 'number'
|
|
803
|
+
},
|
|
804
|
+
validators: [
|
|
805
|
+
(value) => {
|
|
806
|
+
if (value.a + value.b === 5) {
|
|
807
|
+
return {
|
|
808
|
+
valid: true
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
return {
|
|
812
|
+
valid: false,
|
|
813
|
+
error: ['some error']
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
]
|
|
817
|
+
},
|
|
818
|
+
{
|
|
819
|
+
a: 1,
|
|
820
|
+
b: 3
|
|
821
|
+
}
|
|
822
|
+
);
|
|
823
|
+
expect(result).toHaveProperty('valid', false);
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
test('Validate - object - 2', async () => {
|
|
827
|
+
let validator = new SchemaValidator().addSchemaType(
|
|
828
|
+
'user',
|
|
829
|
+
deepExtend(getUserSchema(), {
|
|
830
|
+
validators: [
|
|
831
|
+
(value): ValidationResult => {
|
|
832
|
+
if (value.bornAt < new Date(1990, 0, 1)) {
|
|
833
|
+
return {
|
|
834
|
+
valid: true
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
return {
|
|
838
|
+
valid: false,
|
|
839
|
+
errors: [`bornAt should be before Jan 1 1990`]
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
]
|
|
843
|
+
})
|
|
844
|
+
);
|
|
845
|
+
const user: User = {
|
|
846
|
+
id: 1,
|
|
847
|
+
name: {
|
|
848
|
+
first: 'Andrew',
|
|
849
|
+
last: 'Zolotukhin'
|
|
850
|
+
},
|
|
851
|
+
bornAt: new Date(1996, 4, 30),
|
|
852
|
+
incomePerMonth: 134234,
|
|
853
|
+
aliases: []
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
let result = await validator.validate('user', user);
|
|
857
|
+
expect(result).toHaveProperty('valid', false);
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
test('Validate - one of - 1', async () => {
|
|
861
|
+
const validator = new SchemaValidator();
|
|
862
|
+
|
|
863
|
+
let result = await validator.validate(['number', 'object'], 'something');
|
|
864
|
+
expect(result).toHaveProperty('valid', false);
|
|
865
|
+
|
|
866
|
+
result = await validator.validate(['number', 'string'], 'something');
|
|
867
|
+
expect(result).toHaveProperty('valid', true);
|
|
868
|
+
|
|
869
|
+
result = await validator.validate(
|
|
870
|
+
[
|
|
871
|
+
'number',
|
|
872
|
+
'string',
|
|
873
|
+
{
|
|
874
|
+
type: 'object',
|
|
875
|
+
properties: {
|
|
876
|
+
first: 'string',
|
|
877
|
+
last: 'string'
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
],
|
|
881
|
+
{
|
|
882
|
+
first: 'something',
|
|
883
|
+
last: 'another'
|
|
884
|
+
}
|
|
885
|
+
);
|
|
886
|
+
expect(result).toHaveProperty('valid', true);
|
|
887
|
+
});
|