@e22m4u/js-repository 0.3.3 → 0.5.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.
- package/README.md +479 -61
- package/dist/cjs/index.cjs +354 -277
- package/package.json +7 -7
- package/src/adapter/adapter-loader.js +1 -1
- package/src/adapter/adapter-loader.spec.js +1 -1
- package/src/adapter/decorator/data-sanitizing-decorator.js +1 -1
- package/src/adapter/decorator/data-transformation-decorator.js +1 -1
- package/src/adapter/decorator/data-validation-decorator.js +1 -1
- package/src/adapter/decorator/default-values-decorator.js +1 -1
- package/src/adapter/decorator/fields-filtering-decorator.js +1 -1
- package/src/adapter/decorator/inclusion-decorator.js +1 -1
- package/src/adapter/decorator/property-uniqueness-decorator.js +1 -1
- package/src/definition/datasource/datasource-definition-validator.js +3 -3
- package/src/definition/datasource/datasource-definition-validator.spec.js +3 -3
- package/src/definition/definition-registry.js +12 -6
- package/src/definition/definition-registry.spec.js +173 -46
- package/src/definition/model/model-data-sanitizer.js +2 -2
- package/src/definition/model/model-data-transformer.js +71 -13
- package/src/definition/model/model-data-transformer.spec.js +2073 -407
- package/src/definition/model/model-data-validator.js +125 -37
- package/src/definition/model/model-data-validator.spec.js +2138 -440
- package/src/definition/model/model-definition-utils.js +5 -5
- package/src/definition/model/model-definition-utils.spec.js +7 -7
- package/src/definition/model/model-definition-validator.js +7 -7
- package/src/definition/model/model-definition-validator.spec.js +10 -7
- package/src/definition/model/properties/properties-definition-validator.js +129 -54
- package/src/definition/model/properties/properties-definition-validator.spec.js +85 -25
- package/src/definition/model/properties/property-transformer/builtin/index.d.ts +0 -1
- package/src/definition/model/properties/property-transformer/builtin/index.js +0 -1
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.d.ts +1 -1
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.js +1 -1
- package/src/definition/model/properties/property-transformer/builtin/to-lower-case-transformer.spec.js +1 -1
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.d.ts +1 -1
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.js +1 -1
- package/src/definition/model/properties/property-transformer/builtin/to-upper-case-transformer.spec.js +1 -1
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.js +1 -1
- package/src/definition/model/properties/property-transformer/builtin/trim-transformer.spec.js +1 -1
- package/src/definition/model/properties/property-transformer/property-transformer-registry.js +2 -4
- package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +2 -4
- package/src/definition/model/properties/property-transformer/property-transformer.d.ts +2 -2
- package/src/definition/model/properties/property-uniqueness-validator.js +4 -4
- package/src/definition/model/properties/property-uniqueness-validator.spec.js +4 -4
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.d.ts +1 -1
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.js +2 -2
- package/src/definition/model/properties/property-validator/builtin/max-length-validator.spec.js +2 -2
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.d.ts +1 -1
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.js +2 -2
- package/src/definition/model/properties/property-validator/builtin/min-length-validator.spec.js +2 -2
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.d.ts +1 -1
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.js +2 -2
- package/src/definition/model/properties/property-validator/builtin/regexp-validator.spec.js +2 -2
- package/src/definition/model/properties/property-validator/property-validator-registry.js +2 -2
- package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +2 -2
- package/src/definition/model/properties/property-validator/property-validator.d.ts +2 -2
- package/src/definition/model/relations/relations-definition-validator.js +23 -23
- package/src/definition/model/relations/relations-definition-validator.spec.js +24 -24
- package/src/errors/invalid-operator-value-error.js +1 -1
- package/src/errors/invalid-operator-value-error.spec.js +1 -1
- package/src/filter/fields-clause-tool.js +5 -5
- package/src/filter/fields-clause-tool.spec.js +16 -16
- package/src/filter/include-clause-tool.js +6 -6
- package/src/filter/include-clause-tool.spec.js +2 -2
- package/src/filter/operator-clause-tool.js +13 -13
- package/src/filter/operator-clause-tool.spec.js +13 -13
- package/src/filter/order-clause-tool.js +3 -3
- package/src/filter/order-clause-tool.spec.js +4 -4
- package/src/filter/slice-clause-tool.js +5 -5
- package/src/filter/slice-clause-tool.spec.js +5 -5
- package/src/filter/where-clause-tool.js +4 -4
- package/src/filter/where-clause-tool.spec.js +3 -3
- package/src/relations/belongs-to-resolver.js +14 -14
- package/src/relations/belongs-to-resolver.spec.js +14 -14
- package/src/relations/has-many-resolver.js +22 -22
- package/src/relations/has-many-resolver.spec.js +23 -23
- package/src/relations/has-one-resolver.js +22 -22
- package/src/relations/has-one-resolver.spec.js +23 -23
- package/src/relations/references-many-resolver.js +7 -7
- package/src/relations/references-many-resolver.spec.js +7 -7
- package/src/repository/repository-registry.js +5 -3
- package/src/repository/repository-registry.spec.js +39 -9
- package/src/utils/exclude-object-keys.js +1 -1
- package/src/utils/exclude-object-keys.spec.js +1 -1
- package/src/utils/index.d.ts +1 -0
- package/src/utils/index.js +1 -0
- package/src/utils/model-name-to-model-key.d.ts +6 -0
- package/src/utils/model-name-to-model-key.js +17 -0
- package/src/utils/model-name-to-model-key.spec.js +92 -0
- package/src/utils/select-object-keys.js +3 -3
- package/src/utils/select-object-keys.spec.js +3 -3
- package/src/definition/model/properties/property-transformer/builtin/to-title-case-transformer.d.ts +0 -6
- package/src/definition/model/properties/property-transformer/builtin/to-title-case-transformer.js +0 -22
- package/src/definition/model/properties/property-transformer/builtin/to-title-case-transformer.spec.js +0 -41
|
@@ -26,7 +26,7 @@ describe('ModelDataValidator', function () {
|
|
|
26
26
|
};
|
|
27
27
|
const error = v =>
|
|
28
28
|
format(
|
|
29
|
-
'The data of the model "model" should be an Object, but %s given.',
|
|
29
|
+
'The data of the model "model" should be an Object, but %s was given.',
|
|
30
30
|
v,
|
|
31
31
|
);
|
|
32
32
|
expect(throwable('str')).to.throw(error('"str"'));
|
|
@@ -54,7 +54,7 @@ describe('ModelDataValidator', function () {
|
|
|
54
54
|
dbs.getService(ModelDataValidator).validate('modelB', {foo: 10});
|
|
55
55
|
expect(throwable).to.throw(
|
|
56
56
|
'The property "foo" of the model "modelB" must ' +
|
|
57
|
-
'have a String, but Number given.',
|
|
57
|
+
'have a String, but Number was given.',
|
|
58
58
|
);
|
|
59
59
|
});
|
|
60
60
|
|
|
@@ -73,7 +73,7 @@ describe('ModelDataValidator', function () {
|
|
|
73
73
|
dbs.getService(ModelDataValidator).validate('model', {});
|
|
74
74
|
expect(throwable).to.throw(
|
|
75
75
|
'The property "foo" of the model "model" ' +
|
|
76
|
-
'is required, but undefined given.',
|
|
76
|
+
'is required, but undefined was given.',
|
|
77
77
|
);
|
|
78
78
|
});
|
|
79
79
|
|
|
@@ -91,7 +91,7 @@ describe('ModelDataValidator', function () {
|
|
|
91
91
|
const throwable = () =>
|
|
92
92
|
dbs.getService(ModelDataValidator).validate('model', {foo: undefined});
|
|
93
93
|
expect(throwable).to.throw(
|
|
94
|
-
'The property "foo" of the model "model" is required, but undefined given.',
|
|
94
|
+
'The property "foo" of the model "model" is required, but undefined was given.',
|
|
95
95
|
);
|
|
96
96
|
});
|
|
97
97
|
|
|
@@ -109,7 +109,7 @@ describe('ModelDataValidator', function () {
|
|
|
109
109
|
const throwable = () =>
|
|
110
110
|
dbs.getService(ModelDataValidator).validate('model', {foo: null});
|
|
111
111
|
expect(throwable).to.throw(
|
|
112
|
-
'The property "foo" of the model "model" is required, but null given.',
|
|
112
|
+
'The property "foo" of the model "model" is required, but null was given.',
|
|
113
113
|
);
|
|
114
114
|
});
|
|
115
115
|
|
|
@@ -131,7 +131,32 @@ describe('ModelDataValidator', function () {
|
|
|
131
131
|
dbs.getService(ModelDataValidator).validate('model', {foo: 'empty'});
|
|
132
132
|
expect(throwable).to.throw(
|
|
133
133
|
'The property "foo" of the model "model" ' +
|
|
134
|
-
'is required, but "empty" given.',
|
|
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.',
|
|
135
160
|
);
|
|
136
161
|
});
|
|
137
162
|
|
|
@@ -167,7 +192,7 @@ describe('ModelDataValidator', function () {
|
|
|
167
192
|
.validate('model', {foo: undefined}, true);
|
|
168
193
|
expect(throwable).to.throw(
|
|
169
194
|
'The property "foo" of the model "model" ' +
|
|
170
|
-
'is required, but undefined given.',
|
|
195
|
+
'is required, but undefined was given.',
|
|
171
196
|
);
|
|
172
197
|
});
|
|
173
198
|
|
|
@@ -187,7 +212,7 @@ describe('ModelDataValidator', function () {
|
|
|
187
212
|
.getService(ModelDataValidator)
|
|
188
213
|
.validate('model', {foo: null}, true);
|
|
189
214
|
expect(throwable).to.throw(
|
|
190
|
-
'The property "foo" of the model "model" is required, but null given.',
|
|
215
|
+
'The property "foo" of the model "model" is required, but null was given.',
|
|
191
216
|
);
|
|
192
217
|
});
|
|
193
218
|
|
|
@@ -208,13 +233,13 @@ describe('ModelDataValidator', function () {
|
|
|
208
233
|
const throwable = () =>
|
|
209
234
|
dbs.getService(ModelDataValidator).validate('model', {foo: 5}, true);
|
|
210
235
|
expect(throwable).to.throw(
|
|
211
|
-
'The property "foo" of the model "model" is required, but 5 given.',
|
|
236
|
+
'The property "foo" of the model "model" is required, but 5 was given.',
|
|
212
237
|
);
|
|
213
238
|
});
|
|
214
239
|
});
|
|
215
240
|
|
|
216
241
|
describe('validate by property type', function () {
|
|
217
|
-
it('
|
|
242
|
+
it('should not validate the empty value', function () {
|
|
218
243
|
const dbs = new DatabaseSchema();
|
|
219
244
|
dbs.defineModel({
|
|
220
245
|
name: 'model',
|
|
@@ -536,7 +561,7 @@ describe('ModelDataValidator', function () {
|
|
|
536
561
|
});
|
|
537
562
|
expect(throwable).to.throw(
|
|
538
563
|
'The property "foo" of the model "model" must have ' +
|
|
539
|
-
'a String, but Number given.',
|
|
564
|
+
'a String, but Number was given.',
|
|
540
565
|
);
|
|
541
566
|
});
|
|
542
567
|
|
|
@@ -555,7 +580,7 @@ describe('ModelDataValidator', function () {
|
|
|
555
580
|
});
|
|
556
581
|
expect(throwable).to.throw(
|
|
557
582
|
'The property "foo" of the model "model" must have ' +
|
|
558
|
-
'a String, but Boolean given.',
|
|
583
|
+
'a String, but Boolean was given.',
|
|
559
584
|
);
|
|
560
585
|
});
|
|
561
586
|
|
|
@@ -574,7 +599,7 @@ describe('ModelDataValidator', function () {
|
|
|
574
599
|
});
|
|
575
600
|
expect(throwable).to.throw(
|
|
576
601
|
'The property "foo" of the model "model" must have ' +
|
|
577
|
-
'a String, but Boolean given.',
|
|
602
|
+
'a String, but Boolean was given.',
|
|
578
603
|
);
|
|
579
604
|
});
|
|
580
605
|
|
|
@@ -593,7 +618,7 @@ describe('ModelDataValidator', function () {
|
|
|
593
618
|
});
|
|
594
619
|
expect(throwable).to.throw(
|
|
595
620
|
'The property "foo" of the model "model" must have ' +
|
|
596
|
-
'a String, but Array given.',
|
|
621
|
+
'a String, but Array was given.',
|
|
597
622
|
);
|
|
598
623
|
});
|
|
599
624
|
|
|
@@ -612,7 +637,7 @@ describe('ModelDataValidator', function () {
|
|
|
612
637
|
});
|
|
613
638
|
expect(throwable).to.throw(
|
|
614
639
|
'The property "foo" of the model "model" must have ' +
|
|
615
|
-
'a String, but Object given.',
|
|
640
|
+
'a String, but Object was given.',
|
|
616
641
|
);
|
|
617
642
|
});
|
|
618
643
|
});
|
|
@@ -683,7 +708,7 @@ describe('ModelDataValidator', function () {
|
|
|
683
708
|
});
|
|
684
709
|
expect(throwable).to.throw(
|
|
685
710
|
'The property "foo" of the model "model" must have ' +
|
|
686
|
-
'a String, but Number given.',
|
|
711
|
+
'a String, but Number was given.',
|
|
687
712
|
);
|
|
688
713
|
});
|
|
689
714
|
|
|
@@ -704,7 +729,7 @@ describe('ModelDataValidator', function () {
|
|
|
704
729
|
});
|
|
705
730
|
expect(throwable).to.throw(
|
|
706
731
|
'The property "foo" of the model "model" must have ' +
|
|
707
|
-
'a String, but Boolean given.',
|
|
732
|
+
'a String, but Boolean was given.',
|
|
708
733
|
);
|
|
709
734
|
});
|
|
710
735
|
|
|
@@ -725,7 +750,7 @@ describe('ModelDataValidator', function () {
|
|
|
725
750
|
});
|
|
726
751
|
expect(throwable).to.throw(
|
|
727
752
|
'The property "foo" of the model "model" must have ' +
|
|
728
|
-
'a String, but Boolean given.',
|
|
753
|
+
'a String, but Boolean was given.',
|
|
729
754
|
);
|
|
730
755
|
});
|
|
731
756
|
|
|
@@ -746,7 +771,7 @@ describe('ModelDataValidator', function () {
|
|
|
746
771
|
});
|
|
747
772
|
expect(throwable).to.throw(
|
|
748
773
|
'The property "foo" of the model "model" must have ' +
|
|
749
|
-
'a String, but Array given.',
|
|
774
|
+
'a String, but Array was given.',
|
|
750
775
|
);
|
|
751
776
|
});
|
|
752
777
|
|
|
@@ -767,7 +792,7 @@ describe('ModelDataValidator', function () {
|
|
|
767
792
|
});
|
|
768
793
|
expect(throwable).to.throw(
|
|
769
794
|
'The property "foo" of the model "model" must have ' +
|
|
770
|
-
'a String, but Object given.',
|
|
795
|
+
'a String, but Object was given.',
|
|
771
796
|
);
|
|
772
797
|
});
|
|
773
798
|
});
|
|
@@ -818,7 +843,7 @@ describe('ModelDataValidator', function () {
|
|
|
818
843
|
});
|
|
819
844
|
expect(throwable).to.throw(
|
|
820
845
|
'The property "foo" of the model "model" must have ' +
|
|
821
|
-
'a Number, but String given.',
|
|
846
|
+
'a Number, but String was given.',
|
|
822
847
|
);
|
|
823
848
|
});
|
|
824
849
|
|
|
@@ -851,7 +876,7 @@ describe('ModelDataValidator', function () {
|
|
|
851
876
|
});
|
|
852
877
|
expect(throwable).to.throw(
|
|
853
878
|
'The property "foo" of the model "model" must have ' +
|
|
854
|
-
'a Number, but Boolean given.',
|
|
879
|
+
'a Number, but Boolean was given.',
|
|
855
880
|
);
|
|
856
881
|
});
|
|
857
882
|
|
|
@@ -870,7 +895,7 @@ describe('ModelDataValidator', function () {
|
|
|
870
895
|
});
|
|
871
896
|
expect(throwable).to.throw(
|
|
872
897
|
'The property "foo" of the model "model" must have ' +
|
|
873
|
-
'a Number, but Boolean given.',
|
|
898
|
+
'a Number, but Boolean was given.',
|
|
874
899
|
);
|
|
875
900
|
});
|
|
876
901
|
|
|
@@ -889,7 +914,7 @@ describe('ModelDataValidator', function () {
|
|
|
889
914
|
});
|
|
890
915
|
expect(throwable).to.throw(
|
|
891
916
|
'The property "foo" of the model "model" must have ' +
|
|
892
|
-
'a Number, but Array given.',
|
|
917
|
+
'a Number, but Array was given.',
|
|
893
918
|
);
|
|
894
919
|
});
|
|
895
920
|
|
|
@@ -908,7 +933,7 @@ describe('ModelDataValidator', function () {
|
|
|
908
933
|
});
|
|
909
934
|
expect(throwable).to.throw(
|
|
910
935
|
'The property "foo" of the model "model" must have ' +
|
|
911
|
-
'a Number, but Object given.',
|
|
936
|
+
'a Number, but Object was given.',
|
|
912
937
|
);
|
|
913
938
|
});
|
|
914
939
|
});
|
|
@@ -963,7 +988,7 @@ describe('ModelDataValidator', function () {
|
|
|
963
988
|
});
|
|
964
989
|
expect(throwable).to.throw(
|
|
965
990
|
'The property "foo" of the model "model" must have ' +
|
|
966
|
-
'a Number, but String given.',
|
|
991
|
+
'a Number, but String was given.',
|
|
967
992
|
);
|
|
968
993
|
});
|
|
969
994
|
|
|
@@ -1000,7 +1025,7 @@ describe('ModelDataValidator', function () {
|
|
|
1000
1025
|
});
|
|
1001
1026
|
expect(throwable).to.throw(
|
|
1002
1027
|
'The property "foo" of the model "model" must have ' +
|
|
1003
|
-
'a Number, but Boolean given.',
|
|
1028
|
+
'a Number, but Boolean was given.',
|
|
1004
1029
|
);
|
|
1005
1030
|
});
|
|
1006
1031
|
|
|
@@ -1021,7 +1046,7 @@ describe('ModelDataValidator', function () {
|
|
|
1021
1046
|
});
|
|
1022
1047
|
expect(throwable).to.throw(
|
|
1023
1048
|
'The property "foo" of the model "model" must have ' +
|
|
1024
|
-
'a Number, but Boolean given.',
|
|
1049
|
+
'a Number, but Boolean was given.',
|
|
1025
1050
|
);
|
|
1026
1051
|
});
|
|
1027
1052
|
|
|
@@ -1042,7 +1067,7 @@ describe('ModelDataValidator', function () {
|
|
|
1042
1067
|
});
|
|
1043
1068
|
expect(throwable).to.throw(
|
|
1044
1069
|
'The property "foo" of the model "model" must have ' +
|
|
1045
|
-
'a Number, but Array given.',
|
|
1070
|
+
'a Number, but Array was given.',
|
|
1046
1071
|
);
|
|
1047
1072
|
});
|
|
1048
1073
|
|
|
@@ -1063,7 +1088,7 @@ describe('ModelDataValidator', function () {
|
|
|
1063
1088
|
});
|
|
1064
1089
|
expect(throwable).to.throw(
|
|
1065
1090
|
'The property "foo" of the model "model" must have ' +
|
|
1066
|
-
'a Number, but Object given.',
|
|
1091
|
+
'a Number, but Object was given.',
|
|
1067
1092
|
);
|
|
1068
1093
|
});
|
|
1069
1094
|
});
|
|
@@ -1114,7 +1139,7 @@ describe('ModelDataValidator', function () {
|
|
|
1114
1139
|
});
|
|
1115
1140
|
expect(throwable).to.throw(
|
|
1116
1141
|
'The property "foo" of the model "model" must have ' +
|
|
1117
|
-
'a Boolean, but String given.',
|
|
1142
|
+
'a Boolean, but String was given.',
|
|
1118
1143
|
);
|
|
1119
1144
|
});
|
|
1120
1145
|
|
|
@@ -1133,7 +1158,7 @@ describe('ModelDataValidator', function () {
|
|
|
1133
1158
|
});
|
|
1134
1159
|
expect(throwable).to.throw(
|
|
1135
1160
|
'The property "foo" of the model "model" must have ' +
|
|
1136
|
-
'a Boolean, but Number given.',
|
|
1161
|
+
'a Boolean, but Number was given.',
|
|
1137
1162
|
);
|
|
1138
1163
|
});
|
|
1139
1164
|
|
|
@@ -1180,7 +1205,7 @@ describe('ModelDataValidator', function () {
|
|
|
1180
1205
|
});
|
|
1181
1206
|
expect(throwable).to.throw(
|
|
1182
1207
|
'The property "foo" of the model "model" must have ' +
|
|
1183
|
-
'a Boolean, but Array given.',
|
|
1208
|
+
'a Boolean, but Array was given.',
|
|
1184
1209
|
);
|
|
1185
1210
|
});
|
|
1186
1211
|
|
|
@@ -1199,7 +1224,7 @@ describe('ModelDataValidator', function () {
|
|
|
1199
1224
|
});
|
|
1200
1225
|
expect(throwable).to.throw(
|
|
1201
1226
|
'The property "foo" of the model "model" must have ' +
|
|
1202
|
-
'a Boolean, but Object given.',
|
|
1227
|
+
'a Boolean, but Object was given.',
|
|
1203
1228
|
);
|
|
1204
1229
|
});
|
|
1205
1230
|
});
|
|
@@ -1254,7 +1279,7 @@ describe('ModelDataValidator', function () {
|
|
|
1254
1279
|
});
|
|
1255
1280
|
expect(throwable).to.throw(
|
|
1256
1281
|
'The property "foo" of the model "model" must have ' +
|
|
1257
|
-
'a Boolean, but String given.',
|
|
1282
|
+
'a Boolean, but String was given.',
|
|
1258
1283
|
);
|
|
1259
1284
|
});
|
|
1260
1285
|
|
|
@@ -1275,7 +1300,7 @@ describe('ModelDataValidator', function () {
|
|
|
1275
1300
|
});
|
|
1276
1301
|
expect(throwable).to.throw(
|
|
1277
1302
|
'The property "foo" of the model "model" must have ' +
|
|
1278
|
-
'a Boolean, but Number given.',
|
|
1303
|
+
'a Boolean, but Number was given.',
|
|
1279
1304
|
);
|
|
1280
1305
|
});
|
|
1281
1306
|
|
|
@@ -1328,7 +1353,7 @@ describe('ModelDataValidator', function () {
|
|
|
1328
1353
|
});
|
|
1329
1354
|
expect(throwable).to.throw(
|
|
1330
1355
|
'The property "foo" of the model "model" must have ' +
|
|
1331
|
-
'a Boolean, but Array given.',
|
|
1356
|
+
'a Boolean, but Array was given.',
|
|
1332
1357
|
);
|
|
1333
1358
|
});
|
|
1334
1359
|
|
|
@@ -1349,7 +1374,7 @@ describe('ModelDataValidator', function () {
|
|
|
1349
1374
|
});
|
|
1350
1375
|
expect(throwable).to.throw(
|
|
1351
1376
|
'The property "foo" of the model "model" must have ' +
|
|
1352
|
-
'a Boolean, but Object given.',
|
|
1377
|
+
'a Boolean, but Object was given.',
|
|
1353
1378
|
);
|
|
1354
1379
|
});
|
|
1355
1380
|
});
|
|
@@ -1400,7 +1425,7 @@ describe('ModelDataValidator', function () {
|
|
|
1400
1425
|
});
|
|
1401
1426
|
expect(throwable).to.throw(
|
|
1402
1427
|
'The property "foo" of the model "model" must have ' +
|
|
1403
|
-
'an Array, but String given.',
|
|
1428
|
+
'an Array, but String was given.',
|
|
1404
1429
|
);
|
|
1405
1430
|
});
|
|
1406
1431
|
|
|
@@ -1419,7 +1444,7 @@ describe('ModelDataValidator', function () {
|
|
|
1419
1444
|
});
|
|
1420
1445
|
expect(throwable).to.throw(
|
|
1421
1446
|
'The property "foo" of the model "model" must have ' +
|
|
1422
|
-
'an Array, but Number given.',
|
|
1447
|
+
'an Array, but Number was given.',
|
|
1423
1448
|
);
|
|
1424
1449
|
});
|
|
1425
1450
|
|
|
@@ -1438,7 +1463,7 @@ describe('ModelDataValidator', function () {
|
|
|
1438
1463
|
});
|
|
1439
1464
|
expect(throwable).to.throw(
|
|
1440
1465
|
'The property "foo" of the model "model" must have ' +
|
|
1441
|
-
'an Array, but Boolean given.',
|
|
1466
|
+
'an Array, but Boolean was given.',
|
|
1442
1467
|
);
|
|
1443
1468
|
});
|
|
1444
1469
|
|
|
@@ -1457,7 +1482,7 @@ describe('ModelDataValidator', function () {
|
|
|
1457
1482
|
});
|
|
1458
1483
|
expect(throwable).to.throw(
|
|
1459
1484
|
'The property "foo" of the model "model" must have ' +
|
|
1460
|
-
'an Array, but Boolean given.',
|
|
1485
|
+
'an Array, but Boolean was given.',
|
|
1461
1486
|
);
|
|
1462
1487
|
});
|
|
1463
1488
|
|
|
@@ -1490,7 +1515,7 @@ describe('ModelDataValidator', function () {
|
|
|
1490
1515
|
});
|
|
1491
1516
|
expect(throwable).to.throw(
|
|
1492
1517
|
'The property "foo" of the model "model" must have ' +
|
|
1493
|
-
'an Array, but Object given.',
|
|
1518
|
+
'an Array, but Object was given.',
|
|
1494
1519
|
);
|
|
1495
1520
|
});
|
|
1496
1521
|
});
|
|
@@ -1545,7 +1570,7 @@ describe('ModelDataValidator', function () {
|
|
|
1545
1570
|
});
|
|
1546
1571
|
expect(throwable).to.throw(
|
|
1547
1572
|
'The property "foo" of the model "model" must have ' +
|
|
1548
|
-
'an Array, but String given.',
|
|
1573
|
+
'an Array, but String was given.',
|
|
1549
1574
|
);
|
|
1550
1575
|
});
|
|
1551
1576
|
|
|
@@ -1566,7 +1591,7 @@ describe('ModelDataValidator', function () {
|
|
|
1566
1591
|
});
|
|
1567
1592
|
expect(throwable).to.throw(
|
|
1568
1593
|
'The property "foo" of the model "model" must have ' +
|
|
1569
|
-
'an Array, but Number given.',
|
|
1594
|
+
'an Array, but Number was given.',
|
|
1570
1595
|
);
|
|
1571
1596
|
});
|
|
1572
1597
|
|
|
@@ -1587,7 +1612,7 @@ describe('ModelDataValidator', function () {
|
|
|
1587
1612
|
});
|
|
1588
1613
|
expect(throwable).to.throw(
|
|
1589
1614
|
'The property "foo" of the model "model" must have ' +
|
|
1590
|
-
'an Array, but Boolean given.',
|
|
1615
|
+
'an Array, but Boolean was given.',
|
|
1591
1616
|
);
|
|
1592
1617
|
});
|
|
1593
1618
|
|
|
@@ -1608,7 +1633,7 @@ describe('ModelDataValidator', function () {
|
|
|
1608
1633
|
});
|
|
1609
1634
|
expect(throwable).to.throw(
|
|
1610
1635
|
'The property "foo" of the model "model" must have ' +
|
|
1611
|
-
'an Array, but Boolean given.',
|
|
1636
|
+
'an Array, but Boolean was given.',
|
|
1612
1637
|
);
|
|
1613
1638
|
});
|
|
1614
1639
|
|
|
@@ -1645,7 +1670,7 @@ describe('ModelDataValidator', function () {
|
|
|
1645
1670
|
});
|
|
1646
1671
|
expect(throwable).to.throw(
|
|
1647
1672
|
'The property "foo" of the model "model" must have ' +
|
|
1648
|
-
'an Array, but Object given.',
|
|
1673
|
+
'an Array, but Object was given.',
|
|
1649
1674
|
);
|
|
1650
1675
|
});
|
|
1651
1676
|
|
|
@@ -1690,7 +1715,7 @@ describe('ModelDataValidator', function () {
|
|
|
1690
1715
|
});
|
|
1691
1716
|
expect(throwable).to.throw(
|
|
1692
1717
|
'The property "foo" of the model "modelA" must have ' +
|
|
1693
|
-
'a String, but Number given.',
|
|
1718
|
+
'a String, but Number was given.',
|
|
1694
1719
|
);
|
|
1695
1720
|
});
|
|
1696
1721
|
|
|
@@ -1766,7 +1791,7 @@ describe('ModelDataValidator', function () {
|
|
|
1766
1791
|
});
|
|
1767
1792
|
expect(throwable).to.throw(
|
|
1768
1793
|
'The property "foo" of the model "model" must have ' +
|
|
1769
|
-
'an Object, but String given.',
|
|
1794
|
+
'an Object, but String was given.',
|
|
1770
1795
|
);
|
|
1771
1796
|
});
|
|
1772
1797
|
|
|
@@ -1785,7 +1810,7 @@ describe('ModelDataValidator', function () {
|
|
|
1785
1810
|
});
|
|
1786
1811
|
expect(throwable).to.throw(
|
|
1787
1812
|
'The property "foo" of the model "model" must have ' +
|
|
1788
|
-
'an Object, but Number given.',
|
|
1813
|
+
'an Object, but Number was given.',
|
|
1789
1814
|
);
|
|
1790
1815
|
});
|
|
1791
1816
|
|
|
@@ -1804,7 +1829,7 @@ describe('ModelDataValidator', function () {
|
|
|
1804
1829
|
});
|
|
1805
1830
|
expect(throwable).to.throw(
|
|
1806
1831
|
'The property "foo" of the model "model" must have ' +
|
|
1807
|
-
'an Object, but Boolean given.',
|
|
1832
|
+
'an Object, but Boolean was given.',
|
|
1808
1833
|
);
|
|
1809
1834
|
});
|
|
1810
1835
|
|
|
@@ -1823,7 +1848,7 @@ describe('ModelDataValidator', function () {
|
|
|
1823
1848
|
});
|
|
1824
1849
|
expect(throwable).to.throw(
|
|
1825
1850
|
'The property "foo" of the model "model" must have ' +
|
|
1826
|
-
'an Object, but Boolean given.',
|
|
1851
|
+
'an Object, but Boolean was given.',
|
|
1827
1852
|
);
|
|
1828
1853
|
});
|
|
1829
1854
|
|
|
@@ -1842,7 +1867,7 @@ describe('ModelDataValidator', function () {
|
|
|
1842
1867
|
});
|
|
1843
1868
|
expect(throwable).to.throw(
|
|
1844
1869
|
'The property "foo" of the model "model" must have ' +
|
|
1845
|
-
'an Object, but Array given.',
|
|
1870
|
+
'an Object, but Array was given.',
|
|
1846
1871
|
);
|
|
1847
1872
|
});
|
|
1848
1873
|
|
|
@@ -1911,7 +1936,7 @@ describe('ModelDataValidator', function () {
|
|
|
1911
1936
|
});
|
|
1912
1937
|
expect(throwable).to.throw(
|
|
1913
1938
|
'The property "foo" of the model "model" must have ' +
|
|
1914
|
-
'an Object, but String given.',
|
|
1939
|
+
'an Object, but String was given.',
|
|
1915
1940
|
);
|
|
1916
1941
|
});
|
|
1917
1942
|
|
|
@@ -1932,7 +1957,7 @@ describe('ModelDataValidator', function () {
|
|
|
1932
1957
|
});
|
|
1933
1958
|
expect(throwable).to.throw(
|
|
1934
1959
|
'The property "foo" of the model "model" must have ' +
|
|
1935
|
-
'an Object, but Number given.',
|
|
1960
|
+
'an Object, but Number was given.',
|
|
1936
1961
|
);
|
|
1937
1962
|
});
|
|
1938
1963
|
|
|
@@ -1953,7 +1978,7 @@ describe('ModelDataValidator', function () {
|
|
|
1953
1978
|
});
|
|
1954
1979
|
expect(throwable).to.throw(
|
|
1955
1980
|
'The property "foo" of the model "model" must have ' +
|
|
1956
|
-
'an Object, but Boolean given.',
|
|
1981
|
+
'an Object, but Boolean was given.',
|
|
1957
1982
|
);
|
|
1958
1983
|
});
|
|
1959
1984
|
|
|
@@ -1974,7 +1999,7 @@ describe('ModelDataValidator', function () {
|
|
|
1974
1999
|
});
|
|
1975
2000
|
expect(throwable).to.throw(
|
|
1976
2001
|
'The property "foo" of the model "model" must have ' +
|
|
1977
|
-
'an Object, but Boolean given.',
|
|
2002
|
+
'an Object, but Boolean was given.',
|
|
1978
2003
|
);
|
|
1979
2004
|
});
|
|
1980
2005
|
|
|
@@ -1995,7 +2020,7 @@ describe('ModelDataValidator', function () {
|
|
|
1995
2020
|
});
|
|
1996
2021
|
expect(throwable).to.throw(
|
|
1997
2022
|
'The property "foo" of the model "model" must have ' +
|
|
1998
|
-
'an Object, but Array given.',
|
|
2023
|
+
'an Object, but Array was given.',
|
|
1999
2024
|
);
|
|
2000
2025
|
});
|
|
2001
2026
|
|
|
@@ -2040,7 +2065,7 @@ describe('ModelDataValidator', function () {
|
|
|
2040
2065
|
});
|
|
2041
2066
|
expect(throwable).to.throw(
|
|
2042
2067
|
'The property "foo" of the model "modelA" must have ' +
|
|
2043
|
-
'a String, but Number given.',
|
|
2068
|
+
'a String, but Number was given.',
|
|
2044
2069
|
);
|
|
2045
2070
|
});
|
|
2046
2071
|
|
|
@@ -2072,32 +2097,13 @@ describe('ModelDataValidator', function () {
|
|
|
2072
2097
|
});
|
|
2073
2098
|
|
|
2074
2099
|
describe('validate by property validators', function () {
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
dbs
|
|
2078
|
-
.getService(PropertyValidatorRegistry)
|
|
2079
|
-
.addValidator('myValidator', () => false);
|
|
2080
|
-
dbs.defineModel({
|
|
2081
|
-
name: 'model',
|
|
2082
|
-
properties: {
|
|
2083
|
-
foo: {
|
|
2084
|
-
type: DataType.STRING,
|
|
2085
|
-
validate: 'myValidator',
|
|
2086
|
-
},
|
|
2087
|
-
},
|
|
2088
|
-
});
|
|
2089
|
-
dbs
|
|
2090
|
-
.getService(EmptyValuesService)
|
|
2091
|
-
.setEmptyValuesOf(DataType.STRING, [5]);
|
|
2092
|
-
dbs.getService(ModelDataValidator).validate('model', {foo: 5});
|
|
2093
|
-
});
|
|
2094
|
-
|
|
2095
|
-
describe('the option "validate" with the string value', function () {
|
|
2096
|
-
it('does not validate a property value if it is not provided', function () {
|
|
2100
|
+
describe('when the option "validate" is a String', function () {
|
|
2101
|
+
it('should not validate the non-provided property', function () {
|
|
2097
2102
|
const dbs = new DatabaseSchema();
|
|
2098
|
-
dbs
|
|
2099
|
-
|
|
2100
|
-
|
|
2103
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2104
|
+
reg.addValidator('myValidator', function () {
|
|
2105
|
+
throw new Error('Should not to be called.');
|
|
2106
|
+
});
|
|
2101
2107
|
dbs.defineModel({
|
|
2102
2108
|
name: 'model',
|
|
2103
2109
|
properties: {
|
|
@@ -2111,11 +2117,12 @@ describe('ModelDataValidator', function () {
|
|
|
2111
2117
|
validator.validate('model', {});
|
|
2112
2118
|
});
|
|
2113
2119
|
|
|
2114
|
-
it('
|
|
2120
|
+
it('should not validate undefined and null values', function () {
|
|
2115
2121
|
const dbs = new DatabaseSchema();
|
|
2116
|
-
dbs
|
|
2117
|
-
|
|
2118
|
-
|
|
2122
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2123
|
+
reg.addValidator('myValidator', function () {
|
|
2124
|
+
throw new Error('Should not to be called.');
|
|
2125
|
+
});
|
|
2119
2126
|
dbs.defineModel({
|
|
2120
2127
|
name: 'model',
|
|
2121
2128
|
properties: {
|
|
@@ -2130,14 +2137,55 @@ describe('ModelDataValidator', function () {
|
|
|
2130
2137
|
validator.validate('model', {foo: null});
|
|
2131
2138
|
});
|
|
2132
2139
|
|
|
2133
|
-
it('
|
|
2134
|
-
const myValidator = function () {
|
|
2135
|
-
throw Error('My error');
|
|
2136
|
-
};
|
|
2140
|
+
it('should not validate the empty value', function () {
|
|
2137
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
|
+
});
|
|
2138
2155
|
dbs
|
|
2139
|
-
.getService(
|
|
2140
|
-
.
|
|
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
|
+
});
|
|
2141
2189
|
dbs.defineModel({
|
|
2142
2190
|
name: 'model',
|
|
2143
2191
|
properties: {
|
|
@@ -2154,11 +2202,14 @@ describe('ModelDataValidator', function () {
|
|
|
2154
2202
|
expect(throwable).to.throw('My error');
|
|
2155
2203
|
});
|
|
2156
2204
|
|
|
2157
|
-
it('
|
|
2205
|
+
it('should allow the given value if the validator returns true', function () {
|
|
2206
|
+
let called = 0;
|
|
2158
2207
|
const dbs = new DatabaseSchema();
|
|
2159
|
-
dbs
|
|
2160
|
-
|
|
2161
|
-
|
|
2208
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2209
|
+
reg.addValidator('myValidator', function () {
|
|
2210
|
+
called++;
|
|
2211
|
+
return true;
|
|
2212
|
+
});
|
|
2162
2213
|
dbs.defineModel({
|
|
2163
2214
|
name: 'model',
|
|
2164
2215
|
properties: {
|
|
@@ -2171,13 +2222,17 @@ describe('ModelDataValidator', function () {
|
|
|
2171
2222
|
dbs.getService(ModelDataValidator).validate('model', {
|
|
2172
2223
|
foo: 'test',
|
|
2173
2224
|
});
|
|
2225
|
+
expect(called).to.be.eq(1);
|
|
2174
2226
|
});
|
|
2175
2227
|
|
|
2176
|
-
it('
|
|
2228
|
+
it('should throw the error if the validator returns a promise', function () {
|
|
2229
|
+
let called = 0;
|
|
2177
2230
|
const dbs = new DatabaseSchema();
|
|
2178
|
-
dbs
|
|
2179
|
-
|
|
2180
|
-
|
|
2231
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2232
|
+
reg.addValidator('myValidator', function () {
|
|
2233
|
+
called++;
|
|
2234
|
+
return Promise.resolve(true);
|
|
2235
|
+
});
|
|
2181
2236
|
dbs.defineModel({
|
|
2182
2237
|
name: 'model',
|
|
2183
2238
|
properties: {
|
|
@@ -2193,16 +2248,21 @@ describe('ModelDataValidator', function () {
|
|
|
2193
2248
|
});
|
|
2194
2249
|
expect(throwable).to.throw(
|
|
2195
2250
|
'Asynchronous property validators are not supported, ' +
|
|
2196
|
-
'but the property
|
|
2251
|
+
'but the property "foo" of the model "model" has the property ' +
|
|
2252
|
+
'validator "myValidator" that returns a Promise.',
|
|
2197
2253
|
);
|
|
2254
|
+
expect(called).to.be.eq(1);
|
|
2198
2255
|
});
|
|
2199
2256
|
|
|
2200
|
-
it('
|
|
2257
|
+
it('should throw the error for a non-true result from the validator', function () {
|
|
2201
2258
|
const testFn = v => {
|
|
2259
|
+
let called = 0;
|
|
2202
2260
|
const dbs = new DatabaseSchema();
|
|
2203
|
-
dbs
|
|
2204
|
-
|
|
2205
|
-
|
|
2261
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2262
|
+
reg.addValidator('myValidator', function () {
|
|
2263
|
+
called++;
|
|
2264
|
+
return v;
|
|
2265
|
+
});
|
|
2206
2266
|
dbs.defineModel({
|
|
2207
2267
|
name: 'model',
|
|
2208
2268
|
properties: {
|
|
@@ -2217,9 +2277,10 @@ describe('ModelDataValidator', function () {
|
|
|
2217
2277
|
foo: 'test',
|
|
2218
2278
|
});
|
|
2219
2279
|
expect(throwable).to.throw(
|
|
2220
|
-
'The property "foo" of the model "model" has
|
|
2221
|
-
'that caught by the validator "myValidator".',
|
|
2280
|
+
'The property "foo" of the model "model" has the invalid value "test" ' +
|
|
2281
|
+
'that caught by the property validator "myValidator".',
|
|
2222
2282
|
);
|
|
2283
|
+
expect(called).to.be.eq(1);
|
|
2223
2284
|
};
|
|
2224
2285
|
testFn('str');
|
|
2225
2286
|
testFn('');
|
|
@@ -2233,10 +2294,11 @@ describe('ModelDataValidator', function () {
|
|
|
2233
2294
|
testFn(() => undefined);
|
|
2234
2295
|
});
|
|
2235
2296
|
|
|
2236
|
-
it('
|
|
2237
|
-
let
|
|
2297
|
+
it('should pass arguments to the validator', function () {
|
|
2298
|
+
let called = 0;
|
|
2238
2299
|
const dbs = new DatabaseSchema();
|
|
2239
|
-
const
|
|
2300
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2301
|
+
reg.addValidator('myValidator', function (value, options, context) {
|
|
2240
2302
|
expect(value).to.be.eq('test');
|
|
2241
2303
|
expect(options).to.be.undefined;
|
|
2242
2304
|
expect(context).to.be.eql({
|
|
@@ -2244,12 +2306,9 @@ describe('ModelDataValidator', function () {
|
|
|
2244
2306
|
modelName: 'model',
|
|
2245
2307
|
propName: 'foo',
|
|
2246
2308
|
});
|
|
2247
|
-
|
|
2309
|
+
called++;
|
|
2248
2310
|
return true;
|
|
2249
|
-
};
|
|
2250
|
-
dbs
|
|
2251
|
-
.getService(PropertyValidatorRegistry)
|
|
2252
|
-
.addValidator('myValidator', myValidator);
|
|
2311
|
+
});
|
|
2253
2312
|
dbs.defineModel({
|
|
2254
2313
|
name: 'model',
|
|
2255
2314
|
properties: {
|
|
@@ -2262,19 +2321,17 @@ describe('ModelDataValidator', function () {
|
|
|
2262
2321
|
dbs.getService(ModelDataValidator).validate('model', {
|
|
2263
2322
|
foo: 'test',
|
|
2264
2323
|
});
|
|
2265
|
-
expect(
|
|
2324
|
+
expect(called).to.be.eq(1);
|
|
2266
2325
|
});
|
|
2267
2326
|
|
|
2268
|
-
it('
|
|
2327
|
+
it('should invoke the validator only once per value', function () {
|
|
2269
2328
|
let invoked = 0;
|
|
2270
|
-
const
|
|
2329
|
+
const dbs = new DatabaseSchema();
|
|
2330
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
2331
|
+
reg.addValidator('myValidator', function () {
|
|
2271
2332
|
invoked++;
|
|
2272
2333
|
return true;
|
|
2273
|
-
};
|
|
2274
|
-
const dbs = new DatabaseSchema();
|
|
2275
|
-
dbs
|
|
2276
|
-
.getService(PropertyValidatorRegistry)
|
|
2277
|
-
.addValidator('myValidator', myValidator);
|
|
2334
|
+
});
|
|
2278
2335
|
dbs.defineModel({
|
|
2279
2336
|
name: 'model',
|
|
2280
2337
|
properties: {
|
|
@@ -2291,141 +2348,119 @@ describe('ModelDataValidator', function () {
|
|
|
2291
2348
|
});
|
|
2292
2349
|
});
|
|
2293
2350
|
|
|
2294
|
-
describe('the option "validate"
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
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
|
+
},
|
|
2303
2365
|
},
|
|
2304
|
-
}
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
foo: 'test',
|
|
2366
|
+
});
|
|
2367
|
+
const validator = dbs.getService(ModelDataValidator);
|
|
2368
|
+
validator.validate('model', {});
|
|
2308
2369
|
});
|
|
2309
|
-
});
|
|
2310
2370
|
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
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
|
+
},
|
|
2322
2383
|
},
|
|
2323
|
-
}
|
|
2384
|
+
});
|
|
2385
|
+
const validator = dbs.getService(ModelDataValidator);
|
|
2386
|
+
validator.validate('model', {foo: undefined});
|
|
2387
|
+
validator.validate('model', {foo: null});
|
|
2324
2388
|
});
|
|
2325
|
-
const validator = dbs.getService(ModelDataValidator);
|
|
2326
|
-
validator.validate('model', {});
|
|
2327
|
-
});
|
|
2328
2389
|
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
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
|
+
},
|
|
2340
2402
|
},
|
|
2341
|
-
}
|
|
2403
|
+
});
|
|
2404
|
+
dbs
|
|
2405
|
+
.getService(EmptyValuesService)
|
|
2406
|
+
.setEmptyValuesOf(DataType.STRING, [5]);
|
|
2407
|
+
dbs.getService(ModelDataValidator).validate('model', {foo: 5});
|
|
2342
2408
|
});
|
|
2343
|
-
const validator = dbs.getService(ModelDataValidator);
|
|
2344
|
-
validator.validate('model', {foo: undefined});
|
|
2345
|
-
validator.validate('model', {foo: null});
|
|
2346
|
-
});
|
|
2347
2409
|
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
type: DataType.ANY,
|
|
2361
|
-
validate: ['myValidator'],
|
|
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
|
+
},
|
|
2362
2422
|
},
|
|
2363
|
-
},
|
|
2364
|
-
});
|
|
2365
|
-
const throwable = () =>
|
|
2366
|
-
dbs.getService(ModelDataValidator).validate('model', {
|
|
2367
|
-
foo: 'test',
|
|
2368
2423
|
});
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
dbs
|
|
2375
|
-
.getService(PropertyValidatorRegistry)
|
|
2376
|
-
.addValidator('myValidator1', () => true)
|
|
2377
|
-
.addValidator('myValidator2', () => true);
|
|
2378
|
-
dbs.defineModel({
|
|
2379
|
-
name: 'model',
|
|
2380
|
-
properties: {
|
|
2381
|
-
foo: {
|
|
2382
|
-
type: DataType.ANY,
|
|
2383
|
-
validate: ['myValidator1', 'myValidator2'],
|
|
2384
|
-
},
|
|
2385
|
-
},
|
|
2386
|
-
});
|
|
2387
|
-
dbs.getService(ModelDataValidator).validate('model', {
|
|
2388
|
-
foo: 'test',
|
|
2424
|
+
const throwable = () =>
|
|
2425
|
+
dbs.getService(ModelDataValidator).validate('model', {
|
|
2426
|
+
foo: 'test',
|
|
2427
|
+
});
|
|
2428
|
+
expect(throwable).to.throw('My error');
|
|
2389
2429
|
});
|
|
2390
|
-
});
|
|
2391
2430
|
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
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
|
+
},
|
|
2403
2445
|
},
|
|
2404
|
-
}
|
|
2405
|
-
});
|
|
2406
|
-
const throwable = () =>
|
|
2446
|
+
});
|
|
2407
2447
|
dbs.getService(ModelDataValidator).validate('model', {
|
|
2408
2448
|
foo: 'test',
|
|
2409
2449
|
});
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
'but the property validator "myValidator" returns a Promise.',
|
|
2413
|
-
);
|
|
2414
|
-
});
|
|
2450
|
+
expect(called).to.be.eq(1);
|
|
2451
|
+
});
|
|
2415
2452
|
|
|
2416
|
-
|
|
2417
|
-
const testFn = v => {
|
|
2453
|
+
it('should throw the error if the validator returns a promise', function () {
|
|
2418
2454
|
const dbs = new DatabaseSchema();
|
|
2419
|
-
|
|
2420
|
-
.
|
|
2421
|
-
|
|
2422
|
-
.addValidator('myValidator2', () => v);
|
|
2455
|
+
const myValidator = function () {
|
|
2456
|
+
return Promise.resolve(true);
|
|
2457
|
+
};
|
|
2423
2458
|
dbs.defineModel({
|
|
2424
2459
|
name: 'model',
|
|
2425
2460
|
properties: {
|
|
2426
2461
|
foo: {
|
|
2427
2462
|
type: DataType.ANY,
|
|
2428
|
-
validate:
|
|
2463
|
+
validate: myValidator,
|
|
2429
2464
|
},
|
|
2430
2465
|
},
|
|
2431
2466
|
});
|
|
@@ -2434,158 +2469,1778 @@ describe('ModelDataValidator', function () {
|
|
|
2434
2469
|
foo: 'test',
|
|
2435
2470
|
});
|
|
2436
2471
|
expect(throwable).to.throw(
|
|
2437
|
-
'
|
|
2438
|
-
'
|
|
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.',
|
|
2439
2475
|
);
|
|
2440
|
-
};
|
|
2441
|
-
testFn('str');
|
|
2442
|
-
testFn('');
|
|
2443
|
-
testFn(10);
|
|
2444
|
-
testFn(0);
|
|
2445
|
-
testFn(false);
|
|
2446
|
-
testFn(undefined);
|
|
2447
|
-
testFn(null);
|
|
2448
|
-
testFn({});
|
|
2449
|
-
testFn([]);
|
|
2450
|
-
testFn(() => undefined);
|
|
2451
|
-
});
|
|
2452
|
-
|
|
2453
|
-
it('passes arguments to the validator', function () {
|
|
2454
|
-
let validated = false;
|
|
2455
|
-
const dbs = new DatabaseSchema();
|
|
2456
|
-
const myValidator = function (value, options, context) {
|
|
2457
|
-
expect(value).to.be.eq('test');
|
|
2458
|
-
expect(options).to.be.undefined;
|
|
2459
|
-
expect(context).to.be.eql({
|
|
2460
|
-
validatorName: 'myValidator',
|
|
2461
|
-
modelName: 'model',
|
|
2462
|
-
propName: 'foo',
|
|
2463
|
-
});
|
|
2464
|
-
validated = true;
|
|
2465
|
-
return true;
|
|
2466
|
-
};
|
|
2467
|
-
dbs
|
|
2468
|
-
.getService(PropertyValidatorRegistry)
|
|
2469
|
-
.addValidator('myValidator', myValidator);
|
|
2470
|
-
dbs.defineModel({
|
|
2471
|
-
name: 'model',
|
|
2472
|
-
properties: {
|
|
2473
|
-
foo: {
|
|
2474
|
-
type: DataType.ANY,
|
|
2475
|
-
validate: ['myValidator'],
|
|
2476
|
-
},
|
|
2477
|
-
},
|
|
2478
2476
|
});
|
|
2479
|
-
|
|
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', {
|
|
2480
2788
|
foo: 'test',
|
|
2481
2789
|
});
|
|
2482
|
-
expect(validated).to.be.true;
|
|
2483
2790
|
});
|
|
2484
2791
|
|
|
2485
|
-
it('
|
|
2486
|
-
const
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
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;
|
|
2495
4073
|
const dbs = new DatabaseSchema();
|
|
2496
|
-
dbs
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
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
|
+
});
|
|
2500
4083
|
dbs.defineModel({
|
|
2501
4084
|
name: 'model',
|
|
2502
4085
|
properties: {
|
|
2503
4086
|
foo: {
|
|
2504
4087
|
type: DataType.ANY,
|
|
2505
|
-
validate:
|
|
4088
|
+
validate: {
|
|
4089
|
+
myValidator1: true,
|
|
4090
|
+
myValidator2: true,
|
|
4091
|
+
},
|
|
2506
4092
|
},
|
|
2507
4093
|
},
|
|
2508
4094
|
});
|
|
2509
|
-
dbs.getService(ModelDataValidator)
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
|
|
4095
|
+
const validator = dbs.getService(ModelDataValidator);
|
|
4096
|
+
validator.validate('model', {});
|
|
4097
|
+
expect(calls).to.be.eq(0);
|
|
2513
4098
|
});
|
|
2514
|
-
});
|
|
2515
4099
|
|
|
2516
|
-
|
|
2517
|
-
|
|
4100
|
+
it('should not validate undefined and null values', function () {
|
|
4101
|
+
let calls = 0;
|
|
2518
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
|
+
});
|
|
2519
4112
|
dbs.defineModel({
|
|
2520
4113
|
name: 'model',
|
|
2521
4114
|
properties: {
|
|
2522
4115
|
foo: {
|
|
2523
4116
|
type: DataType.ANY,
|
|
2524
|
-
validate: {
|
|
4117
|
+
validate: {
|
|
4118
|
+
myValidator1: true,
|
|
4119
|
+
myValidator2: true,
|
|
4120
|
+
},
|
|
2525
4121
|
},
|
|
2526
4122
|
},
|
|
2527
4123
|
});
|
|
2528
|
-
dbs.getService(ModelDataValidator)
|
|
2529
|
-
|
|
2530
|
-
});
|
|
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);
|
|
2531
4128
|
});
|
|
2532
4129
|
|
|
2533
|
-
it('
|
|
4130
|
+
it('should not validate the empty value', function () {
|
|
4131
|
+
let calls = 0;
|
|
2534
4132
|
const dbs = new DatabaseSchema();
|
|
2535
|
-
dbs
|
|
2536
|
-
|
|
2537
|
-
|
|
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
|
+
});
|
|
2538
4142
|
dbs.defineModel({
|
|
2539
4143
|
name: 'model',
|
|
2540
4144
|
properties: {
|
|
2541
4145
|
foo: {
|
|
2542
|
-
type: DataType.
|
|
4146
|
+
type: DataType.STRING,
|
|
2543
4147
|
validate: {
|
|
2544
|
-
|
|
4148
|
+
myValidator1: true,
|
|
4149
|
+
myValidator2: true,
|
|
2545
4150
|
},
|
|
2546
4151
|
},
|
|
2547
4152
|
},
|
|
2548
4153
|
});
|
|
2549
|
-
|
|
2550
|
-
|
|
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);
|
|
2551
4159
|
});
|
|
2552
4160
|
|
|
2553
|
-
it('
|
|
4161
|
+
it('should throw the error for the non-existent validator name', function () {
|
|
4162
|
+
let called = 0;
|
|
2554
4163
|
const dbs = new DatabaseSchema();
|
|
2555
|
-
dbs
|
|
2556
|
-
|
|
2557
|
-
|
|
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
|
+
});
|
|
2558
4204
|
dbs.defineModel({
|
|
2559
4205
|
name: 'model',
|
|
2560
4206
|
properties: {
|
|
2561
4207
|
foo: {
|
|
2562
4208
|
type: DataType.ANY,
|
|
2563
4209
|
validate: {
|
|
2564
|
-
|
|
4210
|
+
myValidator1: true,
|
|
4211
|
+
myValidator2: true,
|
|
2565
4212
|
},
|
|
2566
4213
|
},
|
|
2567
4214
|
},
|
|
2568
4215
|
});
|
|
2569
|
-
const
|
|
2570
|
-
|
|
2571
|
-
|
|
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);
|
|
2572
4222
|
});
|
|
2573
4223
|
|
|
2574
|
-
it('
|
|
2575
|
-
|
|
2576
|
-
throw Error('My error');
|
|
2577
|
-
};
|
|
4224
|
+
it('should throw the error from the second validator', function () {
|
|
4225
|
+
let called = 0;
|
|
2578
4226
|
const dbs = new DatabaseSchema();
|
|
2579
|
-
dbs
|
|
2580
|
-
|
|
2581
|
-
|
|
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
|
+
});
|
|
2582
4236
|
dbs.defineModel({
|
|
2583
4237
|
name: 'model',
|
|
2584
4238
|
properties: {
|
|
2585
4239
|
foo: {
|
|
2586
4240
|
type: DataType.ANY,
|
|
2587
4241
|
validate: {
|
|
2588
|
-
|
|
4242
|
+
myValidator1: true,
|
|
4243
|
+
myValidator2: true,
|
|
2589
4244
|
},
|
|
2590
4245
|
},
|
|
2591
4246
|
},
|
|
@@ -2595,14 +4250,21 @@ describe('ModelDataValidator', function () {
|
|
|
2595
4250
|
foo: 'test',
|
|
2596
4251
|
});
|
|
2597
4252
|
expect(throwable).to.throw('My error');
|
|
4253
|
+
expect(called).to.be.eq(2);
|
|
2598
4254
|
});
|
|
2599
4255
|
|
|
2600
|
-
it('
|
|
4256
|
+
it('should allow the given value if validators returns true', function () {
|
|
4257
|
+
let called = 0;
|
|
2601
4258
|
const dbs = new DatabaseSchema();
|
|
2602
|
-
dbs
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
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
|
+
});
|
|
2606
4268
|
dbs.defineModel({
|
|
2607
4269
|
name: 'model',
|
|
2608
4270
|
properties: {
|
|
@@ -2618,20 +4280,65 @@ describe('ModelDataValidator', function () {
|
|
|
2618
4280
|
dbs.getService(ModelDataValidator).validate('model', {
|
|
2619
4281
|
foo: 'test',
|
|
2620
4282
|
});
|
|
4283
|
+
expect(called).to.be.eq(2);
|
|
2621
4284
|
});
|
|
2622
4285
|
|
|
2623
|
-
it('
|
|
4286
|
+
it('should throw the error if the first validator returns a promise', function () {
|
|
4287
|
+
let called = 0;
|
|
2624
4288
|
const dbs = new DatabaseSchema();
|
|
2625
|
-
dbs
|
|
2626
|
-
|
|
2627
|
-
|
|
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
|
+
});
|
|
2628
4334
|
dbs.defineModel({
|
|
2629
4335
|
name: 'model',
|
|
2630
4336
|
properties: {
|
|
2631
4337
|
foo: {
|
|
2632
4338
|
type: DataType.ANY,
|
|
2633
4339
|
validate: {
|
|
2634
|
-
|
|
4340
|
+
myValidator1: true,
|
|
4341
|
+
myValidator2: true,
|
|
2635
4342
|
},
|
|
2636
4343
|
},
|
|
2637
4344
|
},
|
|
@@ -2642,17 +4349,72 @@ describe('ModelDataValidator', function () {
|
|
|
2642
4349
|
});
|
|
2643
4350
|
expect(throwable).to.throw(
|
|
2644
4351
|
'Asynchronous property validators are not supported, ' +
|
|
2645
|
-
'but the property
|
|
4352
|
+
'but the property "foo" of the model "model" has the property ' +
|
|
4353
|
+
'validator "myValidator2" that returns a Promise.',
|
|
2646
4354
|
);
|
|
4355
|
+
expect(called).to.be.eq(2);
|
|
2647
4356
|
});
|
|
2648
4357
|
|
|
2649
|
-
it('
|
|
4358
|
+
it('should throw the error for a non-true result from the first validator', function () {
|
|
2650
4359
|
const testFn = v => {
|
|
4360
|
+
let called = 0;
|
|
2651
4361
|
const dbs = new DatabaseSchema();
|
|
2652
|
-
dbs
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
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
|
+
});
|
|
2656
4418
|
dbs.defineModel({
|
|
2657
4419
|
name: 'model',
|
|
2658
4420
|
properties: {
|
|
@@ -2670,9 +4432,10 @@ describe('ModelDataValidator', function () {
|
|
|
2670
4432
|
foo: 'test',
|
|
2671
4433
|
});
|
|
2672
4434
|
expect(throwable).to.throw(
|
|
2673
|
-
'The property "foo" of the model "model" has
|
|
2674
|
-
'that caught by the validator "myValidator2".',
|
|
4435
|
+
'The property "foo" of the model "model" has the invalid value "test" ' +
|
|
4436
|
+
'that caught by the property validator "myValidator2".',
|
|
2675
4437
|
);
|
|
4438
|
+
expect(called).to.be.eq(2);
|
|
2676
4439
|
};
|
|
2677
4440
|
testFn('str');
|
|
2678
4441
|
testFn('');
|
|
@@ -2686,36 +4449,40 @@ describe('ModelDataValidator', function () {
|
|
|
2686
4449
|
testFn(() => undefined);
|
|
2687
4450
|
});
|
|
2688
4451
|
|
|
2689
|
-
it('
|
|
2690
|
-
let
|
|
4452
|
+
it('should pass arguments to validators', function () {
|
|
4453
|
+
let called = false;
|
|
2691
4454
|
const dbs = new DatabaseSchema();
|
|
2692
|
-
const
|
|
4455
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
4456
|
+
reg.addValidator('myValidator1', function (value, options, context) {
|
|
2693
4457
|
expect(value).to.be.eq('test');
|
|
2694
|
-
expect(options).to.be.
|
|
2695
|
-
|
|
2696
|
-
|
|
4458
|
+
expect(options).to.be.eq('foo');
|
|
4459
|
+
expect(context).to.be.eql({
|
|
4460
|
+
validatorName: 'myValidator1',
|
|
4461
|
+
modelName: 'model',
|
|
4462
|
+
propName: 'foo',
|
|
2697
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');
|
|
2698
4470
|
expect(context).to.be.eql({
|
|
2699
|
-
validatorName: '
|
|
4471
|
+
validatorName: 'myValidator2',
|
|
2700
4472
|
modelName: 'model',
|
|
2701
4473
|
propName: 'foo',
|
|
2702
4474
|
});
|
|
2703
|
-
|
|
4475
|
+
called++;
|
|
2704
4476
|
return true;
|
|
2705
|
-
};
|
|
2706
|
-
dbs
|
|
2707
|
-
.getService(PropertyValidatorRegistry)
|
|
2708
|
-
.addValidator('myValidator', myValidator);
|
|
4477
|
+
});
|
|
2709
4478
|
dbs.defineModel({
|
|
2710
4479
|
name: 'model',
|
|
2711
4480
|
properties: {
|
|
2712
4481
|
foo: {
|
|
2713
4482
|
type: DataType.ANY,
|
|
2714
4483
|
validate: {
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
option2: 'value2',
|
|
2718
|
-
},
|
|
4484
|
+
myValidator1: 'foo',
|
|
4485
|
+
myValidator2: 'bar',
|
|
2719
4486
|
},
|
|
2720
4487
|
},
|
|
2721
4488
|
},
|
|
@@ -2723,24 +4490,21 @@ describe('ModelDataValidator', function () {
|
|
|
2723
4490
|
dbs.getService(ModelDataValidator).validate('model', {
|
|
2724
4491
|
foo: 'test',
|
|
2725
4492
|
});
|
|
2726
|
-
expect(
|
|
4493
|
+
expect(called).to.be.eq(2);
|
|
2727
4494
|
});
|
|
2728
4495
|
|
|
2729
|
-
it('
|
|
4496
|
+
it('should invoke validators in the correct order', function () {
|
|
2730
4497
|
const invocation = [];
|
|
2731
|
-
const
|
|
4498
|
+
const dbs = new DatabaseSchema();
|
|
4499
|
+
const reg = dbs.getService(PropertyValidatorRegistry);
|
|
4500
|
+
reg.addValidator('myValidator1', function () {
|
|
2732
4501
|
invocation.push('myValidator1');
|
|
2733
4502
|
return true;
|
|
2734
|
-
};
|
|
2735
|
-
|
|
4503
|
+
});
|
|
4504
|
+
reg.addValidator('myValidator2', function () {
|
|
2736
4505
|
invocation.push('myValidator2');
|
|
2737
4506
|
return true;
|
|
2738
|
-
};
|
|
2739
|
-
const dbs = new DatabaseSchema();
|
|
2740
|
-
dbs
|
|
2741
|
-
.getService(PropertyValidatorRegistry)
|
|
2742
|
-
.addValidator('myValidator1', validator1)
|
|
2743
|
-
.addValidator('myValidator2', validator2);
|
|
4507
|
+
});
|
|
2744
4508
|
dbs.defineModel({
|
|
2745
4509
|
name: 'model',
|
|
2746
4510
|
properties: {
|
|
@@ -2758,72 +4522,6 @@ describe('ModelDataValidator', function () {
|
|
|
2758
4522
|
});
|
|
2759
4523
|
expect(invocation).to.be.eql(['myValidator1', 'myValidator2']);
|
|
2760
4524
|
});
|
|
2761
|
-
|
|
2762
|
-
it('validates even the validator options is false', function () {
|
|
2763
|
-
let validated = false;
|
|
2764
|
-
const myValidator = function () {
|
|
2765
|
-
validated = true;
|
|
2766
|
-
return true;
|
|
2767
|
-
};
|
|
2768
|
-
const dbs = new DatabaseSchema();
|
|
2769
|
-
dbs
|
|
2770
|
-
.getService(PropertyValidatorRegistry)
|
|
2771
|
-
.addValidator('myValidator', myValidator);
|
|
2772
|
-
dbs.defineModel({
|
|
2773
|
-
name: 'model',
|
|
2774
|
-
properties: {
|
|
2775
|
-
foo: {
|
|
2776
|
-
type: DataType.ANY,
|
|
2777
|
-
validate: {
|
|
2778
|
-
myValidator: false,
|
|
2779
|
-
},
|
|
2780
|
-
},
|
|
2781
|
-
},
|
|
2782
|
-
});
|
|
2783
|
-
dbs.getService(ModelDataValidator).validate('model', {
|
|
2784
|
-
foo: 'test',
|
|
2785
|
-
});
|
|
2786
|
-
expect(validated).to.be.true;
|
|
2787
|
-
});
|
|
2788
|
-
});
|
|
2789
|
-
|
|
2790
|
-
it('the option "validate" requires a non-empty String, an Array or an Object', function () {
|
|
2791
|
-
const dbs = new DatabaseSchema();
|
|
2792
|
-
dbs
|
|
2793
|
-
.getService(PropertyValidatorRegistry)
|
|
2794
|
-
.addValidator('myValidator', () => true);
|
|
2795
|
-
dbs.defineModel({
|
|
2796
|
-
name: 'model',
|
|
2797
|
-
properties: {
|
|
2798
|
-
foo: {
|
|
2799
|
-
type: DataType.ANY,
|
|
2800
|
-
validate: undefined,
|
|
2801
|
-
},
|
|
2802
|
-
},
|
|
2803
|
-
});
|
|
2804
|
-
const V = dbs.getService(ModelDataValidator);
|
|
2805
|
-
const throwable = v => () => {
|
|
2806
|
-
const models = dbs.getService(DefinitionRegistry)['_models'];
|
|
2807
|
-
models.model.properties.foo.validate = v;
|
|
2808
|
-
V.validate('model', {foo: 'bar'});
|
|
2809
|
-
};
|
|
2810
|
-
const error = v =>
|
|
2811
|
-
format(
|
|
2812
|
-
'The provided option "validate" of the property "foo" in the model "model" ' +
|
|
2813
|
-
'should be a non-empty String, an Array of String or an Object, ' +
|
|
2814
|
-
'but %s given.',
|
|
2815
|
-
v,
|
|
2816
|
-
);
|
|
2817
|
-
expect(throwable('')).to.throw(error('""'));
|
|
2818
|
-
expect(throwable(10)).to.throw(error('10'));
|
|
2819
|
-
expect(throwable(0)).to.throw(error('0'));
|
|
2820
|
-
expect(throwable(true)).to.throw(error('true'));
|
|
2821
|
-
expect(throwable(false)).to.throw(error('false'));
|
|
2822
|
-
throwable('myValidator')();
|
|
2823
|
-
throwable(['myValidator'])();
|
|
2824
|
-
throwable([])();
|
|
2825
|
-
throwable({myValidator: true})();
|
|
2826
|
-
throwable({})();
|
|
2827
4525
|
});
|
|
2828
4526
|
});
|
|
2829
4527
|
});
|