@e22m4u/js-repository 0.6.4 → 0.7.0

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.
Files changed (90) hide show
  1. package/README.md +6 -532
  2. package/dist/cjs/index.cjs +141 -1156
  3. package/package.json +7 -7
  4. package/src/adapter/adapter.d.ts +2 -4
  5. package/src/adapter/adapter.js +2 -6
  6. package/src/adapter/adapter.spec.js +3 -15
  7. package/src/adapter/builtin/memory-adapter.js +41 -12
  8. package/src/adapter/builtin/memory-adapter.spec.js +47 -0
  9. package/src/adapter/decorator/index.d.ts +0 -2
  10. package/src/adapter/decorator/index.js +0 -2
  11. package/src/definition/model/index.d.ts +0 -2
  12. package/src/definition/model/index.js +0 -2
  13. package/src/definition/model/properties/index.d.ts +0 -2
  14. package/src/definition/model/properties/index.js +0 -2
  15. package/src/definition/model/properties/properties-definition-validator.js +0 -169
  16. package/src/definition/model/properties/properties-definition-validator.spec.js +0 -162
  17. package/src/definition/model/properties/property-definition.d.ts +1 -5
  18. package/src/definition/model/properties/property-uniqueness-validator.js +2 -2
  19. package/src/filter/filter-clause.d.ts +3 -4
  20. package/src/filter/where-clause-tool.js +2 -2
  21. package/src/utils/index.d.ts +1 -5
  22. package/src/utils/index.js +1 -5
  23. package/src/utils/is-plain-object.d.ts +6 -0
  24. package/src/utils/{is-pure-object.js → is-plain-object.js} +2 -2
  25. package/src/utils/is-plain-object.spec.js +25 -0
  26. package/src/utils/model-name-to-model-key.js +0 -4
  27. package/src/utils/model-name-to-model-key.spec.js +0 -23
  28. package/tsconfig.json +2 -2
  29. package/src/adapter/decorator/data-transformation-decorator.d.ts +0 -14
  30. package/src/adapter/decorator/data-transformation-decorator.js +0 -54
  31. package/src/adapter/decorator/data-transformation-decorator.spec.js +0 -193
  32. package/src/adapter/decorator/data-validation-decorator.d.ts +0 -14
  33. package/src/adapter/decorator/data-validation-decorator.js +0 -54
  34. package/src/adapter/decorator/data-validation-decorator.spec.js +0 -105
  35. package/src/definition/model/model-data-transformer.d.ts +0 -16
  36. package/src/definition/model/model-data-transformer.js +0 -170
  37. package/src/definition/model/model-data-transformer.spec.js +0 -2312
  38. package/src/definition/model/model-data-validator.d.ts +0 -16
  39. package/src/definition/model/model-data-validator.js +0 -318
  40. package/src/definition/model/model-data-validator.spec.js +0 -4528
  41. package/src/definition/model/properties/property-transformer/builtin/index.d.ts +0 -3
  42. package/src/definition/model/properties/property-transformer/builtin/index.js +0 -3
  43. package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.d.ts +0 -6
  44. package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.js +0 -19
  45. package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.spec.js +0 -39
  46. package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.d.ts +0 -6
  47. package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.js +0 -19
  48. package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.spec.js +0 -39
  49. package/src/definition/model/properties/property-transformer/builtin/trim-transformer.d.ts +0 -6
  50. package/src/definition/model/properties/property-transformer/builtin/trim-transformer.js +0 -19
  51. package/src/definition/model/properties/property-transformer/builtin/trim-transformer.spec.js +0 -39
  52. package/src/definition/model/properties/property-transformer/index.d.ts +0 -2
  53. package/src/definition/model/properties/property-transformer/index.js +0 -2
  54. package/src/definition/model/properties/property-transformer/property-transformer-registry.d.ts +0 -29
  55. package/src/definition/model/properties/property-transformer/property-transformer-registry.js +0 -76
  56. package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +0 -133
  57. package/src/definition/model/properties/property-transformer/property-transformer.d.ts +0 -27
  58. package/src/definition/model/properties/property-transformer/property-transformer.js +0 -1
  59. package/src/definition/model/properties/property-validator/builtin/index.d.ts +0 -3
  60. package/src/definition/model/properties/property-validator/builtin/index.js +0 -3
  61. package/src/definition/model/properties/property-validator/builtin/max-length-validator.d.ts +0 -6
  62. package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +0 -28
  63. package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +0 -100
  64. package/src/definition/model/properties/property-validator/builtin/min-length-validator.d.ts +0 -6
  65. package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +0 -28
  66. package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +0 -100
  67. package/src/definition/model/properties/property-validator/builtin/regexp-validator.d.ts +0 -6
  68. package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +0 -30
  69. package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +0 -102
  70. package/src/definition/model/properties/property-validator/index.d.ts +0 -2
  71. package/src/definition/model/properties/property-validator/index.js +0 -2
  72. package/src/definition/model/properties/property-validator/property-validator-registry.d.ts +0 -29
  73. package/src/definition/model/properties/property-validator/property-validator-registry.js +0 -76
  74. package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +0 -132
  75. package/src/definition/model/properties/property-validator/property-validator.d.ts +0 -25
  76. package/src/definition/model/properties/property-validator/property-validator.js +0 -1
  77. package/src/utils/get-ctor-name.d.ts +0 -6
  78. package/src/utils/get-ctor-name.js +0 -11
  79. package/src/utils/get-ctor-name.spec.js +0 -17
  80. package/src/utils/get-decorator-target-type.d.ts +0 -27
  81. package/src/utils/get-decorator-target-type.js +0 -63
  82. package/src/utils/get-decorator-target-type.spec.js +0 -80
  83. package/src/utils/is-ctor.d.ts +0 -8
  84. package/src/utils/is-ctor.js +0 -11
  85. package/src/utils/is-ctor.spec.js +0 -26
  86. package/src/utils/is-pure-object.d.ts +0 -6
  87. package/src/utils/is-pure-object.spec.js +0 -25
  88. package/src/utils/transform-promise.d.ts +0 -13
  89. package/src/utils/transform-promise.js +0 -15
  90. package/src/utils/transform-promise.spec.js +0 -19
@@ -1,4528 +0,0 @@
1
- import {expect} from 'chai';
2
- import {format} from '@e22m4u/js-format';
3
- import {DataType} from './properties/index.js';
4
- import {DatabaseSchema} from '../../database-schema.js';
5
- import {EmptyValuesService} from '@e22m4u/js-empty-values';
6
- import {ModelDataValidator} from './model-data-validator.js';
7
- import {DefinitionRegistry} from '../definition-registry.js';
8
- import {PropertyValidatorRegistry} from './properties/index.js';
9
-
10
- describe('ModelDataValidator', function () {
11
- describe('validate', function () {
12
- it('does not throw an error if a model does not have a property of a given data', function () {
13
- const dbs = new DatabaseSchema();
14
- dbs.defineModel({name: 'model'});
15
- dbs.getService(ModelDataValidator).validate('model', {foo: 'bar'});
16
- });
17
-
18
- it('throws an error if a given data is not a pure object', function () {
19
- const throwable = modelData => () => {
20
- const dbs = new DatabaseSchema();
21
- dbs.defineModel({
22
- name: 'model',
23
- datasource: 'datasource',
24
- });
25
- dbs.getService(ModelDataValidator).validate('model', modelData);
26
- };
27
- const error = v =>
28
- format(
29
- 'The data of the model "model" should be an Object, but %s was given.',
30
- v,
31
- );
32
- expect(throwable('str')).to.throw(error('"str"'));
33
- expect(throwable(10)).to.throw(error('10'));
34
- expect(throwable(true)).to.throw(error('true'));
35
- expect(throwable(false)).to.throw(error('false'));
36
- expect(throwable([])).to.throw(error('Array'));
37
- expect(throwable(null)).to.throw(error('null'));
38
- expect(throwable(undefined)).to.throw(error('undefined'));
39
- });
40
-
41
- it('uses a base model hierarchy to validate a given data', function () {
42
- const dbs = new DatabaseSchema();
43
- dbs.defineModel({
44
- name: 'modelA',
45
- properties: {
46
- foo: DataType.STRING,
47
- },
48
- });
49
- dbs.defineModel({
50
- name: 'modelB',
51
- base: 'modelA',
52
- });
53
- const throwable = () =>
54
- dbs.getService(ModelDataValidator).validate('modelB', {foo: 10});
55
- expect(throwable).to.throw(
56
- 'The property "foo" of the model "modelB" must ' +
57
- 'have a String, but Number was given.',
58
- );
59
- });
60
-
61
- it('throws an error if a given data does not have a required property', function () {
62
- const dbs = new DatabaseSchema();
63
- dbs.defineModel({
64
- name: 'model',
65
- properties: {
66
- foo: {
67
- type: DataType.STRING,
68
- required: true,
69
- },
70
- },
71
- });
72
- const throwable = () =>
73
- dbs.getService(ModelDataValidator).validate('model', {});
74
- expect(throwable).to.throw(
75
- 'The property "foo" of the model "model" ' +
76
- 'is required, but undefined was given.',
77
- );
78
- });
79
-
80
- it('throws an error if a required property is undefined', function () {
81
- const dbs = new DatabaseSchema();
82
- dbs.defineModel({
83
- name: 'model',
84
- properties: {
85
- foo: {
86
- type: DataType.STRING,
87
- required: true,
88
- },
89
- },
90
- });
91
- const throwable = () =>
92
- dbs.getService(ModelDataValidator).validate('model', {foo: undefined});
93
- expect(throwable).to.throw(
94
- 'The property "foo" of the model "model" is required, but undefined was given.',
95
- );
96
- });
97
-
98
- it('throws an error if a required property is null', function () {
99
- const dbs = new DatabaseSchema();
100
- dbs.defineModel({
101
- name: 'model',
102
- properties: {
103
- foo: {
104
- type: DataType.STRING,
105
- required: true,
106
- },
107
- },
108
- });
109
- const throwable = () =>
110
- dbs.getService(ModelDataValidator).validate('model', {foo: null});
111
- expect(throwable).to.throw(
112
- 'The property "foo" of the model "model" is required, but null was given.',
113
- );
114
- });
115
-
116
- it('throws an error if a required property has an empty value', function () {
117
- const dbs = new DatabaseSchema();
118
- dbs.defineModel({
119
- name: 'model',
120
- properties: {
121
- foo: {
122
- type: DataType.STRING,
123
- required: true,
124
- },
125
- },
126
- });
127
- dbs
128
- .getService(EmptyValuesService)
129
- .setEmptyValuesOf(DataType.STRING, ['empty']);
130
- const throwable = () =>
131
- dbs.getService(ModelDataValidator).validate('model', {foo: 'empty'});
132
- expect(throwable).to.throw(
133
- 'The property "foo" of the model "model" ' +
134
- 'is required, but "empty" was given.',
135
- );
136
- });
137
-
138
- it('should validate property type before passing value through property validators', function () {
139
- const dbs = new DatabaseSchema();
140
- dbs.defineModel({
141
- name: 'model',
142
- properties: {
143
- foo: {
144
- type: DataType.STRING,
145
- validate: () => false,
146
- },
147
- },
148
- });
149
- const typeCheckFail = () =>
150
- dbs.getService(ModelDataValidator).validate('model', {foo: 10});
151
- expect(typeCheckFail).to.throw(
152
- 'The property "foo" of the model "model" must have ' +
153
- 'a String, but Number was given.',
154
- );
155
- const validatorCheckFail = () =>
156
- dbs.getService(ModelDataValidator).validate('model', {foo: 'test'});
157
- expect(validatorCheckFail).to.throw(
158
- 'The property "foo" of the model "model" has the invalid value "test" ' +
159
- 'that caught by a property validator.',
160
- );
161
- });
162
-
163
- describe('an option "isPartial" is true', function () {
164
- it('does not throw an error if a given data does not have a required property', function () {
165
- const dbs = new DatabaseSchema();
166
- dbs.defineModel({
167
- name: 'model',
168
- properties: {
169
- foo: {
170
- type: DataType.STRING,
171
- required: true,
172
- },
173
- },
174
- });
175
- dbs.getService(ModelDataValidator).validate('model', {}, true);
176
- });
177
-
178
- it('throws an error if a required property is undefined', function () {
179
- const dbs = new DatabaseSchema();
180
- dbs.defineModel({
181
- name: 'model',
182
- properties: {
183
- foo: {
184
- type: DataType.STRING,
185
- required: true,
186
- },
187
- },
188
- });
189
- const throwable = () =>
190
- dbs
191
- .getService(ModelDataValidator)
192
- .validate('model', {foo: undefined}, true);
193
- expect(throwable).to.throw(
194
- 'The property "foo" of the model "model" ' +
195
- 'is required, but undefined was given.',
196
- );
197
- });
198
-
199
- it('throws an error if a required property is null', function () {
200
- const dbs = new DatabaseSchema();
201
- dbs.defineModel({
202
- name: 'model',
203
- properties: {
204
- foo: {
205
- type: DataType.STRING,
206
- required: true,
207
- },
208
- },
209
- });
210
- const throwable = () =>
211
- dbs
212
- .getService(ModelDataValidator)
213
- .validate('model', {foo: null}, true);
214
- expect(throwable).to.throw(
215
- 'The property "foo" of the model "model" is required, but null was given.',
216
- );
217
- });
218
-
219
- it('throws an error if a required property has an empty value', function () {
220
- const dbs = new DatabaseSchema();
221
- dbs.defineModel({
222
- name: 'model',
223
- properties: {
224
- foo: {
225
- type: DataType.STRING,
226
- required: true,
227
- },
228
- },
229
- });
230
- dbs
231
- .getService(EmptyValuesService)
232
- .setEmptyValuesOf(DataType.STRING, [5]);
233
- const throwable = () =>
234
- dbs.getService(ModelDataValidator).validate('model', {foo: 5}, true);
235
- expect(throwable).to.throw(
236
- 'The property "foo" of the model "model" is required, but 5 was given.',
237
- );
238
- });
239
- });
240
-
241
- describe('validate by property type', function () {
242
- it('should not validate the empty value', function () {
243
- const dbs = new DatabaseSchema();
244
- dbs.defineModel({
245
- name: 'model',
246
- datasource: 'datasource',
247
- properties: {
248
- foo: {
249
- type: DataType.STRING,
250
- },
251
- },
252
- });
253
- dbs
254
- .getService(EmptyValuesService)
255
- .setEmptyValuesOf(DataType.STRING, [5]);
256
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
257
- });
258
-
259
- describe('DataType.ANY', function () {
260
- describe('ShortPropertyDefinition', function () {
261
- it('does not throw an error if an undefined given', function () {
262
- const dbs = new DatabaseSchema();
263
- dbs.defineModel({
264
- name: 'model',
265
- datasource: 'datasource',
266
- properties: {
267
- foo: DataType.ANY,
268
- },
269
- });
270
- dbs.getService(ModelDataValidator).validate('model', {
271
- foo: undefined,
272
- });
273
- });
274
-
275
- it('does not throw an error if a null given', function () {
276
- const dbs = new DatabaseSchema();
277
- dbs.defineModel({
278
- name: 'model',
279
- datasource: 'datasource',
280
- properties: {
281
- foo: DataType.ANY,
282
- },
283
- });
284
- dbs.getService(ModelDataValidator).validate('model', {
285
- foo: null,
286
- });
287
- });
288
-
289
- it('does not throw an error if a string given', function () {
290
- const dbs = new DatabaseSchema();
291
- dbs.defineModel({
292
- name: 'model',
293
- datasource: 'datasource',
294
- properties: {
295
- foo: DataType.ANY,
296
- },
297
- });
298
- dbs.getService(ModelDataValidator).validate('model', {
299
- foo: 'bar',
300
- });
301
- });
302
-
303
- it('does not throw an error if a number given', function () {
304
- const dbs = new DatabaseSchema();
305
- dbs.defineModel({
306
- name: 'model',
307
- datasource: 'datasource',
308
- properties: {
309
- foo: DataType.ANY,
310
- },
311
- });
312
- dbs.getService(ModelDataValidator).validate('model', {
313
- foo: 10,
314
- });
315
- });
316
-
317
- it('does not throw an error if true given', function () {
318
- const dbs = new DatabaseSchema();
319
- dbs.defineModel({
320
- name: 'model',
321
- datasource: 'datasource',
322
- properties: {
323
- foo: DataType.ANY,
324
- },
325
- });
326
- dbs.getService(ModelDataValidator).validate('model', {
327
- foo: true,
328
- });
329
- });
330
-
331
- it('does not throw an error if false given', function () {
332
- const dbs = new DatabaseSchema();
333
- dbs.defineModel({
334
- name: 'model',
335
- datasource: 'datasource',
336
- properties: {
337
- foo: DataType.ANY,
338
- },
339
- });
340
- dbs.getService(ModelDataValidator).validate('model', {
341
- foo: false,
342
- });
343
- });
344
-
345
- it('does not throw an error if an array given', function () {
346
- const dbs = new DatabaseSchema();
347
- dbs.defineModel({
348
- name: 'model',
349
- datasource: 'datasource',
350
- properties: {
351
- foo: DataType.ANY,
352
- },
353
- });
354
- dbs.getService(ModelDataValidator).validate('model', {
355
- foo: [],
356
- });
357
- });
358
-
359
- it('does not throw an error if an object given', function () {
360
- const dbs = new DatabaseSchema();
361
- dbs.defineModel({
362
- name: 'model',
363
- datasource: 'datasource',
364
- properties: {
365
- foo: DataType.ANY,
366
- },
367
- });
368
- dbs.getService(ModelDataValidator).validate('model', {
369
- foo: {},
370
- });
371
- });
372
- });
373
-
374
- describe('FullPropertyDefinition', function () {
375
- it('does not throw an error if an undefined given', function () {
376
- const dbs = new DatabaseSchema();
377
- dbs.defineModel({
378
- name: 'model',
379
- datasource: 'datasource',
380
- properties: {
381
- foo: {
382
- type: DataType.ANY,
383
- },
384
- },
385
- });
386
- dbs.getService(ModelDataValidator).validate('model', {
387
- foo: undefined,
388
- });
389
- });
390
-
391
- it('does not throw an error if a null given', function () {
392
- const dbs = new DatabaseSchema();
393
- dbs.defineModel({
394
- name: 'model',
395
- datasource: 'datasource',
396
- properties: {
397
- foo: {
398
- type: DataType.ANY,
399
- },
400
- },
401
- });
402
- dbs.getService(ModelDataValidator).validate('model', {
403
- foo: null,
404
- });
405
- });
406
-
407
- it('does not throw an error if a string given', function () {
408
- const dbs = new DatabaseSchema();
409
- dbs.defineModel({
410
- name: 'model',
411
- datasource: 'datasource',
412
- properties: {
413
- foo: {
414
- type: DataType.ANY,
415
- },
416
- },
417
- });
418
- dbs.getService(ModelDataValidator).validate('model', {
419
- foo: 'bar',
420
- });
421
- });
422
-
423
- it('does not throw an error if a number given', function () {
424
- const dbs = new DatabaseSchema();
425
- dbs.defineModel({
426
- name: 'model',
427
- datasource: 'datasource',
428
- properties: {
429
- foo: {
430
- type: DataType.ANY,
431
- },
432
- },
433
- });
434
- dbs.getService(ModelDataValidator).validate('model', {
435
- foo: 10,
436
- });
437
- });
438
-
439
- it('does not throw an error if true given', function () {
440
- const dbs = new DatabaseSchema();
441
- dbs.defineModel({
442
- name: 'model',
443
- datasource: 'datasource',
444
- properties: {
445
- foo: {
446
- type: DataType.ANY,
447
- },
448
- },
449
- });
450
- dbs.getService(ModelDataValidator).validate('model', {
451
- foo: true,
452
- });
453
- });
454
-
455
- it('does not throw an error if false given', function () {
456
- const dbs = new DatabaseSchema();
457
- dbs.defineModel({
458
- name: 'model',
459
- datasource: 'datasource',
460
- properties: {
461
- foo: {
462
- type: DataType.ANY,
463
- },
464
- },
465
- });
466
- dbs.getService(ModelDataValidator).validate('model', {
467
- foo: false,
468
- });
469
- });
470
-
471
- it('does not throw an error if an array given', function () {
472
- const dbs = new DatabaseSchema();
473
- dbs.defineModel({
474
- name: 'model',
475
- datasource: 'datasource',
476
- properties: {
477
- foo: {
478
- type: DataType.ANY,
479
- },
480
- },
481
- });
482
- dbs.getService(ModelDataValidator).validate('model', {
483
- foo: [],
484
- });
485
- });
486
-
487
- it('does not throw an error if an object given', function () {
488
- const dbs = new DatabaseSchema();
489
- dbs.defineModel({
490
- name: 'model',
491
- datasource: 'datasource',
492
- properties: {
493
- foo: {
494
- type: DataType.ANY,
495
- },
496
- },
497
- });
498
- dbs.getService(ModelDataValidator).validate('model', {
499
- foo: {},
500
- });
501
- });
502
- });
503
- });
504
-
505
- describe('DataType.STRING', function () {
506
- describe('ShortPropertyDefinition', function () {
507
- it('does not throw an error if an undefined given', function () {
508
- const dbs = new DatabaseSchema();
509
- dbs.defineModel({
510
- name: 'model',
511
- datasource: 'datasource',
512
- properties: {
513
- foo: DataType.STRING,
514
- },
515
- });
516
- dbs.getService(ModelDataValidator).validate('model', {
517
- foo: undefined,
518
- });
519
- });
520
-
521
- it('does not throw an error if a null given', function () {
522
- const dbs = new DatabaseSchema();
523
- dbs.defineModel({
524
- name: 'model',
525
- datasource: 'datasource',
526
- properties: {
527
- foo: DataType.STRING,
528
- },
529
- });
530
- dbs.getService(ModelDataValidator).validate('model', {
531
- foo: null,
532
- });
533
- });
534
-
535
- it('does not throw an error if a string given', function () {
536
- const dbs = new DatabaseSchema();
537
- dbs.defineModel({
538
- name: 'model',
539
- datasource: 'datasource',
540
- properties: {
541
- foo: DataType.STRING,
542
- },
543
- });
544
- dbs.getService(ModelDataValidator).validate('model', {
545
- foo: 'bar',
546
- });
547
- });
548
-
549
- it('throws an error if a number given', function () {
550
- const dbs = new DatabaseSchema();
551
- dbs.defineModel({
552
- name: 'model',
553
- datasource: 'datasource',
554
- properties: {
555
- foo: DataType.STRING,
556
- },
557
- });
558
- const throwable = () =>
559
- dbs.getService(ModelDataValidator).validate('model', {
560
- foo: 10,
561
- });
562
- expect(throwable).to.throw(
563
- 'The property "foo" of the model "model" must have ' +
564
- 'a String, but Number was given.',
565
- );
566
- });
567
-
568
- it('throws an error if true given', function () {
569
- const dbs = new DatabaseSchema();
570
- dbs.defineModel({
571
- name: 'model',
572
- datasource: 'datasource',
573
- properties: {
574
- foo: DataType.STRING,
575
- },
576
- });
577
- const throwable = () =>
578
- dbs.getService(ModelDataValidator).validate('model', {
579
- foo: true,
580
- });
581
- expect(throwable).to.throw(
582
- 'The property "foo" of the model "model" must have ' +
583
- 'a String, but Boolean was given.',
584
- );
585
- });
586
-
587
- it('throws an error if false given', function () {
588
- const dbs = new DatabaseSchema();
589
- dbs.defineModel({
590
- name: 'model',
591
- datasource: 'datasource',
592
- properties: {
593
- foo: DataType.STRING,
594
- },
595
- });
596
- const throwable = () =>
597
- dbs.getService(ModelDataValidator).validate('model', {
598
- foo: false,
599
- });
600
- expect(throwable).to.throw(
601
- 'The property "foo" of the model "model" must have ' +
602
- 'a String, but Boolean was given.',
603
- );
604
- });
605
-
606
- it('throws an error if an array given', function () {
607
- const dbs = new DatabaseSchema();
608
- dbs.defineModel({
609
- name: 'model',
610
- datasource: 'datasource',
611
- properties: {
612
- foo: DataType.STRING,
613
- },
614
- });
615
- const throwable = () =>
616
- dbs.getService(ModelDataValidator).validate('model', {
617
- foo: [],
618
- });
619
- expect(throwable).to.throw(
620
- 'The property "foo" of the model "model" must have ' +
621
- 'a String, but Array was given.',
622
- );
623
- });
624
-
625
- it('throws an error if an object given', function () {
626
- const dbs = new DatabaseSchema();
627
- dbs.defineModel({
628
- name: 'model',
629
- datasource: 'datasource',
630
- properties: {
631
- foo: DataType.STRING,
632
- },
633
- });
634
- const throwable = () =>
635
- dbs.getService(ModelDataValidator).validate('model', {
636
- foo: {},
637
- });
638
- expect(throwable).to.throw(
639
- 'The property "foo" of the model "model" must have ' +
640
- 'a String, but Object was given.',
641
- );
642
- });
643
- });
644
-
645
- describe('FullPropertyDefinition', function () {
646
- it('does not throw an error if an undefined given', function () {
647
- const dbs = new DatabaseSchema();
648
- dbs.defineModel({
649
- name: 'model',
650
- datasource: 'datasource',
651
- properties: {
652
- foo: {
653
- type: DataType.STRING,
654
- },
655
- },
656
- });
657
- dbs.getService(ModelDataValidator).validate('model', {
658
- foo: undefined,
659
- });
660
- });
661
-
662
- it('does not throw an error if a null given', function () {
663
- const dbs = new DatabaseSchema();
664
- dbs.defineModel({
665
- name: 'model',
666
- datasource: 'datasource',
667
- properties: {
668
- foo: {
669
- type: DataType.STRING,
670
- },
671
- },
672
- });
673
- dbs.getService(ModelDataValidator).validate('model', {
674
- foo: null,
675
- });
676
- });
677
-
678
- it('does not throw an error if a string given', function () {
679
- const dbs = new DatabaseSchema();
680
- dbs.defineModel({
681
- name: 'model',
682
- datasource: 'datasource',
683
- properties: {
684
- foo: {
685
- type: DataType.STRING,
686
- },
687
- },
688
- });
689
- dbs.getService(ModelDataValidator).validate('model', {
690
- foo: 'bar',
691
- });
692
- });
693
-
694
- it('throws an error if a number given', function () {
695
- const dbs = new DatabaseSchema();
696
- dbs.defineModel({
697
- name: 'model',
698
- datasource: 'datasource',
699
- properties: {
700
- foo: {
701
- type: DataType.STRING,
702
- },
703
- },
704
- });
705
- const throwable = () =>
706
- dbs.getService(ModelDataValidator).validate('model', {
707
- foo: 10,
708
- });
709
- expect(throwable).to.throw(
710
- 'The property "foo" of the model "model" must have ' +
711
- 'a String, but Number was given.',
712
- );
713
- });
714
-
715
- it('throws an error if true given', function () {
716
- const dbs = new DatabaseSchema();
717
- dbs.defineModel({
718
- name: 'model',
719
- datasource: 'datasource',
720
- properties: {
721
- foo: {
722
- type: DataType.STRING,
723
- },
724
- },
725
- });
726
- const throwable = () =>
727
- dbs.getService(ModelDataValidator).validate('model', {
728
- foo: true,
729
- });
730
- expect(throwable).to.throw(
731
- 'The property "foo" of the model "model" must have ' +
732
- 'a String, but Boolean was given.',
733
- );
734
- });
735
-
736
- it('throws an error if false given', function () {
737
- const dbs = new DatabaseSchema();
738
- dbs.defineModel({
739
- name: 'model',
740
- datasource: 'datasource',
741
- properties: {
742
- foo: {
743
- type: DataType.STRING,
744
- },
745
- },
746
- });
747
- const throwable = () =>
748
- dbs.getService(ModelDataValidator).validate('model', {
749
- foo: false,
750
- });
751
- expect(throwable).to.throw(
752
- 'The property "foo" of the model "model" must have ' +
753
- 'a String, but Boolean was given.',
754
- );
755
- });
756
-
757
- it('throws an error if an array given', function () {
758
- const dbs = new DatabaseSchema();
759
- dbs.defineModel({
760
- name: 'model',
761
- datasource: 'datasource',
762
- properties: {
763
- foo: {
764
- type: DataType.STRING,
765
- },
766
- },
767
- });
768
- const throwable = () =>
769
- dbs.getService(ModelDataValidator).validate('model', {
770
- foo: [],
771
- });
772
- expect(throwable).to.throw(
773
- 'The property "foo" of the model "model" must have ' +
774
- 'a String, but Array was given.',
775
- );
776
- });
777
-
778
- it('throws an error if an object given', function () {
779
- const dbs = new DatabaseSchema();
780
- dbs.defineModel({
781
- name: 'model',
782
- datasource: 'datasource',
783
- properties: {
784
- foo: {
785
- type: DataType.STRING,
786
- },
787
- },
788
- });
789
- const throwable = () =>
790
- dbs.getService(ModelDataValidator).validate('model', {
791
- foo: {},
792
- });
793
- expect(throwable).to.throw(
794
- 'The property "foo" of the model "model" must have ' +
795
- 'a String, but Object was given.',
796
- );
797
- });
798
- });
799
- });
800
-
801
- describe('DataType.NUMBER', function () {
802
- describe('ShortPropertyDefinition', function () {
803
- it('does not throw an error if an undefined given', function () {
804
- const dbs = new DatabaseSchema();
805
- dbs.defineModel({
806
- name: 'model',
807
- datasource: 'datasource',
808
- properties: {
809
- foo: DataType.NUMBER,
810
- },
811
- });
812
- dbs.getService(ModelDataValidator).validate('model', {
813
- foo: undefined,
814
- });
815
- });
816
-
817
- it('does not throw an error if a null given', function () {
818
- const dbs = new DatabaseSchema();
819
- dbs.defineModel({
820
- name: 'model',
821
- datasource: 'datasource',
822
- properties: {
823
- foo: DataType.NUMBER,
824
- },
825
- });
826
- dbs.getService(ModelDataValidator).validate('model', {
827
- foo: null,
828
- });
829
- });
830
-
831
- it('throws an error if a string given', function () {
832
- const dbs = new DatabaseSchema();
833
- dbs.defineModel({
834
- name: 'model',
835
- datasource: 'datasource',
836
- properties: {
837
- foo: DataType.NUMBER,
838
- },
839
- });
840
- const throwable = () =>
841
- dbs.getService(ModelDataValidator).validate('model', {
842
- foo: 'bar',
843
- });
844
- expect(throwable).to.throw(
845
- 'The property "foo" of the model "model" must have ' +
846
- 'a Number, but String was given.',
847
- );
848
- });
849
-
850
- it('does not throw an error if a number given', function () {
851
- const dbs = new DatabaseSchema();
852
- dbs.defineModel({
853
- name: 'model',
854
- datasource: 'datasource',
855
- properties: {
856
- foo: DataType.NUMBER,
857
- },
858
- });
859
- dbs.getService(ModelDataValidator).validate('model', {
860
- foo: 10,
861
- });
862
- });
863
-
864
- it('throws an error if true given', function () {
865
- const dbs = new DatabaseSchema();
866
- dbs.defineModel({
867
- name: 'model',
868
- datasource: 'datasource',
869
- properties: {
870
- foo: DataType.NUMBER,
871
- },
872
- });
873
- const throwable = () =>
874
- dbs.getService(ModelDataValidator).validate('model', {
875
- foo: true,
876
- });
877
- expect(throwable).to.throw(
878
- 'The property "foo" of the model "model" must have ' +
879
- 'a Number, but Boolean was given.',
880
- );
881
- });
882
-
883
- it('throws an error if false given', function () {
884
- const dbs = new DatabaseSchema();
885
- dbs.defineModel({
886
- name: 'model',
887
- datasource: 'datasource',
888
- properties: {
889
- foo: DataType.NUMBER,
890
- },
891
- });
892
- const throwable = () =>
893
- dbs.getService(ModelDataValidator).validate('model', {
894
- foo: false,
895
- });
896
- expect(throwable).to.throw(
897
- 'The property "foo" of the model "model" must have ' +
898
- 'a Number, but Boolean was given.',
899
- );
900
- });
901
-
902
- it('throws an error if an array given', function () {
903
- const dbs = new DatabaseSchema();
904
- dbs.defineModel({
905
- name: 'model',
906
- datasource: 'datasource',
907
- properties: {
908
- foo: DataType.NUMBER,
909
- },
910
- });
911
- const throwable = () =>
912
- dbs.getService(ModelDataValidator).validate('model', {
913
- foo: [],
914
- });
915
- expect(throwable).to.throw(
916
- 'The property "foo" of the model "model" must have ' +
917
- 'a Number, but Array was given.',
918
- );
919
- });
920
-
921
- it('throws an error if an object given', function () {
922
- const dbs = new DatabaseSchema();
923
- dbs.defineModel({
924
- name: 'model',
925
- datasource: 'datasource',
926
- properties: {
927
- foo: DataType.NUMBER,
928
- },
929
- });
930
- const throwable = () =>
931
- dbs.getService(ModelDataValidator).validate('model', {
932
- foo: {},
933
- });
934
- expect(throwable).to.throw(
935
- 'The property "foo" of the model "model" must have ' +
936
- 'a Number, but Object was given.',
937
- );
938
- });
939
- });
940
-
941
- describe('FullPropertyDefinition', function () {
942
- it('does not throw an error if an undefined given', function () {
943
- const dbs = new DatabaseSchema();
944
- dbs.defineModel({
945
- name: 'model',
946
- datasource: 'datasource',
947
- properties: {
948
- foo: {
949
- type: DataType.NUMBER,
950
- },
951
- },
952
- });
953
- dbs.getService(ModelDataValidator).validate('model', {
954
- foo: undefined,
955
- });
956
- });
957
-
958
- it('does not throw an error if a null given', function () {
959
- const dbs = new DatabaseSchema();
960
- dbs.defineModel({
961
- name: 'model',
962
- datasource: 'datasource',
963
- properties: {
964
- foo: {
965
- type: DataType.NUMBER,
966
- },
967
- },
968
- });
969
- dbs.getService(ModelDataValidator).validate('model', {
970
- foo: null,
971
- });
972
- });
973
-
974
- it('throws an error if a string given', function () {
975
- const dbs = new DatabaseSchema();
976
- dbs.defineModel({
977
- name: 'model',
978
- datasource: 'datasource',
979
- properties: {
980
- foo: {
981
- type: DataType.NUMBER,
982
- },
983
- },
984
- });
985
- const throwable = () =>
986
- dbs.getService(ModelDataValidator).validate('model', {
987
- foo: 'bar',
988
- });
989
- expect(throwable).to.throw(
990
- 'The property "foo" of the model "model" must have ' +
991
- 'a Number, but String was given.',
992
- );
993
- });
994
-
995
- it('does not throw an error if a number given', function () {
996
- const dbs = new DatabaseSchema();
997
- dbs.defineModel({
998
- name: 'model',
999
- datasource: 'datasource',
1000
- properties: {
1001
- foo: {
1002
- type: DataType.NUMBER,
1003
- },
1004
- },
1005
- });
1006
- dbs.getService(ModelDataValidator).validate('model', {
1007
- foo: 10,
1008
- });
1009
- });
1010
-
1011
- it('throws an error if true given', function () {
1012
- const dbs = new DatabaseSchema();
1013
- dbs.defineModel({
1014
- name: 'model',
1015
- datasource: 'datasource',
1016
- properties: {
1017
- foo: {
1018
- type: DataType.NUMBER,
1019
- },
1020
- },
1021
- });
1022
- const throwable = () =>
1023
- dbs.getService(ModelDataValidator).validate('model', {
1024
- foo: true,
1025
- });
1026
- expect(throwable).to.throw(
1027
- 'The property "foo" of the model "model" must have ' +
1028
- 'a Number, but Boolean was given.',
1029
- );
1030
- });
1031
-
1032
- it('throws an error if false given', function () {
1033
- const dbs = new DatabaseSchema();
1034
- dbs.defineModel({
1035
- name: 'model',
1036
- datasource: 'datasource',
1037
- properties: {
1038
- foo: {
1039
- type: DataType.NUMBER,
1040
- },
1041
- },
1042
- });
1043
- const throwable = () =>
1044
- dbs.getService(ModelDataValidator).validate('model', {
1045
- foo: false,
1046
- });
1047
- expect(throwable).to.throw(
1048
- 'The property "foo" of the model "model" must have ' +
1049
- 'a Number, but Boolean was given.',
1050
- );
1051
- });
1052
-
1053
- it('throws an error if an array given', function () {
1054
- const dbs = new DatabaseSchema();
1055
- dbs.defineModel({
1056
- name: 'model',
1057
- datasource: 'datasource',
1058
- properties: {
1059
- foo: {
1060
- type: DataType.NUMBER,
1061
- },
1062
- },
1063
- });
1064
- const throwable = () =>
1065
- dbs.getService(ModelDataValidator).validate('model', {
1066
- foo: [],
1067
- });
1068
- expect(throwable).to.throw(
1069
- 'The property "foo" of the model "model" must have ' +
1070
- 'a Number, but Array was given.',
1071
- );
1072
- });
1073
-
1074
- it('throws an error if an object given', function () {
1075
- const dbs = new DatabaseSchema();
1076
- dbs.defineModel({
1077
- name: 'model',
1078
- datasource: 'datasource',
1079
- properties: {
1080
- foo: {
1081
- type: DataType.NUMBER,
1082
- },
1083
- },
1084
- });
1085
- const throwable = () =>
1086
- dbs.getService(ModelDataValidator).validate('model', {
1087
- foo: {},
1088
- });
1089
- expect(throwable).to.throw(
1090
- 'The property "foo" of the model "model" must have ' +
1091
- 'a Number, but Object was given.',
1092
- );
1093
- });
1094
- });
1095
- });
1096
-
1097
- describe('DataType.BOOLEAN', function () {
1098
- describe('ShortPropertyDefinition', function () {
1099
- it('does not throw an error if an undefined given', function () {
1100
- const dbs = new DatabaseSchema();
1101
- dbs.defineModel({
1102
- name: 'model',
1103
- datasource: 'datasource',
1104
- properties: {
1105
- foo: DataType.BOOLEAN,
1106
- },
1107
- });
1108
- dbs.getService(ModelDataValidator).validate('model', {
1109
- foo: undefined,
1110
- });
1111
- });
1112
-
1113
- it('does not throw an error if a null given', function () {
1114
- const dbs = new DatabaseSchema();
1115
- dbs.defineModel({
1116
- name: 'model',
1117
- datasource: 'datasource',
1118
- properties: {
1119
- foo: DataType.BOOLEAN,
1120
- },
1121
- });
1122
- dbs.getService(ModelDataValidator).validate('model', {
1123
- foo: null,
1124
- });
1125
- });
1126
-
1127
- it('throws an error if a string given', function () {
1128
- const dbs = new DatabaseSchema();
1129
- dbs.defineModel({
1130
- name: 'model',
1131
- datasource: 'datasource',
1132
- properties: {
1133
- foo: DataType.BOOLEAN,
1134
- },
1135
- });
1136
- const throwable = () =>
1137
- dbs.getService(ModelDataValidator).validate('model', {
1138
- foo: 'bar',
1139
- });
1140
- expect(throwable).to.throw(
1141
- 'The property "foo" of the model "model" must have ' +
1142
- 'a Boolean, but String was given.',
1143
- );
1144
- });
1145
-
1146
- it('throws an error if a number given', function () {
1147
- const dbs = new DatabaseSchema();
1148
- dbs.defineModel({
1149
- name: 'model',
1150
- datasource: 'datasource',
1151
- properties: {
1152
- foo: DataType.BOOLEAN,
1153
- },
1154
- });
1155
- const throwable = () =>
1156
- dbs.getService(ModelDataValidator).validate('model', {
1157
- foo: 10,
1158
- });
1159
- expect(throwable).to.throw(
1160
- 'The property "foo" of the model "model" must have ' +
1161
- 'a Boolean, but Number was given.',
1162
- );
1163
- });
1164
-
1165
- it('does not throw an error if true given', function () {
1166
- const dbs = new DatabaseSchema();
1167
- dbs.defineModel({
1168
- name: 'model',
1169
- datasource: 'datasource',
1170
- properties: {
1171
- foo: DataType.BOOLEAN,
1172
- },
1173
- });
1174
- dbs.getService(ModelDataValidator).validate('model', {
1175
- foo: true,
1176
- });
1177
- });
1178
-
1179
- it('does not throw an error if false given', function () {
1180
- const dbs = new DatabaseSchema();
1181
- dbs.defineModel({
1182
- name: 'model',
1183
- datasource: 'datasource',
1184
- properties: {
1185
- foo: DataType.BOOLEAN,
1186
- },
1187
- });
1188
- dbs.getService(ModelDataValidator).validate('model', {
1189
- foo: false,
1190
- });
1191
- });
1192
-
1193
- it('throws an error if an array given', function () {
1194
- const dbs = new DatabaseSchema();
1195
- dbs.defineModel({
1196
- name: 'model',
1197
- datasource: 'datasource',
1198
- properties: {
1199
- foo: DataType.BOOLEAN,
1200
- },
1201
- });
1202
- const throwable = () =>
1203
- dbs.getService(ModelDataValidator).validate('model', {
1204
- foo: [],
1205
- });
1206
- expect(throwable).to.throw(
1207
- 'The property "foo" of the model "model" must have ' +
1208
- 'a Boolean, but Array was given.',
1209
- );
1210
- });
1211
-
1212
- it('throws an error if an object given', function () {
1213
- const dbs = new DatabaseSchema();
1214
- dbs.defineModel({
1215
- name: 'model',
1216
- datasource: 'datasource',
1217
- properties: {
1218
- foo: DataType.BOOLEAN,
1219
- },
1220
- });
1221
- const throwable = () =>
1222
- dbs.getService(ModelDataValidator).validate('model', {
1223
- foo: {},
1224
- });
1225
- expect(throwable).to.throw(
1226
- 'The property "foo" of the model "model" must have ' +
1227
- 'a Boolean, but Object was given.',
1228
- );
1229
- });
1230
- });
1231
-
1232
- describe('FullPropertyDefinition', function () {
1233
- it('does not throw an error if an undefined given', function () {
1234
- const dbs = new DatabaseSchema();
1235
- dbs.defineModel({
1236
- name: 'model',
1237
- datasource: 'datasource',
1238
- properties: {
1239
- foo: {
1240
- type: DataType.BOOLEAN,
1241
- },
1242
- },
1243
- });
1244
- dbs.getService(ModelDataValidator).validate('model', {
1245
- foo: undefined,
1246
- });
1247
- });
1248
-
1249
- it('does not throw an error if a null given', function () {
1250
- const dbs = new DatabaseSchema();
1251
- dbs.defineModel({
1252
- name: 'model',
1253
- datasource: 'datasource',
1254
- properties: {
1255
- foo: {
1256
- type: DataType.BOOLEAN,
1257
- },
1258
- },
1259
- });
1260
- dbs.getService(ModelDataValidator).validate('model', {
1261
- foo: null,
1262
- });
1263
- });
1264
-
1265
- it('throws an error if a string given', function () {
1266
- const dbs = new DatabaseSchema();
1267
- dbs.defineModel({
1268
- name: 'model',
1269
- datasource: 'datasource',
1270
- properties: {
1271
- foo: {
1272
- type: DataType.BOOLEAN,
1273
- },
1274
- },
1275
- });
1276
- const throwable = () =>
1277
- dbs.getService(ModelDataValidator).validate('model', {
1278
- foo: 'bar',
1279
- });
1280
- expect(throwable).to.throw(
1281
- 'The property "foo" of the model "model" must have ' +
1282
- 'a Boolean, but String was given.',
1283
- );
1284
- });
1285
-
1286
- it('throws an error if a number given', function () {
1287
- const dbs = new DatabaseSchema();
1288
- dbs.defineModel({
1289
- name: 'model',
1290
- datasource: 'datasource',
1291
- properties: {
1292
- foo: {
1293
- type: DataType.BOOLEAN,
1294
- },
1295
- },
1296
- });
1297
- const throwable = () =>
1298
- dbs.getService(ModelDataValidator).validate('model', {
1299
- foo: 10,
1300
- });
1301
- expect(throwable).to.throw(
1302
- 'The property "foo" of the model "model" must have ' +
1303
- 'a Boolean, but Number was given.',
1304
- );
1305
- });
1306
-
1307
- it('does not throw an error if true given', function () {
1308
- const dbs = new DatabaseSchema();
1309
- dbs.defineModel({
1310
- name: 'model',
1311
- datasource: 'datasource',
1312
- properties: {
1313
- foo: {
1314
- type: DataType.BOOLEAN,
1315
- },
1316
- },
1317
- });
1318
- dbs.getService(ModelDataValidator).validate('model', {
1319
- foo: true,
1320
- });
1321
- });
1322
-
1323
- it('does not throw an error if false given', function () {
1324
- const dbs = new DatabaseSchema();
1325
- dbs.defineModel({
1326
- name: 'model',
1327
- datasource: 'datasource',
1328
- properties: {
1329
- foo: {
1330
- type: DataType.BOOLEAN,
1331
- },
1332
- },
1333
- });
1334
- dbs.getService(ModelDataValidator).validate('model', {
1335
- foo: false,
1336
- });
1337
- });
1338
-
1339
- it('throws an error if an array given', function () {
1340
- const dbs = new DatabaseSchema();
1341
- dbs.defineModel({
1342
- name: 'model',
1343
- datasource: 'datasource',
1344
- properties: {
1345
- foo: {
1346
- type: DataType.BOOLEAN,
1347
- },
1348
- },
1349
- });
1350
- const throwable = () =>
1351
- dbs.getService(ModelDataValidator).validate('model', {
1352
- foo: [],
1353
- });
1354
- expect(throwable).to.throw(
1355
- 'The property "foo" of the model "model" must have ' +
1356
- 'a Boolean, but Array was given.',
1357
- );
1358
- });
1359
-
1360
- it('throws an error if an object given', function () {
1361
- const dbs = new DatabaseSchema();
1362
- dbs.defineModel({
1363
- name: 'model',
1364
- datasource: 'datasource',
1365
- properties: {
1366
- foo: {
1367
- type: DataType.BOOLEAN,
1368
- },
1369
- },
1370
- });
1371
- const throwable = () =>
1372
- dbs.getService(ModelDataValidator).validate('model', {
1373
- foo: {},
1374
- });
1375
- expect(throwable).to.throw(
1376
- 'The property "foo" of the model "model" must have ' +
1377
- 'a Boolean, but Object was given.',
1378
- );
1379
- });
1380
- });
1381
- });
1382
-
1383
- describe('DataType.ARRAY', function () {
1384
- describe('ShortPropertyDefinition', function () {
1385
- it('does not throw an error if an undefined given', function () {
1386
- const dbs = new DatabaseSchema();
1387
- dbs.defineModel({
1388
- name: 'model',
1389
- datasource: 'datasource',
1390
- properties: {
1391
- foo: DataType.ARRAY,
1392
- },
1393
- });
1394
- dbs.getService(ModelDataValidator).validate('model', {
1395
- foo: undefined,
1396
- });
1397
- });
1398
-
1399
- it('does not throw an error if a null given', function () {
1400
- const dbs = new DatabaseSchema();
1401
- dbs.defineModel({
1402
- name: 'model',
1403
- datasource: 'datasource',
1404
- properties: {
1405
- foo: DataType.ARRAY,
1406
- },
1407
- });
1408
- dbs.getService(ModelDataValidator).validate('model', {
1409
- foo: null,
1410
- });
1411
- });
1412
-
1413
- it('throws an error if a string given', function () {
1414
- const dbs = new DatabaseSchema();
1415
- dbs.defineModel({
1416
- name: 'model',
1417
- datasource: 'datasource',
1418
- properties: {
1419
- foo: DataType.ARRAY,
1420
- },
1421
- });
1422
- const throwable = () =>
1423
- dbs.getService(ModelDataValidator).validate('model', {
1424
- foo: 'bar',
1425
- });
1426
- expect(throwable).to.throw(
1427
- 'The property "foo" of the model "model" must have ' +
1428
- 'an Array, but String was given.',
1429
- );
1430
- });
1431
-
1432
- it('throws an error if a number given', function () {
1433
- const dbs = new DatabaseSchema();
1434
- dbs.defineModel({
1435
- name: 'model',
1436
- datasource: 'datasource',
1437
- properties: {
1438
- foo: DataType.ARRAY,
1439
- },
1440
- });
1441
- const throwable = () =>
1442
- dbs.getService(ModelDataValidator).validate('model', {
1443
- foo: 10,
1444
- });
1445
- expect(throwable).to.throw(
1446
- 'The property "foo" of the model "model" must have ' +
1447
- 'an Array, but Number was given.',
1448
- );
1449
- });
1450
-
1451
- it('throws an error if true given', function () {
1452
- const dbs = new DatabaseSchema();
1453
- dbs.defineModel({
1454
- name: 'model',
1455
- datasource: 'datasource',
1456
- properties: {
1457
- foo: DataType.ARRAY,
1458
- },
1459
- });
1460
- const throwable = () =>
1461
- dbs.getService(ModelDataValidator).validate('model', {
1462
- foo: true,
1463
- });
1464
- expect(throwable).to.throw(
1465
- 'The property "foo" of the model "model" must have ' +
1466
- 'an Array, but Boolean was given.',
1467
- );
1468
- });
1469
-
1470
- it('throws an error if false given', function () {
1471
- const dbs = new DatabaseSchema();
1472
- dbs.defineModel({
1473
- name: 'model',
1474
- datasource: 'datasource',
1475
- properties: {
1476
- foo: DataType.ARRAY,
1477
- },
1478
- });
1479
- const throwable = () =>
1480
- dbs.getService(ModelDataValidator).validate('model', {
1481
- foo: false,
1482
- });
1483
- expect(throwable).to.throw(
1484
- 'The property "foo" of the model "model" must have ' +
1485
- 'an Array, but Boolean was given.',
1486
- );
1487
- });
1488
-
1489
- it('does not throw an error if an array given', function () {
1490
- const dbs = new DatabaseSchema();
1491
- dbs.defineModel({
1492
- name: 'model',
1493
- datasource: 'datasource',
1494
- properties: {
1495
- foo: DataType.ARRAY,
1496
- },
1497
- });
1498
- dbs.getService(ModelDataValidator).validate('model', {
1499
- foo: [],
1500
- });
1501
- });
1502
-
1503
- it('throws an error if an object given', function () {
1504
- const dbs = new DatabaseSchema();
1505
- dbs.defineModel({
1506
- name: 'model',
1507
- datasource: 'datasource',
1508
- properties: {
1509
- foo: DataType.ARRAY,
1510
- },
1511
- });
1512
- const throwable = () =>
1513
- dbs.getService(ModelDataValidator).validate('model', {
1514
- foo: {},
1515
- });
1516
- expect(throwable).to.throw(
1517
- 'The property "foo" of the model "model" must have ' +
1518
- 'an Array, but Object was given.',
1519
- );
1520
- });
1521
- });
1522
-
1523
- describe('FullPropertyDefinition', function () {
1524
- it('does not throw an error if an undefined given', function () {
1525
- const dbs = new DatabaseSchema();
1526
- dbs.defineModel({
1527
- name: 'model',
1528
- datasource: 'datasource',
1529
- properties: {
1530
- foo: {
1531
- type: DataType.ARRAY,
1532
- },
1533
- },
1534
- });
1535
- dbs.getService(ModelDataValidator).validate('model', {
1536
- foo: undefined,
1537
- });
1538
- });
1539
-
1540
- it('does not throw an error if a null given', function () {
1541
- const dbs = new DatabaseSchema();
1542
- dbs.defineModel({
1543
- name: 'model',
1544
- datasource: 'datasource',
1545
- properties: {
1546
- foo: {
1547
- type: DataType.ARRAY,
1548
- },
1549
- },
1550
- });
1551
- dbs.getService(ModelDataValidator).validate('model', {
1552
- foo: null,
1553
- });
1554
- });
1555
-
1556
- it('throws an error if a string given', function () {
1557
- const dbs = new DatabaseSchema();
1558
- dbs.defineModel({
1559
- name: 'model',
1560
- datasource: 'datasource',
1561
- properties: {
1562
- foo: {
1563
- type: DataType.ARRAY,
1564
- },
1565
- },
1566
- });
1567
- const throwable = () =>
1568
- dbs.getService(ModelDataValidator).validate('model', {
1569
- foo: 'bar',
1570
- });
1571
- expect(throwable).to.throw(
1572
- 'The property "foo" of the model "model" must have ' +
1573
- 'an Array, but String was given.',
1574
- );
1575
- });
1576
-
1577
- it('throws an error if a number given', function () {
1578
- const dbs = new DatabaseSchema();
1579
- dbs.defineModel({
1580
- name: 'model',
1581
- datasource: 'datasource',
1582
- properties: {
1583
- foo: {
1584
- type: DataType.ARRAY,
1585
- },
1586
- },
1587
- });
1588
- const throwable = () =>
1589
- dbs.getService(ModelDataValidator).validate('model', {
1590
- foo: 10,
1591
- });
1592
- expect(throwable).to.throw(
1593
- 'The property "foo" of the model "model" must have ' +
1594
- 'an Array, but Number was given.',
1595
- );
1596
- });
1597
-
1598
- it('throws an error if true given', function () {
1599
- const dbs = new DatabaseSchema();
1600
- dbs.defineModel({
1601
- name: 'model',
1602
- datasource: 'datasource',
1603
- properties: {
1604
- foo: {
1605
- type: DataType.ARRAY,
1606
- },
1607
- },
1608
- });
1609
- const throwable = () =>
1610
- dbs.getService(ModelDataValidator).validate('model', {
1611
- foo: true,
1612
- });
1613
- expect(throwable).to.throw(
1614
- 'The property "foo" of the model "model" must have ' +
1615
- 'an Array, but Boolean was given.',
1616
- );
1617
- });
1618
-
1619
- it('throws an error if false given', function () {
1620
- const dbs = new DatabaseSchema();
1621
- dbs.defineModel({
1622
- name: 'model',
1623
- datasource: 'datasource',
1624
- properties: {
1625
- foo: {
1626
- type: DataType.ARRAY,
1627
- },
1628
- },
1629
- });
1630
- const throwable = () =>
1631
- dbs.getService(ModelDataValidator).validate('model', {
1632
- foo: false,
1633
- });
1634
- expect(throwable).to.throw(
1635
- 'The property "foo" of the model "model" must have ' +
1636
- 'an Array, but Boolean was given.',
1637
- );
1638
- });
1639
-
1640
- it('does not throw an error if an array given', function () {
1641
- const dbs = new DatabaseSchema();
1642
- dbs.defineModel({
1643
- name: 'model',
1644
- datasource: 'datasource',
1645
- properties: {
1646
- foo: {
1647
- type: DataType.ARRAY,
1648
- },
1649
- },
1650
- });
1651
- dbs.getService(ModelDataValidator).validate('model', {
1652
- foo: [],
1653
- });
1654
- });
1655
-
1656
- it('throws an error if an object given', function () {
1657
- const dbs = new DatabaseSchema();
1658
- dbs.defineModel({
1659
- name: 'model',
1660
- datasource: 'datasource',
1661
- properties: {
1662
- foo: {
1663
- type: DataType.ARRAY,
1664
- },
1665
- },
1666
- });
1667
- const throwable = () =>
1668
- dbs.getService(ModelDataValidator).validate('model', {
1669
- foo: {},
1670
- });
1671
- expect(throwable).to.throw(
1672
- 'The property "foo" of the model "model" must have ' +
1673
- 'an Array, but Object was given.',
1674
- );
1675
- });
1676
-
1677
- describe('the "itemModel" option', function () {
1678
- it('does not throw an error if the option "itemModel" is not specified in case of Object item type', function () {
1679
- const dbs = new DatabaseSchema();
1680
- dbs.defineModel({
1681
- name: 'model',
1682
- properties: {
1683
- foo: {
1684
- type: DataType.ARRAY,
1685
- itemType: DataType.OBJECT,
1686
- },
1687
- },
1688
- });
1689
- const value = {foo: [{a: 1}, {b: 2}]};
1690
- dbs.getService(ModelDataValidator).validate('model', value);
1691
- });
1692
-
1693
- it('throws an error when the given object element has an invalid model', function () {
1694
- const dbs = new DatabaseSchema();
1695
- dbs.defineModel({
1696
- name: 'modelA',
1697
- properties: {
1698
- foo: DataType.STRING,
1699
- },
1700
- });
1701
- dbs.defineModel({
1702
- name: 'modelB',
1703
- datasource: 'datasource',
1704
- properties: {
1705
- bar: {
1706
- type: DataType.ARRAY,
1707
- itemType: DataType.OBJECT,
1708
- itemModel: 'modelA',
1709
- },
1710
- },
1711
- });
1712
- const throwable = () =>
1713
- dbs.getService(ModelDataValidator).validate('modelB', {
1714
- bar: [{foo: 10}],
1715
- });
1716
- expect(throwable).to.throw(
1717
- 'The property "foo" of the model "modelA" must have ' +
1718
- 'a String, but Number was given.',
1719
- );
1720
- });
1721
-
1722
- it('does not throw an error when the given object element has a valid model', function () {
1723
- const dbs = new DatabaseSchema();
1724
- dbs.defineModel({
1725
- name: 'modelA',
1726
- properties: {
1727
- foo: DataType.STRING,
1728
- },
1729
- });
1730
- dbs.defineModel({
1731
- name: 'modelB',
1732
- datasource: 'datasource',
1733
- properties: {
1734
- bar: {
1735
- type: DataType.ARRAY,
1736
- itemType: DataType.OBJECT,
1737
- itemModel: 'modelA',
1738
- },
1739
- },
1740
- });
1741
- dbs.getService(ModelDataValidator).validate('modelB', {
1742
- bar: [{foo: '10'}],
1743
- });
1744
- });
1745
- });
1746
- });
1747
- });
1748
-
1749
- describe('DataType.OBJECT', function () {
1750
- describe('ShortPropertyDefinition', function () {
1751
- it('does not throw an error if an undefined given', function () {
1752
- const dbs = new DatabaseSchema();
1753
- dbs.defineModel({
1754
- name: 'model',
1755
- datasource: 'datasource',
1756
- properties: {
1757
- foo: DataType.OBJECT,
1758
- },
1759
- });
1760
- dbs.getService(ModelDataValidator).validate('model', {
1761
- foo: undefined,
1762
- });
1763
- });
1764
-
1765
- it('does not throw an error if a null given', function () {
1766
- const dbs = new DatabaseSchema();
1767
- dbs.defineModel({
1768
- name: 'model',
1769
- datasource: 'datasource',
1770
- properties: {
1771
- foo: DataType.OBJECT,
1772
- },
1773
- });
1774
- dbs.getService(ModelDataValidator).validate('model', {
1775
- foo: null,
1776
- });
1777
- });
1778
-
1779
- it('throws an error if a string given', function () {
1780
- const dbs = new DatabaseSchema();
1781
- dbs.defineModel({
1782
- name: 'model',
1783
- datasource: 'datasource',
1784
- properties: {
1785
- foo: DataType.OBJECT,
1786
- },
1787
- });
1788
- const throwable = () =>
1789
- dbs.getService(ModelDataValidator).validate('model', {
1790
- foo: 'bar',
1791
- });
1792
- expect(throwable).to.throw(
1793
- 'The property "foo" of the model "model" must have ' +
1794
- 'an Object, but String was given.',
1795
- );
1796
- });
1797
-
1798
- it('throws an error if a number given', function () {
1799
- const dbs = new DatabaseSchema();
1800
- dbs.defineModel({
1801
- name: 'model',
1802
- datasource: 'datasource',
1803
- properties: {
1804
- foo: DataType.OBJECT,
1805
- },
1806
- });
1807
- const throwable = () =>
1808
- dbs.getService(ModelDataValidator).validate('model', {
1809
- foo: 10,
1810
- });
1811
- expect(throwable).to.throw(
1812
- 'The property "foo" of the model "model" must have ' +
1813
- 'an Object, but Number was given.',
1814
- );
1815
- });
1816
-
1817
- it('throws an error if true given', function () {
1818
- const dbs = new DatabaseSchema();
1819
- dbs.defineModel({
1820
- name: 'model',
1821
- datasource: 'datasource',
1822
- properties: {
1823
- foo: DataType.OBJECT,
1824
- },
1825
- });
1826
- const throwable = () =>
1827
- dbs.getService(ModelDataValidator).validate('model', {
1828
- foo: true,
1829
- });
1830
- expect(throwable).to.throw(
1831
- 'The property "foo" of the model "model" must have ' +
1832
- 'an Object, but Boolean was given.',
1833
- );
1834
- });
1835
-
1836
- it('throws an error if false given', function () {
1837
- const dbs = new DatabaseSchema();
1838
- dbs.defineModel({
1839
- name: 'model',
1840
- datasource: 'datasource',
1841
- properties: {
1842
- foo: DataType.OBJECT,
1843
- },
1844
- });
1845
- const throwable = () =>
1846
- dbs.getService(ModelDataValidator).validate('model', {
1847
- foo: false,
1848
- });
1849
- expect(throwable).to.throw(
1850
- 'The property "foo" of the model "model" must have ' +
1851
- 'an Object, but Boolean was given.',
1852
- );
1853
- });
1854
-
1855
- it('throws an error if an array given', function () {
1856
- const dbs = new DatabaseSchema();
1857
- dbs.defineModel({
1858
- name: 'model',
1859
- datasource: 'datasource',
1860
- properties: {
1861
- foo: DataType.OBJECT,
1862
- },
1863
- });
1864
- const throwable = () =>
1865
- dbs.getService(ModelDataValidator).validate('model', {
1866
- foo: [],
1867
- });
1868
- expect(throwable).to.throw(
1869
- 'The property "foo" of the model "model" must have ' +
1870
- 'an Object, but Array was given.',
1871
- );
1872
- });
1873
-
1874
- it('does not throw an error if an object given', function () {
1875
- const dbs = new DatabaseSchema();
1876
- dbs.defineModel({
1877
- name: 'model',
1878
- datasource: 'datasource',
1879
- properties: {
1880
- foo: DataType.OBJECT,
1881
- },
1882
- });
1883
- dbs.getService(ModelDataValidator).validate('model', {
1884
- foo: {},
1885
- });
1886
- });
1887
- });
1888
-
1889
- describe('FullPropertyDefinition', function () {
1890
- it('does not throw an error if an undefined given', function () {
1891
- const dbs = new DatabaseSchema();
1892
- dbs.defineModel({
1893
- name: 'model',
1894
- datasource: 'datasource',
1895
- properties: {
1896
- foo: {
1897
- type: DataType.OBJECT,
1898
- },
1899
- },
1900
- });
1901
- dbs.getService(ModelDataValidator).validate('model', {
1902
- foo: undefined,
1903
- });
1904
- });
1905
-
1906
- it('does not throw an error if a null given', function () {
1907
- const dbs = new DatabaseSchema();
1908
- dbs.defineModel({
1909
- name: 'model',
1910
- datasource: 'datasource',
1911
- properties: {
1912
- foo: {
1913
- type: DataType.OBJECT,
1914
- },
1915
- },
1916
- });
1917
- dbs.getService(ModelDataValidator).validate('model', {
1918
- foo: null,
1919
- });
1920
- });
1921
-
1922
- it('throws an error if a string given', function () {
1923
- const dbs = new DatabaseSchema();
1924
- dbs.defineModel({
1925
- name: 'model',
1926
- datasource: 'datasource',
1927
- properties: {
1928
- foo: {
1929
- type: DataType.OBJECT,
1930
- },
1931
- },
1932
- });
1933
- const throwable = () =>
1934
- dbs.getService(ModelDataValidator).validate('model', {
1935
- foo: 'bar',
1936
- });
1937
- expect(throwable).to.throw(
1938
- 'The property "foo" of the model "model" must have ' +
1939
- 'an Object, but String was given.',
1940
- );
1941
- });
1942
-
1943
- it('throws an error if a number given', function () {
1944
- const dbs = new DatabaseSchema();
1945
- dbs.defineModel({
1946
- name: 'model',
1947
- datasource: 'datasource',
1948
- properties: {
1949
- foo: {
1950
- type: DataType.OBJECT,
1951
- },
1952
- },
1953
- });
1954
- const throwable = () =>
1955
- dbs.getService(ModelDataValidator).validate('model', {
1956
- foo: 10,
1957
- });
1958
- expect(throwable).to.throw(
1959
- 'The property "foo" of the model "model" must have ' +
1960
- 'an Object, but Number was given.',
1961
- );
1962
- });
1963
-
1964
- it('throws an error if true given', function () {
1965
- const dbs = new DatabaseSchema();
1966
- dbs.defineModel({
1967
- name: 'model',
1968
- datasource: 'datasource',
1969
- properties: {
1970
- foo: {
1971
- type: DataType.OBJECT,
1972
- },
1973
- },
1974
- });
1975
- const throwable = () =>
1976
- dbs.getService(ModelDataValidator).validate('model', {
1977
- foo: true,
1978
- });
1979
- expect(throwable).to.throw(
1980
- 'The property "foo" of the model "model" must have ' +
1981
- 'an Object, but Boolean was given.',
1982
- );
1983
- });
1984
-
1985
- it('throws an error if false given', function () {
1986
- const dbs = new DatabaseSchema();
1987
- dbs.defineModel({
1988
- name: 'model',
1989
- datasource: 'datasource',
1990
- properties: {
1991
- foo: {
1992
- type: DataType.OBJECT,
1993
- },
1994
- },
1995
- });
1996
- const throwable = () =>
1997
- dbs.getService(ModelDataValidator).validate('model', {
1998
- foo: false,
1999
- });
2000
- expect(throwable).to.throw(
2001
- 'The property "foo" of the model "model" must have ' +
2002
- 'an Object, but Boolean was given.',
2003
- );
2004
- });
2005
-
2006
- it('throws an error if an array given', function () {
2007
- const dbs = new DatabaseSchema();
2008
- dbs.defineModel({
2009
- name: 'model',
2010
- datasource: 'datasource',
2011
- properties: {
2012
- foo: {
2013
- type: DataType.OBJECT,
2014
- },
2015
- },
2016
- });
2017
- const throwable = () =>
2018
- dbs.getService(ModelDataValidator).validate('model', {
2019
- foo: [],
2020
- });
2021
- expect(throwable).to.throw(
2022
- 'The property "foo" of the model "model" must have ' +
2023
- 'an Object, but Array was given.',
2024
- );
2025
- });
2026
-
2027
- it('does not throw an error if an object given', function () {
2028
- const dbs = new DatabaseSchema();
2029
- dbs.defineModel({
2030
- name: 'model',
2031
- datasource: 'datasource',
2032
- properties: {
2033
- foo: {
2034
- type: DataType.OBJECT,
2035
- },
2036
- },
2037
- });
2038
- dbs.getService(ModelDataValidator).validate('model', {
2039
- foo: {},
2040
- });
2041
- });
2042
-
2043
- describe('the "model" option', function () {
2044
- it('throws an error when the given object has an invalid model', function () {
2045
- const dbs = new DatabaseSchema();
2046
- dbs.defineModel({
2047
- name: 'modelA',
2048
- properties: {
2049
- foo: DataType.STRING,
2050
- },
2051
- });
2052
- dbs.defineModel({
2053
- name: 'modelB',
2054
- datasource: 'datasource',
2055
- properties: {
2056
- bar: {
2057
- type: DataType.OBJECT,
2058
- model: 'modelA',
2059
- },
2060
- },
2061
- });
2062
- const throwable = () =>
2063
- dbs.getService(ModelDataValidator).validate('modelB', {
2064
- bar: {foo: 10},
2065
- });
2066
- expect(throwable).to.throw(
2067
- 'The property "foo" of the model "modelA" must have ' +
2068
- 'a String, but Number was given.',
2069
- );
2070
- });
2071
-
2072
- it('does not throw an error when the given object has a valid model', function () {
2073
- const dbs = new DatabaseSchema();
2074
- dbs.defineModel({
2075
- name: 'modelA',
2076
- properties: {
2077
- foo: DataType.STRING,
2078
- },
2079
- });
2080
- dbs.defineModel({
2081
- name: 'modelB',
2082
- datasource: 'datasource',
2083
- properties: {
2084
- bar: {
2085
- type: DataType.OBJECT,
2086
- model: 'modelA',
2087
- },
2088
- },
2089
- });
2090
- dbs.getService(ModelDataValidator).validate('modelB', {
2091
- bar: {foo: '10'},
2092
- });
2093
- });
2094
- });
2095
- });
2096
- });
2097
- });
2098
-
2099
- describe('validate by property validators', function () {
2100
- describe('when the option "validate" is a String', function () {
2101
- it('should not validate the non-provided property', function () {
2102
- const dbs = new DatabaseSchema();
2103
- const reg = dbs.getService(PropertyValidatorRegistry);
2104
- reg.addValidator('myValidator', function () {
2105
- throw new Error('Should not to be called.');
2106
- });
2107
- dbs.defineModel({
2108
- name: 'model',
2109
- properties: {
2110
- foo: {
2111
- type: DataType.ANY,
2112
- validate: 'myValidator',
2113
- },
2114
- },
2115
- });
2116
- const validator = dbs.getService(ModelDataValidator);
2117
- validator.validate('model', {});
2118
- });
2119
-
2120
- it('should not validate undefined and null values', function () {
2121
- const dbs = new DatabaseSchema();
2122
- const reg = dbs.getService(PropertyValidatorRegistry);
2123
- reg.addValidator('myValidator', function () {
2124
- throw new Error('Should not to be called.');
2125
- });
2126
- dbs.defineModel({
2127
- name: 'model',
2128
- properties: {
2129
- foo: {
2130
- type: DataType.ANY,
2131
- validate: 'myValidator',
2132
- },
2133
- },
2134
- });
2135
- const validator = dbs.getService(ModelDataValidator);
2136
- validator.validate('model', {foo: undefined});
2137
- validator.validate('model', {foo: null});
2138
- });
2139
-
2140
- it('should not validate the empty value', function () {
2141
- const dbs = new DatabaseSchema();
2142
- const reg = dbs.getService(PropertyValidatorRegistry);
2143
- reg.addValidator('myValidator', function () {
2144
- throw new Error('Should not to be called.');
2145
- });
2146
- dbs.defineModel({
2147
- name: 'model',
2148
- properties: {
2149
- foo: {
2150
- type: DataType.STRING,
2151
- validate: 'myValidator',
2152
- },
2153
- },
2154
- });
2155
- dbs
2156
- .getService(EmptyValuesService)
2157
- .setEmptyValuesOf(DataType.STRING, [5]);
2158
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
2159
- });
2160
-
2161
- it('should throw the error for the non-existent validator name', function () {
2162
- const dbs = new DatabaseSchema();
2163
- const modelDef = {
2164
- name: 'model',
2165
- properties: {
2166
- foo: {
2167
- type: DataType.ANY,
2168
- validate: undefined,
2169
- },
2170
- },
2171
- };
2172
- dbs.defineModel(modelDef);
2173
- modelDef.properties.foo.validate = 'myValidator';
2174
- const throwable = () =>
2175
- dbs.getService(ModelDataValidator).validate('model', {
2176
- foo: 'test',
2177
- });
2178
- expect(throwable).to.throw(
2179
- 'The property validator "myValidator" is not defined.',
2180
- );
2181
- });
2182
-
2183
- it('should throw the error from the validator', function () {
2184
- const dbs = new DatabaseSchema();
2185
- const reg = dbs.getService(PropertyValidatorRegistry);
2186
- reg.addValidator('myValidator', function () {
2187
- throw Error('My error');
2188
- });
2189
- dbs.defineModel({
2190
- name: 'model',
2191
- properties: {
2192
- foo: {
2193
- type: DataType.ANY,
2194
- validate: 'myValidator',
2195
- },
2196
- },
2197
- });
2198
- const throwable = () =>
2199
- dbs.getService(ModelDataValidator).validate('model', {
2200
- foo: 'test',
2201
- });
2202
- expect(throwable).to.throw('My error');
2203
- });
2204
-
2205
- it('should allow the given value if the validator returns true', function () {
2206
- let called = 0;
2207
- const dbs = new DatabaseSchema();
2208
- const reg = dbs.getService(PropertyValidatorRegistry);
2209
- reg.addValidator('myValidator', function () {
2210
- called++;
2211
- return true;
2212
- });
2213
- dbs.defineModel({
2214
- name: 'model',
2215
- properties: {
2216
- foo: {
2217
- type: DataType.ANY,
2218
- validate: 'myValidator',
2219
- },
2220
- },
2221
- });
2222
- dbs.getService(ModelDataValidator).validate('model', {
2223
- foo: 'test',
2224
- });
2225
- expect(called).to.be.eq(1);
2226
- });
2227
-
2228
- it('should throw the error if the validator returns a promise', function () {
2229
- let called = 0;
2230
- const dbs = new DatabaseSchema();
2231
- const reg = dbs.getService(PropertyValidatorRegistry);
2232
- reg.addValidator('myValidator', function () {
2233
- called++;
2234
- return Promise.resolve(true);
2235
- });
2236
- dbs.defineModel({
2237
- name: 'model',
2238
- properties: {
2239
- foo: {
2240
- type: DataType.ANY,
2241
- validate: 'myValidator',
2242
- },
2243
- },
2244
- });
2245
- const throwable = () =>
2246
- dbs.getService(ModelDataValidator).validate('model', {
2247
- foo: 'test',
2248
- });
2249
- expect(throwable).to.throw(
2250
- 'Asynchronous property validators are not supported, ' +
2251
- 'but the property "foo" of the model "model" has the property ' +
2252
- 'validator "myValidator" that returns a Promise.',
2253
- );
2254
- expect(called).to.be.eq(1);
2255
- });
2256
-
2257
- it('should throw the error for a non-true result from the validator', function () {
2258
- const testFn = v => {
2259
- let called = 0;
2260
- const dbs = new DatabaseSchema();
2261
- const reg = dbs.getService(PropertyValidatorRegistry);
2262
- reg.addValidator('myValidator', function () {
2263
- called++;
2264
- return v;
2265
- });
2266
- dbs.defineModel({
2267
- name: 'model',
2268
- properties: {
2269
- foo: {
2270
- type: DataType.ANY,
2271
- validate: 'myValidator',
2272
- },
2273
- },
2274
- });
2275
- const throwable = () =>
2276
- dbs.getService(ModelDataValidator).validate('model', {
2277
- foo: 'test',
2278
- });
2279
- expect(throwable).to.throw(
2280
- 'The property "foo" of the model "model" has the invalid value "test" ' +
2281
- 'that caught by the property validator "myValidator".',
2282
- );
2283
- expect(called).to.be.eq(1);
2284
- };
2285
- testFn('str');
2286
- testFn('');
2287
- testFn(10);
2288
- testFn(0);
2289
- testFn(false);
2290
- testFn(undefined);
2291
- testFn(null);
2292
- testFn({});
2293
- testFn([]);
2294
- testFn(() => undefined);
2295
- });
2296
-
2297
- it('should pass arguments to the validator', function () {
2298
- let called = 0;
2299
- const dbs = new DatabaseSchema();
2300
- const reg = dbs.getService(PropertyValidatorRegistry);
2301
- reg.addValidator('myValidator', function (value, options, context) {
2302
- expect(value).to.be.eq('test');
2303
- expect(options).to.be.undefined;
2304
- expect(context).to.be.eql({
2305
- validatorName: 'myValidator',
2306
- modelName: 'model',
2307
- propName: 'foo',
2308
- });
2309
- called++;
2310
- return true;
2311
- });
2312
- dbs.defineModel({
2313
- name: 'model',
2314
- properties: {
2315
- foo: {
2316
- type: DataType.ANY,
2317
- validate: 'myValidator',
2318
- },
2319
- },
2320
- });
2321
- dbs.getService(ModelDataValidator).validate('model', {
2322
- foo: 'test',
2323
- });
2324
- expect(called).to.be.eq(1);
2325
- });
2326
-
2327
- it('should invoke the validator only once per value', function () {
2328
- let invoked = 0;
2329
- const dbs = new DatabaseSchema();
2330
- const reg = dbs.getService(PropertyValidatorRegistry);
2331
- reg.addValidator('myValidator', function () {
2332
- invoked++;
2333
- return true;
2334
- });
2335
- dbs.defineModel({
2336
- name: 'model',
2337
- properties: {
2338
- foo: {
2339
- type: DataType.ANY,
2340
- validate: 'myValidator',
2341
- },
2342
- },
2343
- });
2344
- dbs.getService(ModelDataValidator).validate('model', {
2345
- foo: 'test',
2346
- });
2347
- expect(invoked).to.be.eq(1);
2348
- });
2349
- });
2350
-
2351
- describe('when the option "validate" is a Function', function () {
2352
- describe('named validators', function () {
2353
- it('should not validate the non-provided property', function () {
2354
- const dbs = new DatabaseSchema();
2355
- const myValidator = function () {
2356
- throw new Error('Should not to be called.');
2357
- };
2358
- dbs.defineModel({
2359
- name: 'model',
2360
- properties: {
2361
- foo: {
2362
- type: DataType.ANY,
2363
- validate: myValidator,
2364
- },
2365
- },
2366
- });
2367
- const validator = dbs.getService(ModelDataValidator);
2368
- validator.validate('model', {});
2369
- });
2370
-
2371
- it('should not validate undefined and null values', function () {
2372
- const dbs = new DatabaseSchema();
2373
- const myValidator = () => {
2374
- throw new Error('Should not to be called.');
2375
- };
2376
- dbs.defineModel({
2377
- name: 'model',
2378
- properties: {
2379
- foo: {
2380
- type: DataType.ANY,
2381
- validate: myValidator,
2382
- },
2383
- },
2384
- });
2385
- const validator = dbs.getService(ModelDataValidator);
2386
- validator.validate('model', {foo: undefined});
2387
- validator.validate('model', {foo: null});
2388
- });
2389
-
2390
- it('should not validate the empty value', function () {
2391
- const dbs = new DatabaseSchema();
2392
- const myValidator = function () {
2393
- throw new Error('Should not to be called.');
2394
- };
2395
- dbs.defineModel({
2396
- name: 'model',
2397
- properties: {
2398
- foo: {
2399
- type: DataType.STRING,
2400
- validate: myValidator,
2401
- },
2402
- },
2403
- });
2404
- dbs
2405
- .getService(EmptyValuesService)
2406
- .setEmptyValuesOf(DataType.STRING, [5]);
2407
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
2408
- });
2409
-
2410
- it('should throw the error from the validator', function () {
2411
- const dbs = new DatabaseSchema();
2412
- const myValidator = function () {
2413
- throw Error('My error');
2414
- };
2415
- dbs.defineModel({
2416
- name: 'model',
2417
- properties: {
2418
- foo: {
2419
- type: DataType.ANY,
2420
- validate: myValidator,
2421
- },
2422
- },
2423
- });
2424
- const throwable = () =>
2425
- dbs.getService(ModelDataValidator).validate('model', {
2426
- foo: 'test',
2427
- });
2428
- expect(throwable).to.throw('My error');
2429
- });
2430
-
2431
- it('should allow the given value if the validator returns true', function () {
2432
- const dbs = new DatabaseSchema();
2433
- let called = 0;
2434
- const myValidator = function () {
2435
- called++;
2436
- return true;
2437
- };
2438
- dbs.defineModel({
2439
- name: 'model',
2440
- properties: {
2441
- foo: {
2442
- type: DataType.ANY,
2443
- validate: myValidator,
2444
- },
2445
- },
2446
- });
2447
- dbs.getService(ModelDataValidator).validate('model', {
2448
- foo: 'test',
2449
- });
2450
- expect(called).to.be.eq(1);
2451
- });
2452
-
2453
- it('should throw the error if the validator returns a promise', function () {
2454
- const dbs = new DatabaseSchema();
2455
- const myValidator = function () {
2456
- return Promise.resolve(true);
2457
- };
2458
- dbs.defineModel({
2459
- name: 'model',
2460
- properties: {
2461
- foo: {
2462
- type: DataType.ANY,
2463
- validate: myValidator,
2464
- },
2465
- },
2466
- });
2467
- const throwable = () =>
2468
- dbs.getService(ModelDataValidator).validate('model', {
2469
- foo: 'test',
2470
- });
2471
- expect(throwable).to.throw(
2472
- 'Asynchronous property validators are not supported, ' +
2473
- 'but the property "foo" of the model "model" has the property ' +
2474
- 'validator "myValidator" that returns a Promise.',
2475
- );
2476
- });
2477
-
2478
- it('should throw the error for a non-true result from the validator', function () {
2479
- const testFn = v => {
2480
- const dbs = new DatabaseSchema();
2481
- const myValidator = function () {
2482
- return v;
2483
- };
2484
- dbs.defineModel({
2485
- name: 'model',
2486
- properties: {
2487
- foo: {
2488
- type: DataType.ANY,
2489
- validate: myValidator,
2490
- },
2491
- },
2492
- });
2493
- const throwable = () =>
2494
- dbs.getService(ModelDataValidator).validate('model', {
2495
- foo: 'test',
2496
- });
2497
- expect(throwable).to.throw(
2498
- 'The property "foo" of the model "model" has the invalid value "test" ' +
2499
- 'that caught by the property validator "myValidator".',
2500
- );
2501
- };
2502
- testFn('str');
2503
- testFn('');
2504
- testFn(10);
2505
- testFn(0);
2506
- testFn(false);
2507
- testFn(undefined);
2508
- testFn(null);
2509
- testFn({});
2510
- testFn([]);
2511
- testFn(() => undefined);
2512
- });
2513
-
2514
- it('should pass arguments to the validator', function () {
2515
- let called = 0;
2516
- const dbs = new DatabaseSchema();
2517
- const myValidator = function (value, options, context) {
2518
- expect(value).to.be.eq('test');
2519
- expect(options).to.be.undefined;
2520
- expect(context).to.be.eql({
2521
- validatorName: 'myValidator',
2522
- modelName: 'model',
2523
- propName: 'foo',
2524
- });
2525
- called++;
2526
- return true;
2527
- };
2528
- dbs
2529
- .getService(PropertyValidatorRegistry)
2530
- .addValidator('myValidator', myValidator);
2531
- dbs.defineModel({
2532
- name: 'model',
2533
- properties: {
2534
- foo: {
2535
- type: DataType.ANY,
2536
- validate: 'myValidator',
2537
- },
2538
- },
2539
- });
2540
- dbs.getService(ModelDataValidator).validate('model', {
2541
- foo: 'test',
2542
- });
2543
- expect(called).to.be.eq(1);
2544
- });
2545
-
2546
- it('should invoke the validator only once per value', function () {
2547
- let invoked = 0;
2548
- const myValidator = function () {
2549
- invoked++;
2550
- return true;
2551
- };
2552
- const dbs = new DatabaseSchema();
2553
- dbs.defineModel({
2554
- name: 'model',
2555
- properties: {
2556
- foo: {
2557
- type: DataType.ANY,
2558
- validate: myValidator,
2559
- },
2560
- },
2561
- });
2562
- dbs.getService(ModelDataValidator).validate('model', {
2563
- foo: 'test',
2564
- });
2565
- expect(invoked).to.be.eq(1);
2566
- });
2567
- });
2568
-
2569
- describe('anonymous validators', function () {
2570
- it('should not validate the non-provided property', function () {
2571
- const dbs = new DatabaseSchema();
2572
- dbs.defineModel({
2573
- name: 'model',
2574
- properties: {
2575
- foo: {
2576
- type: DataType.ANY,
2577
- validate() {
2578
- throw new Error('Should not to be called.');
2579
- },
2580
- },
2581
- },
2582
- });
2583
- const validator = dbs.getService(ModelDataValidator);
2584
- validator.validate('model', {});
2585
- });
2586
-
2587
- it('should not validate undefined and null values', function () {
2588
- const dbs = new DatabaseSchema();
2589
- dbs.defineModel({
2590
- name: 'model',
2591
- properties: {
2592
- foo: {
2593
- type: DataType.ANY,
2594
- validate() {
2595
- throw new Error('Should not to be called.');
2596
- },
2597
- },
2598
- },
2599
- });
2600
- const validator = dbs.getService(ModelDataValidator);
2601
- validator.validate('model', {foo: undefined});
2602
- validator.validate('model', {foo: null});
2603
- });
2604
-
2605
- it('should not validate the empty value', function () {
2606
- const dbs = new DatabaseSchema();
2607
- dbs.defineModel({
2608
- name: 'model',
2609
- properties: {
2610
- foo: {
2611
- type: DataType.STRING,
2612
- validate() {
2613
- throw new Error('Should not to be called.');
2614
- },
2615
- },
2616
- },
2617
- });
2618
- dbs
2619
- .getService(EmptyValuesService)
2620
- .setEmptyValuesOf(DataType.STRING, [5]);
2621
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
2622
- });
2623
-
2624
- it('should throw the error from the validator', function () {
2625
- const dbs = new DatabaseSchema();
2626
- dbs.defineModel({
2627
- name: 'model',
2628
- properties: {
2629
- foo: {
2630
- type: DataType.ANY,
2631
- validate() {
2632
- throw Error('My error');
2633
- },
2634
- },
2635
- },
2636
- });
2637
- const throwable = () =>
2638
- dbs.getService(ModelDataValidator).validate('model', {
2639
- foo: 'test',
2640
- });
2641
- expect(throwable).to.throw('My error');
2642
- });
2643
-
2644
- it('should allow the given value if the validator returns true', function () {
2645
- const dbs = new DatabaseSchema();
2646
- let called = 0;
2647
- dbs.defineModel({
2648
- name: 'model',
2649
- properties: {
2650
- foo: {
2651
- type: DataType.ANY,
2652
- validate() {
2653
- called++;
2654
- return true;
2655
- },
2656
- },
2657
- },
2658
- });
2659
- dbs.getService(ModelDataValidator).validate('model', {
2660
- foo: 'test',
2661
- });
2662
- expect(called).to.be.eq(1);
2663
- });
2664
-
2665
- it('should throw the error if the validator returns a promise', function () {
2666
- const dbs = new DatabaseSchema();
2667
- dbs.defineModel({
2668
- name: 'model',
2669
- properties: {
2670
- foo: {
2671
- type: DataType.ANY,
2672
- validate() {
2673
- return Promise.resolve(true);
2674
- },
2675
- },
2676
- },
2677
- });
2678
- const throwable = () =>
2679
- dbs.getService(ModelDataValidator).validate('model', {
2680
- foo: 'test',
2681
- });
2682
- expect(throwable).to.throw(
2683
- 'Asynchronous property validators are not supported, ' +
2684
- 'but the property "foo" of the model "model" has a property ' +
2685
- 'validator that returns a Promise.',
2686
- );
2687
- });
2688
-
2689
- it('should throw the error for a non-true result from the validator', function () {
2690
- const testFn = v => {
2691
- const dbs = new DatabaseSchema();
2692
- dbs.defineModel({
2693
- name: 'model',
2694
- properties: {
2695
- foo: {
2696
- type: DataType.ANY,
2697
- validate() {
2698
- return v;
2699
- },
2700
- },
2701
- },
2702
- });
2703
- const throwable = () =>
2704
- dbs.getService(ModelDataValidator).validate('model', {
2705
- foo: 'test',
2706
- });
2707
- expect(throwable).to.throw(
2708
- 'The property "foo" of the model "model" has the invalid value "test" ' +
2709
- 'that caught by a property validator.',
2710
- );
2711
- };
2712
- testFn('str');
2713
- testFn('');
2714
- testFn(10);
2715
- testFn(0);
2716
- testFn(false);
2717
- testFn(undefined);
2718
- testFn(null);
2719
- testFn({});
2720
- testFn([]);
2721
- testFn(() => undefined);
2722
- });
2723
-
2724
- it('should pass arguments to the validator', function () {
2725
- let called = 0;
2726
- const dbs = new DatabaseSchema();
2727
- dbs.defineModel({
2728
- name: 'model',
2729
- properties: {
2730
- foo: {
2731
- type: DataType.ANY,
2732
- validate(value, options, context) {
2733
- expect(value).to.be.eq('test');
2734
- expect(options).to.be.undefined;
2735
- expect(context).to.be.eql({
2736
- validatorName: undefined,
2737
- modelName: 'model',
2738
- propName: 'foo',
2739
- });
2740
- called++;
2741
- return true;
2742
- },
2743
- },
2744
- },
2745
- });
2746
- dbs.getService(ModelDataValidator).validate('model', {
2747
- foo: 'test',
2748
- });
2749
- expect(called).to.be.eq(1);
2750
- });
2751
-
2752
- it('should invoke the validator only once per value', function () {
2753
- let invoked = 0;
2754
- const dbs = new DatabaseSchema();
2755
- dbs.defineModel({
2756
- name: 'model',
2757
- properties: {
2758
- foo: {
2759
- type: DataType.ANY,
2760
- validate() {
2761
- invoked++;
2762
- return true;
2763
- },
2764
- },
2765
- },
2766
- });
2767
- dbs.getService(ModelDataValidator).validate('model', {
2768
- foo: 'test',
2769
- });
2770
- expect(invoked).to.be.eq(1);
2771
- });
2772
- });
2773
- });
2774
-
2775
- describe('when the option "validate" is an Array', function () {
2776
- it('does nothing for an empty array validators', function () {
2777
- const dbs = new DatabaseSchema();
2778
- dbs.defineModel({
2779
- name: 'model',
2780
- properties: {
2781
- foo: {
2782
- type: DataType.ANY,
2783
- validate: [],
2784
- },
2785
- },
2786
- });
2787
- dbs.getService(ModelDataValidator).validate('model', {
2788
- foo: 'test',
2789
- });
2790
- });
2791
-
2792
- it('the option "validate" requires a non-empty String, a Function, an Array or an Object', function () {
2793
- const dbs = new DatabaseSchema();
2794
- dbs
2795
- .getService(PropertyValidatorRegistry)
2796
- .addValidator('myValidator', () => true);
2797
- dbs.defineModel({
2798
- name: 'model',
2799
- properties: {
2800
- foo: {
2801
- type: DataType.ANY,
2802
- validate: undefined,
2803
- },
2804
- },
2805
- });
2806
- const V = dbs.getService(ModelDataValidator);
2807
- const throwable = v => () => {
2808
- const models = dbs.getService(DefinitionRegistry)['_models'];
2809
- models.model.properties.foo.validate = v;
2810
- V.validate('model', {foo: 'bar'});
2811
- };
2812
- const error = v =>
2813
- format(
2814
- 'The provided option "validate" for the property "foo" in the model "model" ' +
2815
- 'should be either a validator name, a validator function, an array ' +
2816
- 'of validator names or functions, or an object mapping validator ' +
2817
- 'names to their arguments, but %s was given.',
2818
- v,
2819
- );
2820
- expect(throwable('')).to.throw(error('""'));
2821
- expect(throwable(10)).to.throw(error('10'));
2822
- expect(throwable(0)).to.throw(error('0'));
2823
- expect(throwable(true)).to.throw(error('true'));
2824
- expect(throwable(false)).to.throw(error('false'));
2825
- throwable('myValidator')();
2826
- throwable(() => true)();
2827
- throwable(['myValidator'])();
2828
- throwable([() => true])();
2829
- throwable([])();
2830
- throwable({myValidator: true})();
2831
- throwable({})();
2832
- });
2833
-
2834
- it('the option "validate" with an Array value requires elements to be a non-empty String or a Function', function () {
2835
- const dbs = new DatabaseSchema();
2836
- dbs
2837
- .getService(PropertyValidatorRegistry)
2838
- .addValidator('myValidator', () => true);
2839
- dbs.defineModel({
2840
- name: 'model',
2841
- properties: {
2842
- foo: {
2843
- type: DataType.ANY,
2844
- validate: undefined,
2845
- },
2846
- },
2847
- });
2848
- const V = dbs.getService(ModelDataValidator);
2849
- const throwable = v => () => {
2850
- const models = dbs.getService(DefinitionRegistry)['_models'];
2851
- models.model.properties.foo.validate = [v];
2852
- V.validate('model', {foo: 'bar'});
2853
- };
2854
- const error = v =>
2855
- format(
2856
- 'The provided option "validate" for the property "foo" in the model "model" ' +
2857
- 'has an Array value that should contain validator names or validator ' +
2858
- 'functions, but %s was given.',
2859
- v,
2860
- );
2861
- expect(throwable('')).to.throw(error('""'));
2862
- expect(throwable(10)).to.throw(error('10'));
2863
- expect(throwable(0)).to.throw(error('0'));
2864
- expect(throwable(true)).to.throw(error('true'));
2865
- expect(throwable(false)).to.throw(error('false'));
2866
- expect(throwable([1, 2, 3])).to.throw(error('Array'));
2867
- expect(throwable({foo: 'bar'})).to.throw(error('Object'));
2868
- expect(throwable(null)).to.throw(error('null'));
2869
- expect(throwable(undefined)).to.throw(error('undefined'));
2870
- throwable('myValidator')();
2871
- throwable(() => true)();
2872
- });
2873
-
2874
- describe('when an array element is a String', function () {
2875
- it('should not validate the non-provided property', function () {
2876
- let calls = 0;
2877
- const dbs = new DatabaseSchema();
2878
- const reg = dbs.getService(PropertyValidatorRegistry);
2879
- reg.addValidator('myValidator1', function () {
2880
- calls++;
2881
- throw new Error('Should not to be called.');
2882
- });
2883
- reg.addValidator('myValidator2', function () {
2884
- calls++;
2885
- throw new Error('Should not to be called.');
2886
- });
2887
- dbs.defineModel({
2888
- name: 'model',
2889
- properties: {
2890
- foo: {
2891
- type: DataType.ANY,
2892
- validate: ['myValidator1', 'myValidator2'],
2893
- },
2894
- },
2895
- });
2896
- const validator = dbs.getService(ModelDataValidator);
2897
- validator.validate('model', {});
2898
- expect(calls).to.be.eq(0);
2899
- });
2900
-
2901
- it('should not validate undefined and null values', function () {
2902
- let calls = 0;
2903
- const dbs = new DatabaseSchema();
2904
- const reg = dbs.getService(PropertyValidatorRegistry);
2905
- reg.addValidator('myValidator1', function () {
2906
- calls++;
2907
- throw new Error('Should not to be called.');
2908
- });
2909
- reg.addValidator('myValidator2', function () {
2910
- calls++;
2911
- throw new Error('Should not to be called.');
2912
- });
2913
- dbs.defineModel({
2914
- name: 'model',
2915
- properties: {
2916
- foo: {
2917
- type: DataType.ANY,
2918
- validate: ['myValidator1', 'myValidator2'],
2919
- },
2920
- },
2921
- });
2922
- const validator = dbs.getService(ModelDataValidator);
2923
- validator.validate('model', {foo: undefined});
2924
- validator.validate('model', {foo: null});
2925
- expect(calls).to.be.eq(0);
2926
- });
2927
-
2928
- it('should not validate the empty value', function () {
2929
- let calls = 0;
2930
- const dbs = new DatabaseSchema();
2931
- const reg = dbs.getService(PropertyValidatorRegistry);
2932
- reg.addValidator('myValidator1', function () {
2933
- calls++;
2934
- throw new Error('Should not to be called.');
2935
- });
2936
- reg.addValidator('myValidator2', function () {
2937
- calls++;
2938
- throw new Error('Should not to be called.');
2939
- });
2940
- dbs.defineModel({
2941
- name: 'model',
2942
- properties: {
2943
- foo: {
2944
- type: DataType.STRING,
2945
- validate: ['myValidator1', 'myValidator2'],
2946
- },
2947
- },
2948
- });
2949
- dbs
2950
- .getService(EmptyValuesService)
2951
- .setEmptyValuesOf(DataType.STRING, [5]);
2952
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
2953
- expect(calls).to.be.eq(0);
2954
- });
2955
-
2956
- it('should throw the error for the non-existent validator name', function () {
2957
- let called = 0;
2958
- const dbs = new DatabaseSchema();
2959
- const reg = dbs.getService(PropertyValidatorRegistry);
2960
- reg.addValidator('myValidator1', function () {
2961
- called++;
2962
- return true;
2963
- });
2964
- const modelDef = {
2965
- name: 'model',
2966
- properties: {
2967
- foo: {
2968
- type: DataType.ANY,
2969
- validate: ['myValidator1'],
2970
- },
2971
- },
2972
- };
2973
- dbs.defineModel(modelDef);
2974
- modelDef.properties.foo.validate.push('myValidator2');
2975
- const throwable = () =>
2976
- dbs.getService(ModelDataValidator).validate('model', {
2977
- foo: 'test',
2978
- });
2979
- expect(throwable).to.throw(
2980
- 'The property validator "myValidator2" is not defined.',
2981
- );
2982
- expect(called).to.be.eq(1);
2983
- });
2984
-
2985
- it('should throw the error from the first validator', function () {
2986
- let called = 0;
2987
- const dbs = new DatabaseSchema();
2988
- const reg = dbs.getService(PropertyValidatorRegistry);
2989
- reg.addValidator('myValidator1', function () {
2990
- called++;
2991
- throw Error('My error');
2992
- });
2993
- reg.addValidator('myValidator2', function () {
2994
- called++;
2995
- return false;
2996
- });
2997
- dbs.defineModel({
2998
- name: 'model',
2999
- properties: {
3000
- foo: {
3001
- type: DataType.ANY,
3002
- validate: ['myValidator1', 'myValidator2'],
3003
- },
3004
- },
3005
- });
3006
- const throwable = () =>
3007
- dbs.getService(ModelDataValidator).validate('model', {
3008
- foo: 'test',
3009
- });
3010
- expect(throwable).to.throw('My error');
3011
- expect(called).to.be.eq(1);
3012
- });
3013
-
3014
- it('should throw the error from the second validator', function () {
3015
- let called = 0;
3016
- const dbs = new DatabaseSchema();
3017
- const reg = dbs.getService(PropertyValidatorRegistry);
3018
- reg.addValidator('myValidator1', function () {
3019
- called++;
3020
- return true;
3021
- });
3022
- reg.addValidator('myValidator2', function () {
3023
- called++;
3024
- throw Error('My error');
3025
- });
3026
- dbs.defineModel({
3027
- name: 'model',
3028
- properties: {
3029
- foo: {
3030
- type: DataType.ANY,
3031
- validate: ['myValidator1', 'myValidator2'],
3032
- },
3033
- },
3034
- });
3035
- const throwable = () =>
3036
- dbs.getService(ModelDataValidator).validate('model', {
3037
- foo: 'test',
3038
- });
3039
- expect(throwable).to.throw('My error');
3040
- expect(called).to.be.eq(2);
3041
- });
3042
-
3043
- it('should allow the given value if validators returns true', function () {
3044
- let called = 0;
3045
- const dbs = new DatabaseSchema();
3046
- const reg = dbs.getService(PropertyValidatorRegistry);
3047
- reg.addValidator('myValidator1', function () {
3048
- called++;
3049
- return true;
3050
- });
3051
- reg.addValidator('myValidator2', function () {
3052
- called++;
3053
- return true;
3054
- });
3055
- dbs.defineModel({
3056
- name: 'model',
3057
- properties: {
3058
- foo: {
3059
- type: DataType.ANY,
3060
- validate: ['myValidator1', 'myValidator2'],
3061
- },
3062
- },
3063
- });
3064
- dbs.getService(ModelDataValidator).validate('model', {
3065
- foo: 'test',
3066
- });
3067
- expect(called).to.be.eq(2);
3068
- });
3069
-
3070
- it('should throw the error if the first validator returns a promise', function () {
3071
- let called = 0;
3072
- const dbs = new DatabaseSchema();
3073
- const reg = dbs.getService(PropertyValidatorRegistry);
3074
- reg.addValidator('myValidator1', function () {
3075
- called++;
3076
- return Promise.resolve(true);
3077
- });
3078
- reg.addValidator('myValidator2', function () {
3079
- called++;
3080
- return true;
3081
- });
3082
- dbs.defineModel({
3083
- name: 'model',
3084
- properties: {
3085
- foo: {
3086
- type: DataType.ANY,
3087
- validate: ['myValidator1', 'myValidator2'],
3088
- },
3089
- },
3090
- });
3091
- const throwable = () =>
3092
- dbs.getService(ModelDataValidator).validate('model', {
3093
- foo: 'test',
3094
- });
3095
- expect(throwable).to.throw(
3096
- 'Asynchronous property validators are not supported, ' +
3097
- 'but the property "foo" of the model "model" has the property ' +
3098
- 'validator "myValidator1" that returns a Promise.',
3099
- );
3100
- expect(called).to.be.eq(1);
3101
- });
3102
-
3103
- it('should throw the error if the second validator returns a promise', function () {
3104
- let called = 0;
3105
- const dbs = new DatabaseSchema();
3106
- const reg = dbs.getService(PropertyValidatorRegistry);
3107
- reg.addValidator('myValidator1', function () {
3108
- called++;
3109
- return true;
3110
- });
3111
- reg.addValidator('myValidator2', function () {
3112
- called++;
3113
- return Promise.resolve(true);
3114
- });
3115
- dbs.defineModel({
3116
- name: 'model',
3117
- properties: {
3118
- foo: {
3119
- type: DataType.ANY,
3120
- validate: ['myValidator1', 'myValidator2'],
3121
- },
3122
- },
3123
- });
3124
- const throwable = () =>
3125
- dbs.getService(ModelDataValidator).validate('model', {
3126
- foo: 'test',
3127
- });
3128
- expect(throwable).to.throw(
3129
- 'Asynchronous property validators are not supported, ' +
3130
- 'but the property "foo" of the model "model" has the property ' +
3131
- 'validator "myValidator2" that returns a Promise.',
3132
- );
3133
- expect(called).to.be.eq(2);
3134
- });
3135
-
3136
- it('should throw the error for a non-true result from the first validator', function () {
3137
- const testFn = v => {
3138
- let called = 0;
3139
- const dbs = new DatabaseSchema();
3140
- const reg = dbs.getService(PropertyValidatorRegistry);
3141
- reg.addValidator('myValidator1', function () {
3142
- called++;
3143
- return v;
3144
- });
3145
- reg.addValidator('myValidator2', function () {
3146
- called++;
3147
- return true;
3148
- });
3149
- dbs.defineModel({
3150
- name: 'model',
3151
- properties: {
3152
- foo: {
3153
- type: DataType.ANY,
3154
- validate: ['myValidator1', 'myValidator2'],
3155
- },
3156
- },
3157
- });
3158
- const throwable = () =>
3159
- dbs.getService(ModelDataValidator).validate('model', {
3160
- foo: 'test',
3161
- });
3162
- expect(throwable).to.throw(
3163
- 'The property "foo" of the model "model" has the invalid value "test" ' +
3164
- 'that caught by the property validator "myValidator1".',
3165
- );
3166
- expect(called).to.be.eq(1);
3167
- };
3168
- testFn('str');
3169
- testFn('');
3170
- testFn(10);
3171
- testFn(0);
3172
- testFn(false);
3173
- testFn(undefined);
3174
- testFn(null);
3175
- testFn({});
3176
- testFn([]);
3177
- testFn(() => undefined);
3178
- });
3179
-
3180
- it('should throw the error for a non-true result from the second validator', function () {
3181
- const testFn = v => {
3182
- let called = 0;
3183
- const dbs = new DatabaseSchema();
3184
- const reg = dbs.getService(PropertyValidatorRegistry);
3185
- reg.addValidator('myValidator1', function () {
3186
- called++;
3187
- return true;
3188
- });
3189
- reg.addValidator('myValidator2', function () {
3190
- called++;
3191
- return v;
3192
- });
3193
- dbs.defineModel({
3194
- name: 'model',
3195
- properties: {
3196
- foo: {
3197
- type: DataType.ANY,
3198
- validate: ['myValidator1', 'myValidator2'],
3199
- },
3200
- },
3201
- });
3202
- const throwable = () =>
3203
- dbs.getService(ModelDataValidator).validate('model', {
3204
- foo: 'test',
3205
- });
3206
- expect(throwable).to.throw(
3207
- 'The property "foo" of the model "model" has the invalid value "test" ' +
3208
- 'that caught by the property validator "myValidator2".',
3209
- );
3210
- expect(called).to.be.eq(2);
3211
- };
3212
- testFn('str');
3213
- testFn('');
3214
- testFn(10);
3215
- testFn(0);
3216
- testFn(false);
3217
- testFn(undefined);
3218
- testFn(null);
3219
- testFn({});
3220
- testFn([]);
3221
- testFn(() => undefined);
3222
- });
3223
-
3224
- it('should pass arguments to validators', function () {
3225
- let called = false;
3226
- const dbs = new DatabaseSchema();
3227
- const reg = dbs.getService(PropertyValidatorRegistry);
3228
- reg.addValidator(
3229
- 'myValidator1',
3230
- function (value, options, context) {
3231
- expect(value).to.be.eq('test');
3232
- expect(options).to.be.undefined;
3233
- expect(context).to.be.eql({
3234
- validatorName: 'myValidator1',
3235
- modelName: 'model',
3236
- propName: 'foo',
3237
- });
3238
- called++;
3239
- return true;
3240
- },
3241
- );
3242
- reg.addValidator(
3243
- 'myValidator2',
3244
- function (value, options, context) {
3245
- expect(value).to.be.eq('test');
3246
- expect(options).to.be.undefined;
3247
- expect(context).to.be.eql({
3248
- validatorName: 'myValidator2',
3249
- modelName: 'model',
3250
- propName: 'foo',
3251
- });
3252
- called++;
3253
- return true;
3254
- },
3255
- );
3256
- dbs.defineModel({
3257
- name: 'model',
3258
- properties: {
3259
- foo: {
3260
- type: DataType.ANY,
3261
- validate: ['myValidator1', 'myValidator2'],
3262
- },
3263
- },
3264
- });
3265
- dbs.getService(ModelDataValidator).validate('model', {
3266
- foo: 'test',
3267
- });
3268
- expect(called).to.be.eq(2);
3269
- });
3270
-
3271
- it('should invoke validators in the correct order', function () {
3272
- const invocation = [];
3273
- const dbs = new DatabaseSchema();
3274
- const reg = dbs.getService(PropertyValidatorRegistry);
3275
- reg.addValidator('myValidator1', function () {
3276
- invocation.push('myValidator1');
3277
- return true;
3278
- });
3279
- reg.addValidator('myValidator2', function () {
3280
- invocation.push('myValidator2');
3281
- return true;
3282
- });
3283
- dbs.defineModel({
3284
- name: 'model',
3285
- properties: {
3286
- foo: {
3287
- type: DataType.ANY,
3288
- validate: ['myValidator1', 'myValidator2'],
3289
- },
3290
- },
3291
- });
3292
- dbs.getService(ModelDataValidator).validate('model', {
3293
- foo: 'test',
3294
- });
3295
- expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
3296
- });
3297
- });
3298
-
3299
- describe('when an array element is a Function', function () {
3300
- describe('named validators', function () {
3301
- it('should not validate the non-provided property', function () {
3302
- let calls = 0;
3303
- const dbs = new DatabaseSchema();
3304
- const myValidator1 = function () {
3305
- calls++;
3306
- throw new Error('Should not to be called.');
3307
- };
3308
- const myValidator2 = function () {
3309
- calls++;
3310
- throw new Error('Should not to be called.');
3311
- };
3312
- dbs.defineModel({
3313
- name: 'model',
3314
- properties: {
3315
- foo: {
3316
- type: DataType.ANY,
3317
- validate: [myValidator1, myValidator2],
3318
- },
3319
- },
3320
- });
3321
- const validator = dbs.getService(ModelDataValidator);
3322
- validator.validate('model', {});
3323
- expect(calls).to.be.eq(0);
3324
- });
3325
-
3326
- it('should not validate undefined and null values', function () {
3327
- let calls = 0;
3328
- const dbs = new DatabaseSchema();
3329
- const myValidator1 = function () {
3330
- calls++;
3331
- throw new Error('Should not to be called.');
3332
- };
3333
- const myValidator2 = function () {
3334
- calls++;
3335
- throw new Error('Should not to be called.');
3336
- };
3337
- dbs.defineModel({
3338
- name: 'model',
3339
- properties: {
3340
- foo: {
3341
- type: DataType.ANY,
3342
- validate: [myValidator1, myValidator2],
3343
- },
3344
- },
3345
- });
3346
- const validator = dbs.getService(ModelDataValidator);
3347
- validator.validate('model', {foo: undefined});
3348
- validator.validate('model', {foo: null});
3349
- expect(calls).to.be.eq(0);
3350
- });
3351
-
3352
- it('should not validate the empty value', function () {
3353
- let calls = 0;
3354
- const dbs = new DatabaseSchema();
3355
- const myValidator1 = function () {
3356
- calls++;
3357
- throw new Error('Should not to be called.');
3358
- };
3359
- const myValidator2 = function () {
3360
- calls++;
3361
- throw new Error('Should not to be called.');
3362
- };
3363
- dbs.defineModel({
3364
- name: 'model',
3365
- properties: {
3366
- foo: {
3367
- type: DataType.STRING,
3368
- validate: [myValidator1, myValidator2],
3369
- },
3370
- },
3371
- });
3372
- dbs
3373
- .getService(EmptyValuesService)
3374
- .setEmptyValuesOf(DataType.STRING, [5]);
3375
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
3376
- expect(calls).to.be.eq(0);
3377
- });
3378
-
3379
- it('should throw the error from the first validator', function () {
3380
- let called = 0;
3381
- const dbs = new DatabaseSchema();
3382
- const myValidator1 = function () {
3383
- called++;
3384
- throw Error('My error');
3385
- };
3386
- const myValidator2 = function () {
3387
- called++;
3388
- return false;
3389
- };
3390
- dbs.defineModel({
3391
- name: 'model',
3392
- properties: {
3393
- foo: {
3394
- type: DataType.ANY,
3395
- validate: [myValidator1, myValidator2],
3396
- },
3397
- },
3398
- });
3399
- const throwable = () =>
3400
- dbs.getService(ModelDataValidator).validate('model', {
3401
- foo: 'test',
3402
- });
3403
- expect(throwable).to.throw('My error');
3404
- expect(called).to.be.eq(1);
3405
- });
3406
-
3407
- it('should throw the error from the second validator', function () {
3408
- let called = 0;
3409
- const dbs = new DatabaseSchema();
3410
- const myValidator1 = function () {
3411
- called++;
3412
- return true;
3413
- };
3414
- const myValidator2 = function () {
3415
- called++;
3416
- throw Error('My error');
3417
- };
3418
- dbs.defineModel({
3419
- name: 'model',
3420
- properties: {
3421
- foo: {
3422
- type: DataType.ANY,
3423
- validate: [myValidator1, myValidator2],
3424
- },
3425
- },
3426
- });
3427
- const throwable = () =>
3428
- dbs.getService(ModelDataValidator).validate('model', {
3429
- foo: 'test',
3430
- });
3431
- expect(throwable).to.throw('My error');
3432
- expect(called).to.be.eq(2);
3433
- });
3434
-
3435
- it('should allow the given value if validators returns true', function () {
3436
- let called = 0;
3437
- const dbs = new DatabaseSchema();
3438
- const myValidator1 = function () {
3439
- called++;
3440
- return true;
3441
- };
3442
- const myValidator2 = function () {
3443
- called++;
3444
- return true;
3445
- };
3446
- dbs.defineModel({
3447
- name: 'model',
3448
- properties: {
3449
- foo: {
3450
- type: DataType.ANY,
3451
- validate: [myValidator1, myValidator2],
3452
- },
3453
- },
3454
- });
3455
- dbs.getService(ModelDataValidator).validate('model', {
3456
- foo: 'test',
3457
- });
3458
- expect(called).to.be.eq(2);
3459
- });
3460
-
3461
- it('should throw the error if the first validator returns a promise', function () {
3462
- let called = 0;
3463
- const dbs = new DatabaseSchema();
3464
- const myValidator1 = function () {
3465
- called++;
3466
- return Promise.resolve(true);
3467
- };
3468
- const myValidator2 = function () {
3469
- called++;
3470
- return true;
3471
- };
3472
- dbs.defineModel({
3473
- name: 'model',
3474
- properties: {
3475
- foo: {
3476
- type: DataType.ANY,
3477
- validate: [myValidator1, myValidator2],
3478
- },
3479
- },
3480
- });
3481
- const throwable = () =>
3482
- dbs.getService(ModelDataValidator).validate('model', {
3483
- foo: 'test',
3484
- });
3485
- expect(throwable).to.throw(
3486
- 'Asynchronous property validators are not supported, ' +
3487
- 'but the property "foo" of the model "model" has the property ' +
3488
- 'validator "myValidator1" that returns a Promise.',
3489
- );
3490
- expect(called).to.be.eq(1);
3491
- });
3492
-
3493
- it('should throw the error if the second validator returns a promise', function () {
3494
- let called = 0;
3495
- const dbs = new DatabaseSchema();
3496
- const myValidator1 = function () {
3497
- called++;
3498
- return true;
3499
- };
3500
- const myValidator2 = function () {
3501
- called++;
3502
- return Promise.resolve(true);
3503
- };
3504
- dbs.defineModel({
3505
- name: 'model',
3506
- properties: {
3507
- foo: {
3508
- type: DataType.ANY,
3509
- validate: [myValidator1, myValidator2],
3510
- },
3511
- },
3512
- });
3513
- const throwable = () =>
3514
- dbs.getService(ModelDataValidator).validate('model', {
3515
- foo: 'test',
3516
- });
3517
- expect(throwable).to.throw(
3518
- 'Asynchronous property validators are not supported, ' +
3519
- 'but the property "foo" of the model "model" has the property ' +
3520
- 'validator "myValidator2" that returns a Promise.',
3521
- );
3522
- expect(called).to.be.eq(2);
3523
- });
3524
-
3525
- it('should throw the error for a non-true result from the first validator', function () {
3526
- const testFn = v => {
3527
- let called = 0;
3528
- const dbs = new DatabaseSchema();
3529
- const myValidator1 = function () {
3530
- called++;
3531
- return v;
3532
- };
3533
- const myValidator2 = function () {
3534
- called++;
3535
- return true;
3536
- };
3537
- dbs.defineModel({
3538
- name: 'model',
3539
- properties: {
3540
- foo: {
3541
- type: DataType.ANY,
3542
- validate: [myValidator1, myValidator2],
3543
- },
3544
- },
3545
- });
3546
- const throwable = () =>
3547
- dbs.getService(ModelDataValidator).validate('model', {
3548
- foo: 'test',
3549
- });
3550
- expect(throwable).to.throw(
3551
- 'The property "foo" of the model "model" has the invalid value "test" ' +
3552
- 'that caught by the property validator "myValidator1".',
3553
- );
3554
- expect(called).to.be.eq(1);
3555
- };
3556
- testFn('str');
3557
- testFn('');
3558
- testFn(10);
3559
- testFn(0);
3560
- testFn(false);
3561
- testFn(undefined);
3562
- testFn(null);
3563
- testFn({});
3564
- testFn([]);
3565
- testFn(() => undefined);
3566
- });
3567
-
3568
- it('should throw the error for a non-true result from the second validator', function () {
3569
- const testFn = v => {
3570
- let called = 0;
3571
- const dbs = new DatabaseSchema();
3572
- const myValidator1 = function () {
3573
- called++;
3574
- return true;
3575
- };
3576
- const myValidator2 = function () {
3577
- called++;
3578
- return v;
3579
- };
3580
- dbs.defineModel({
3581
- name: 'model',
3582
- properties: {
3583
- foo: {
3584
- type: DataType.ANY,
3585
- validate: [myValidator1, myValidator2],
3586
- },
3587
- },
3588
- });
3589
- const throwable = () =>
3590
- dbs.getService(ModelDataValidator).validate('model', {
3591
- foo: 'test',
3592
- });
3593
- expect(throwable).to.throw(
3594
- 'The property "foo" of the model "model" has the invalid value "test" ' +
3595
- 'that caught by the property validator "myValidator2".',
3596
- );
3597
- expect(called).to.be.eq(2);
3598
- };
3599
- testFn('str');
3600
- testFn('');
3601
- testFn(10);
3602
- testFn(0);
3603
- testFn(false);
3604
- testFn(undefined);
3605
- testFn(null);
3606
- testFn({});
3607
- testFn([]);
3608
- testFn(() => undefined);
3609
- });
3610
-
3611
- it('should pass arguments to validators', function () {
3612
- let called = false;
3613
- const dbs = new DatabaseSchema();
3614
- const myValidator1 = function (value, options, context) {
3615
- expect(value).to.be.eq('test');
3616
- expect(options).to.be.undefined;
3617
- expect(context).to.be.eql({
3618
- validatorName: 'myValidator1',
3619
- modelName: 'model',
3620
- propName: 'foo',
3621
- });
3622
- called++;
3623
- return true;
3624
- };
3625
- const myValidator2 = function (value, options, context) {
3626
- expect(value).to.be.eq('test');
3627
- expect(options).to.be.undefined;
3628
- expect(context).to.be.eql({
3629
- validatorName: 'myValidator2',
3630
- modelName: 'model',
3631
- propName: 'foo',
3632
- });
3633
- called++;
3634
- return true;
3635
- };
3636
- dbs.defineModel({
3637
- name: 'model',
3638
- properties: {
3639
- foo: {
3640
- type: DataType.ANY,
3641
- validate: [myValidator1, myValidator2],
3642
- },
3643
- },
3644
- });
3645
- dbs.getService(ModelDataValidator).validate('model', {
3646
- foo: 'test',
3647
- });
3648
- expect(called).to.be.eq(2);
3649
- });
3650
-
3651
- it('should invoke validators in the correct order', function () {
3652
- const invocation = [];
3653
- const myValidator1 = function () {
3654
- invocation.push('myValidator1');
3655
- return true;
3656
- };
3657
- const myValidator2 = function () {
3658
- invocation.push('myValidator2');
3659
- return true;
3660
- };
3661
- const dbs = new DatabaseSchema();
3662
- dbs.defineModel({
3663
- name: 'model',
3664
- properties: {
3665
- foo: {
3666
- type: DataType.ANY,
3667
- validate: [myValidator1, myValidator2],
3668
- },
3669
- },
3670
- });
3671
- dbs.getService(ModelDataValidator).validate('model', {
3672
- foo: 'test',
3673
- });
3674
- expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
3675
- });
3676
- });
3677
-
3678
- describe('anonymous validators', function () {
3679
- it('should not validate the non-provided property', function () {
3680
- let calls = 0;
3681
- const dbs = new DatabaseSchema();
3682
- dbs.defineModel({
3683
- name: 'model',
3684
- properties: {
3685
- foo: {
3686
- type: DataType.ANY,
3687
- validate: [
3688
- () => {
3689
- calls++;
3690
- throw new Error('Should not to be called.');
3691
- },
3692
- () => {
3693
- calls++;
3694
- throw new Error('Should not to be called.');
3695
- },
3696
- ],
3697
- },
3698
- },
3699
- });
3700
- const validator = dbs.getService(ModelDataValidator);
3701
- validator.validate('model', {});
3702
- expect(calls).to.be.eq(0);
3703
- });
3704
-
3705
- it('should not validate undefined and null values', function () {
3706
- let calls = 0;
3707
- const dbs = new DatabaseSchema();
3708
- dbs.defineModel({
3709
- name: 'model',
3710
- properties: {
3711
- foo: {
3712
- type: DataType.ANY,
3713
- validate: [
3714
- () => {
3715
- calls++;
3716
- throw new Error('Should not to be called.');
3717
- },
3718
- () => {
3719
- calls++;
3720
- throw new Error('Should not to be called.');
3721
- },
3722
- ],
3723
- },
3724
- },
3725
- });
3726
- const validator = dbs.getService(ModelDataValidator);
3727
- validator.validate('model', {foo: undefined});
3728
- validator.validate('model', {foo: null});
3729
- expect(calls).to.be.eq(0);
3730
- });
3731
-
3732
- it('should not validate the empty value', function () {
3733
- let calls = 0;
3734
- const dbs = new DatabaseSchema();
3735
- dbs.defineModel({
3736
- name: 'model',
3737
- properties: {
3738
- foo: {
3739
- type: DataType.STRING,
3740
- validate: [
3741
- () => {
3742
- calls++;
3743
- throw new Error('Should not to be called.');
3744
- },
3745
- () => {
3746
- calls++;
3747
- throw new Error('Should not to be called.');
3748
- },
3749
- ],
3750
- },
3751
- },
3752
- });
3753
- dbs
3754
- .getService(EmptyValuesService)
3755
- .setEmptyValuesOf(DataType.STRING, [5]);
3756
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
3757
- expect(calls).to.be.eq(0);
3758
- });
3759
-
3760
- it('should throw the error from the first validator', function () {
3761
- let called = 0;
3762
- const dbs = new DatabaseSchema();
3763
- dbs.defineModel({
3764
- name: 'model',
3765
- properties: {
3766
- foo: {
3767
- type: DataType.ANY,
3768
- validate: [
3769
- () => {
3770
- called++;
3771
- throw Error('My error');
3772
- },
3773
- () => {
3774
- called++;
3775
- return false;
3776
- },
3777
- ],
3778
- },
3779
- },
3780
- });
3781
- const throwable = () =>
3782
- dbs.getService(ModelDataValidator).validate('model', {
3783
- foo: 'test',
3784
- });
3785
- expect(throwable).to.throw('My error');
3786
- expect(called).to.be.eq(1);
3787
- });
3788
-
3789
- it('should throw the error from the second validator', function () {
3790
- let called = 0;
3791
- const dbs = new DatabaseSchema();
3792
- dbs.defineModel({
3793
- name: 'model',
3794
- properties: {
3795
- foo: {
3796
- type: DataType.ANY,
3797
- validate: [
3798
- () => {
3799
- called++;
3800
- return true;
3801
- },
3802
- () => {
3803
- called++;
3804
- throw Error('My error');
3805
- },
3806
- ],
3807
- },
3808
- },
3809
- });
3810
- const throwable = () =>
3811
- dbs.getService(ModelDataValidator).validate('model', {
3812
- foo: 'test',
3813
- });
3814
- expect(throwable).to.throw('My error');
3815
- expect(called).to.be.eq(2);
3816
- });
3817
-
3818
- it('should allow the given value if validators returns true', function () {
3819
- let called = 0;
3820
- const dbs = new DatabaseSchema();
3821
- dbs.defineModel({
3822
- name: 'model',
3823
- properties: {
3824
- foo: {
3825
- type: DataType.ANY,
3826
- validate: [
3827
- () => {
3828
- called++;
3829
- return true;
3830
- },
3831
- () => {
3832
- called++;
3833
- return true;
3834
- },
3835
- ],
3836
- },
3837
- },
3838
- });
3839
- dbs.getService(ModelDataValidator).validate('model', {
3840
- foo: 'test',
3841
- });
3842
- expect(called).to.be.eq(2);
3843
- });
3844
-
3845
- it('should throw the error if the first validator returns a promise', function () {
3846
- let called = 0;
3847
- const dbs = new DatabaseSchema();
3848
- dbs.defineModel({
3849
- name: 'model',
3850
- properties: {
3851
- foo: {
3852
- type: DataType.ANY,
3853
- validate: [
3854
- () => {
3855
- called++;
3856
- return Promise.resolve(true);
3857
- },
3858
- () => {
3859
- called++;
3860
- return true;
3861
- },
3862
- ],
3863
- },
3864
- },
3865
- });
3866
- const throwable = () =>
3867
- dbs.getService(ModelDataValidator).validate('model', {
3868
- foo: 'test',
3869
- });
3870
- expect(throwable).to.throw(
3871
- 'Asynchronous property validators are not supported, ' +
3872
- 'but the property "foo" of the model "model" has a property ' +
3873
- 'validator that returns a Promise.',
3874
- );
3875
- expect(called).to.be.eq(1);
3876
- });
3877
-
3878
- it('should throw the error if the second validator returns a promise', function () {
3879
- let called = 0;
3880
- const dbs = new DatabaseSchema();
3881
- dbs.defineModel({
3882
- name: 'model',
3883
- properties: {
3884
- foo: {
3885
- type: DataType.ANY,
3886
- validate: [
3887
- () => {
3888
- called++;
3889
- return true;
3890
- },
3891
- () => {
3892
- called++;
3893
- return Promise.resolve(true);
3894
- },
3895
- ],
3896
- },
3897
- },
3898
- });
3899
- const throwable = () =>
3900
- dbs.getService(ModelDataValidator).validate('model', {
3901
- foo: 'test',
3902
- });
3903
- expect(throwable).to.throw(
3904
- 'Asynchronous property validators are not supported, ' +
3905
- 'but the property "foo" of the model "model" has a property ' +
3906
- 'validator that returns a Promise.',
3907
- );
3908
- expect(called).to.be.eq(2);
3909
- });
3910
-
3911
- it('should throw the error for a non-true result from the first validator', function () {
3912
- const testFn = v => {
3913
- let called = 0;
3914
- const dbs = new DatabaseSchema();
3915
- dbs.defineModel({
3916
- name: 'model',
3917
- properties: {
3918
- foo: {
3919
- type: DataType.ANY,
3920
- validate: [
3921
- () => {
3922
- called++;
3923
- return v;
3924
- },
3925
- () => {
3926
- called++;
3927
- return true;
3928
- },
3929
- ],
3930
- },
3931
- },
3932
- });
3933
- const throwable = () =>
3934
- dbs.getService(ModelDataValidator).validate('model', {
3935
- foo: 'test',
3936
- });
3937
- expect(throwable).to.throw(
3938
- 'The property "foo" of the model "model" has the invalid value "test" ' +
3939
- 'that caught by a property validator.',
3940
- );
3941
- expect(called).to.be.eq(1);
3942
- };
3943
- testFn('str');
3944
- testFn('');
3945
- testFn(10);
3946
- testFn(0);
3947
- testFn(false);
3948
- testFn(undefined);
3949
- testFn(null);
3950
- testFn({});
3951
- testFn([]);
3952
- testFn(() => undefined);
3953
- });
3954
-
3955
- it('should throw the error for a non-true result from the second validator', function () {
3956
- const testFn = v => {
3957
- let called = 0;
3958
- const dbs = new DatabaseSchema();
3959
- dbs.defineModel({
3960
- name: 'model',
3961
- properties: {
3962
- foo: {
3963
- type: DataType.ANY,
3964
- validate: [
3965
- () => {
3966
- called++;
3967
- return true;
3968
- },
3969
- () => {
3970
- called++;
3971
- return v;
3972
- },
3973
- ],
3974
- },
3975
- },
3976
- });
3977
- const throwable = () =>
3978
- dbs.getService(ModelDataValidator).validate('model', {
3979
- foo: 'test',
3980
- });
3981
- expect(throwable).to.throw(
3982
- 'The property "foo" of the model "model" has the invalid value "test" ' +
3983
- 'that caught by a property validator.',
3984
- );
3985
- expect(called).to.be.eq(2);
3986
- };
3987
- testFn('str');
3988
- testFn('');
3989
- testFn(10);
3990
- testFn(0);
3991
- testFn(false);
3992
- testFn(undefined);
3993
- testFn(null);
3994
- testFn({});
3995
- testFn([]);
3996
- testFn(() => undefined);
3997
- });
3998
-
3999
- it('should pass arguments to validators', function () {
4000
- let called = false;
4001
- const dbs = new DatabaseSchema();
4002
- dbs.defineModel({
4003
- name: 'model',
4004
- properties: {
4005
- foo: {
4006
- type: DataType.ANY,
4007
- validate: [
4008
- (value, options, context) => {
4009
- expect(value).to.be.eq('test');
4010
- expect(options).to.be.undefined;
4011
- expect(context).to.be.eql({
4012
- validatorName: undefined,
4013
- modelName: 'model',
4014
- propName: 'foo',
4015
- });
4016
- called++;
4017
- return true;
4018
- },
4019
- (value, options, context) => {
4020
- expect(value).to.be.eq('test');
4021
- expect(options).to.be.undefined;
4022
- expect(context).to.be.eql({
4023
- validatorName: undefined,
4024
- modelName: 'model',
4025
- propName: 'foo',
4026
- });
4027
- called++;
4028
- return true;
4029
- },
4030
- ],
4031
- },
4032
- },
4033
- });
4034
- dbs.getService(ModelDataValidator).validate('model', {
4035
- foo: 'test',
4036
- });
4037
- expect(called).to.be.eq(2);
4038
- });
4039
-
4040
- it('should invoke validators in the correct order', function () {
4041
- const invocation = [];
4042
- const dbs = new DatabaseSchema();
4043
- dbs.defineModel({
4044
- name: 'model',
4045
- properties: {
4046
- foo: {
4047
- type: DataType.ANY,
4048
- validate: [
4049
- () => {
4050
- invocation.push('myValidator1');
4051
- return true;
4052
- },
4053
- () => {
4054
- invocation.push('myValidator2');
4055
- return true;
4056
- },
4057
- ],
4058
- },
4059
- },
4060
- });
4061
- dbs.getService(ModelDataValidator).validate('model', {
4062
- foo: 'test',
4063
- });
4064
- expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
4065
- });
4066
- });
4067
- });
4068
- });
4069
-
4070
- describe('when the option "validate" is an Object', function () {
4071
- it('should not validate the non-provided property', function () {
4072
- let calls = 0;
4073
- const dbs = new DatabaseSchema();
4074
- const reg = dbs.getService(PropertyValidatorRegistry);
4075
- reg.addValidator('myValidator1', function () {
4076
- calls++;
4077
- throw new Error('Should not to be called.');
4078
- });
4079
- reg.addValidator('myValidator2', function () {
4080
- calls++;
4081
- throw new Error('Should not to be called.');
4082
- });
4083
- dbs.defineModel({
4084
- name: 'model',
4085
- properties: {
4086
- foo: {
4087
- type: DataType.ANY,
4088
- validate: {
4089
- myValidator1: true,
4090
- myValidator2: true,
4091
- },
4092
- },
4093
- },
4094
- });
4095
- const validator = dbs.getService(ModelDataValidator);
4096
- validator.validate('model', {});
4097
- expect(calls).to.be.eq(0);
4098
- });
4099
-
4100
- it('should not validate undefined and null values', function () {
4101
- let calls = 0;
4102
- const dbs = new DatabaseSchema();
4103
- const reg = dbs.getService(PropertyValidatorRegistry);
4104
- reg.addValidator('myValidator1', function () {
4105
- calls++;
4106
- throw new Error('Should not to be called.');
4107
- });
4108
- reg.addValidator('myValidator2', function () {
4109
- calls++;
4110
- throw new Error('Should not to be called.');
4111
- });
4112
- dbs.defineModel({
4113
- name: 'model',
4114
- properties: {
4115
- foo: {
4116
- type: DataType.ANY,
4117
- validate: {
4118
- myValidator1: true,
4119
- myValidator2: true,
4120
- },
4121
- },
4122
- },
4123
- });
4124
- const validator = dbs.getService(ModelDataValidator);
4125
- validator.validate('model', {foo: undefined});
4126
- validator.validate('model', {foo: null});
4127
- expect(calls).to.be.eq(0);
4128
- });
4129
-
4130
- it('should not validate the empty value', function () {
4131
- let calls = 0;
4132
- const dbs = new DatabaseSchema();
4133
- const reg = dbs.getService(PropertyValidatorRegistry);
4134
- reg.addValidator('myValidator1', function () {
4135
- calls++;
4136
- throw new Error('Should not to be called.');
4137
- });
4138
- reg.addValidator('myValidator2', function () {
4139
- calls++;
4140
- throw new Error('Should not to be called.');
4141
- });
4142
- dbs.defineModel({
4143
- name: 'model',
4144
- properties: {
4145
- foo: {
4146
- type: DataType.STRING,
4147
- validate: {
4148
- myValidator1: true,
4149
- myValidator2: true,
4150
- },
4151
- },
4152
- },
4153
- });
4154
- dbs
4155
- .getService(EmptyValuesService)
4156
- .setEmptyValuesOf(DataType.STRING, [5]);
4157
- dbs.getService(ModelDataValidator).validate('model', {foo: 5});
4158
- expect(calls).to.be.eq(0);
4159
- });
4160
-
4161
- it('should throw the error for the non-existent validator name', function () {
4162
- let called = 0;
4163
- const dbs = new DatabaseSchema();
4164
- const reg = dbs.getService(PropertyValidatorRegistry);
4165
- reg.addValidator('myValidator1', function () {
4166
- called++;
4167
- return true;
4168
- });
4169
- const modelDef = {
4170
- name: 'model',
4171
- properties: {
4172
- foo: {
4173
- type: DataType.ANY,
4174
- validate: {
4175
- myValidator1: true,
4176
- },
4177
- },
4178
- },
4179
- };
4180
- dbs.defineModel(modelDef);
4181
- modelDef.properties.foo.validate['myValidator2'] = true;
4182
- const throwable = () =>
4183
- dbs.getService(ModelDataValidator).validate('model', {
4184
- foo: 'test',
4185
- });
4186
- expect(throwable).to.throw(
4187
- 'The property validator "myValidator2" is not defined.',
4188
- );
4189
- expect(called).to.be.eq(1);
4190
- });
4191
-
4192
- it('should throw the error from the first validator', function () {
4193
- let called = 0;
4194
- const dbs = new DatabaseSchema();
4195
- const reg = dbs.getService(PropertyValidatorRegistry);
4196
- reg.addValidator('myValidator1', function () {
4197
- called++;
4198
- throw Error('My error');
4199
- });
4200
- reg.addValidator('myValidator2', function () {
4201
- called++;
4202
- return false;
4203
- });
4204
- dbs.defineModel({
4205
- name: 'model',
4206
- properties: {
4207
- foo: {
4208
- type: DataType.ANY,
4209
- validate: {
4210
- myValidator1: true,
4211
- myValidator2: true,
4212
- },
4213
- },
4214
- },
4215
- });
4216
- const throwable = () =>
4217
- dbs.getService(ModelDataValidator).validate('model', {
4218
- foo: 'test',
4219
- });
4220
- expect(throwable).to.throw('My error');
4221
- expect(called).to.be.eq(1);
4222
- });
4223
-
4224
- it('should throw the error from the second validator', function () {
4225
- let called = 0;
4226
- const dbs = new DatabaseSchema();
4227
- const reg = dbs.getService(PropertyValidatorRegistry);
4228
- reg.addValidator('myValidator1', function () {
4229
- called++;
4230
- return true;
4231
- });
4232
- reg.addValidator('myValidator2', function () {
4233
- called++;
4234
- throw Error('My error');
4235
- });
4236
- dbs.defineModel({
4237
- name: 'model',
4238
- properties: {
4239
- foo: {
4240
- type: DataType.ANY,
4241
- validate: {
4242
- myValidator1: true,
4243
- myValidator2: true,
4244
- },
4245
- },
4246
- },
4247
- });
4248
- const throwable = () =>
4249
- dbs.getService(ModelDataValidator).validate('model', {
4250
- foo: 'test',
4251
- });
4252
- expect(throwable).to.throw('My error');
4253
- expect(called).to.be.eq(2);
4254
- });
4255
-
4256
- it('should allow the given value if validators returns true', function () {
4257
- let called = 0;
4258
- const dbs = new DatabaseSchema();
4259
- const reg = dbs.getService(PropertyValidatorRegistry);
4260
- reg.addValidator('myValidator1', function () {
4261
- called++;
4262
- return true;
4263
- });
4264
- reg.addValidator('myValidator2', function () {
4265
- called++;
4266
- return true;
4267
- });
4268
- dbs.defineModel({
4269
- name: 'model',
4270
- properties: {
4271
- foo: {
4272
- type: DataType.ANY,
4273
- validate: {
4274
- myValidator1: true,
4275
- myValidator2: true,
4276
- },
4277
- },
4278
- },
4279
- });
4280
- dbs.getService(ModelDataValidator).validate('model', {
4281
- foo: 'test',
4282
- });
4283
- expect(called).to.be.eq(2);
4284
- });
4285
-
4286
- it('should throw the error if the first validator returns a promise', function () {
4287
- let called = 0;
4288
- const dbs = new DatabaseSchema();
4289
- const reg = dbs.getService(PropertyValidatorRegistry);
4290
- reg.addValidator('myValidator1', function () {
4291
- called++;
4292
- return Promise.resolve(true);
4293
- });
4294
- reg.addValidator('myValidator2', function () {
4295
- called++;
4296
- return true;
4297
- });
4298
- dbs.defineModel({
4299
- name: 'model',
4300
- properties: {
4301
- foo: {
4302
- type: DataType.ANY,
4303
- validate: {
4304
- myValidator1: true,
4305
- myValidator2: true,
4306
- },
4307
- },
4308
- },
4309
- });
4310
- const throwable = () =>
4311
- dbs.getService(ModelDataValidator).validate('model', {
4312
- foo: 'test',
4313
- });
4314
- expect(throwable).to.throw(
4315
- 'Asynchronous property validators are not supported, ' +
4316
- 'but the property "foo" of the model "model" has the property ' +
4317
- 'validator "myValidator1" that returns a Promise.',
4318
- );
4319
- expect(called).to.be.eq(1);
4320
- });
4321
-
4322
- it('should throw the error if the second validator returns a promise', function () {
4323
- let called = 0;
4324
- const dbs = new DatabaseSchema();
4325
- const reg = dbs.getService(PropertyValidatorRegistry);
4326
- reg.addValidator('myValidator1', function () {
4327
- called++;
4328
- return true;
4329
- });
4330
- reg.addValidator('myValidator2', function () {
4331
- called++;
4332
- return Promise.resolve(true);
4333
- });
4334
- dbs.defineModel({
4335
- name: 'model',
4336
- properties: {
4337
- foo: {
4338
- type: DataType.ANY,
4339
- validate: {
4340
- myValidator1: true,
4341
- myValidator2: true,
4342
- },
4343
- },
4344
- },
4345
- });
4346
- const throwable = () =>
4347
- dbs.getService(ModelDataValidator).validate('model', {
4348
- foo: 'test',
4349
- });
4350
- expect(throwable).to.throw(
4351
- 'Asynchronous property validators are not supported, ' +
4352
- 'but the property "foo" of the model "model" has the property ' +
4353
- 'validator "myValidator2" that returns a Promise.',
4354
- );
4355
- expect(called).to.be.eq(2);
4356
- });
4357
-
4358
- it('should throw the error for a non-true result from the first validator', function () {
4359
- const testFn = v => {
4360
- let called = 0;
4361
- const dbs = new DatabaseSchema();
4362
- const reg = dbs.getService(PropertyValidatorRegistry);
4363
- reg.addValidator('myValidator1', function () {
4364
- called++;
4365
- return v;
4366
- });
4367
- reg.addValidator('myValidator2', function () {
4368
- called++;
4369
- return true;
4370
- });
4371
- dbs.defineModel({
4372
- name: 'model',
4373
- properties: {
4374
- foo: {
4375
- type: DataType.ANY,
4376
- validate: {
4377
- myValidator1: true,
4378
- myValidator2: true,
4379
- },
4380
- },
4381
- },
4382
- });
4383
- const throwable = () =>
4384
- dbs.getService(ModelDataValidator).validate('model', {
4385
- foo: 'test',
4386
- });
4387
- expect(throwable).to.throw(
4388
- 'The property "foo" of the model "model" has the invalid value "test" ' +
4389
- 'that caught by the property validator "myValidator1".',
4390
- );
4391
- expect(called).to.be.eq(1);
4392
- };
4393
- testFn('str');
4394
- testFn('');
4395
- testFn(10);
4396
- testFn(0);
4397
- testFn(false);
4398
- testFn(undefined);
4399
- testFn(null);
4400
- testFn({});
4401
- testFn([]);
4402
- testFn(() => undefined);
4403
- });
4404
-
4405
- it('should throw the error for a non-true result from the second validator', function () {
4406
- const testFn = v => {
4407
- let called = 0;
4408
- const dbs = new DatabaseSchema();
4409
- const reg = dbs.getService(PropertyValidatorRegistry);
4410
- reg.addValidator('myValidator1', function () {
4411
- called++;
4412
- return true;
4413
- });
4414
- reg.addValidator('myValidator2', function () {
4415
- called++;
4416
- return v;
4417
- });
4418
- dbs.defineModel({
4419
- name: 'model',
4420
- properties: {
4421
- foo: {
4422
- type: DataType.ANY,
4423
- validate: {
4424
- myValidator1: true,
4425
- myValidator2: true,
4426
- },
4427
- },
4428
- },
4429
- });
4430
- const throwable = () =>
4431
- dbs.getService(ModelDataValidator).validate('model', {
4432
- foo: 'test',
4433
- });
4434
- expect(throwable).to.throw(
4435
- 'The property "foo" of the model "model" has the invalid value "test" ' +
4436
- 'that caught by the property validator "myValidator2".',
4437
- );
4438
- expect(called).to.be.eq(2);
4439
- };
4440
- testFn('str');
4441
- testFn('');
4442
- testFn(10);
4443
- testFn(0);
4444
- testFn(false);
4445
- testFn(undefined);
4446
- testFn(null);
4447
- testFn({});
4448
- testFn([]);
4449
- testFn(() => undefined);
4450
- });
4451
-
4452
- it('should pass arguments to validators', function () {
4453
- let called = false;
4454
- const dbs = new DatabaseSchema();
4455
- const reg = dbs.getService(PropertyValidatorRegistry);
4456
- reg.addValidator('myValidator1', function (value, options, context) {
4457
- expect(value).to.be.eq('test');
4458
- expect(options).to.be.eq('foo');
4459
- expect(context).to.be.eql({
4460
- validatorName: 'myValidator1',
4461
- modelName: 'model',
4462
- propName: 'foo',
4463
- });
4464
- called++;
4465
- return true;
4466
- });
4467
- reg.addValidator('myValidator2', function (value, options, context) {
4468
- expect(value).to.be.eq('test');
4469
- expect(options).to.be.eq('bar');
4470
- expect(context).to.be.eql({
4471
- validatorName: 'myValidator2',
4472
- modelName: 'model',
4473
- propName: 'foo',
4474
- });
4475
- called++;
4476
- return true;
4477
- });
4478
- dbs.defineModel({
4479
- name: 'model',
4480
- properties: {
4481
- foo: {
4482
- type: DataType.ANY,
4483
- validate: {
4484
- myValidator1: 'foo',
4485
- myValidator2: 'bar',
4486
- },
4487
- },
4488
- },
4489
- });
4490
- dbs.getService(ModelDataValidator).validate('model', {
4491
- foo: 'test',
4492
- });
4493
- expect(called).to.be.eq(2);
4494
- });
4495
-
4496
- it('should invoke validators in the correct order', function () {
4497
- const invocation = [];
4498
- const dbs = new DatabaseSchema();
4499
- const reg = dbs.getService(PropertyValidatorRegistry);
4500
- reg.addValidator('myValidator1', function () {
4501
- invocation.push('myValidator1');
4502
- return true;
4503
- });
4504
- reg.addValidator('myValidator2', function () {
4505
- invocation.push('myValidator2');
4506
- return true;
4507
- });
4508
- dbs.defineModel({
4509
- name: 'model',
4510
- properties: {
4511
- foo: {
4512
- type: DataType.ANY,
4513
- validate: {
4514
- myValidator1: true,
4515
- myValidator2: true,
4516
- },
4517
- },
4518
- },
4519
- });
4520
- dbs.getService(ModelDataValidator).validate('model', {
4521
- foo: 'test',
4522
- });
4523
- expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
4524
- });
4525
- });
4526
- });
4527
- });
4528
- });