knight-validation 2.0.0 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -31
- package/lib/index.d.ts +12 -13
- package/lib/index.js +26 -26
- package/lib/lib/Constraint.d.ts +3 -7
- package/lib/lib/Constraint.js +5 -121
- package/lib/lib/Misfit.d.ts +8 -14
- package/lib/lib/Misfit.js +24 -44
- package/lib/lib/MisfitsError.d.ts +2 -2
- package/lib/lib/MisfitsError.js +7 -22
- package/lib/lib/QuickConstraint.d.ts +8 -0
- package/lib/lib/QuickConstraint.js +26 -0
- package/lib/lib/Validator.d.ts +16 -31
- package/lib/lib/Validator.js +104 -403
- package/lib/lib/constraints/Absent.d.ts +7 -4
- package/lib/lib/constraints/Absent.js +10 -75
- package/lib/lib/constraints/Bounds.d.ts +18 -0
- package/lib/lib/constraints/Bounds.js +50 -0
- package/lib/lib/constraints/Enum.d.ts +8 -4
- package/lib/lib/constraints/Enum.js +27 -76
- package/lib/lib/constraints/Exists.d.ts +9 -6
- package/lib/lib/constraints/Exists.js +19 -67
- package/lib/lib/constraints/Length.d.ts +16 -0
- package/lib/lib/constraints/Length.js +46 -0
- package/lib/lib/constraints/Required.d.ts +4 -4
- package/lib/lib/constraints/Required.js +10 -75
- package/lib/lib/constraints/TypeOf.d.ts +9 -10
- package/lib/lib/constraints/TypeOf.js +57 -81
- package/lib/lib/constraints/Unique.d.ts +9 -6
- package/lib/lib/constraints/Unique.js +19 -68
- package/package.json +7 -7
- package/lib/lib/constraints/Max.d.ts +0 -7
- package/lib/lib/constraints/Max.js +0 -84
- package/lib/lib/constraints/QuickConstraint.d.ts +0 -7
- package/lib/lib/constraints/QuickConstraint.js +0 -70
- package/lib/lib/fieldsEqual.d.ts +0 -1
- package/lib/lib/fieldsEqual.js +0 -17
package/lib/lib/Validator.js
CHANGED
|
@@ -8,432 +8,133 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.
|
|
12
|
+
exports.Validator = void 0;
|
|
13
|
+
const Constraint_1 = require("./Constraint");
|
|
14
|
+
const QuickConstraint_1 = require("./QuickConstraint");
|
|
15
|
+
class Validator {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.entries = [];
|
|
45
18
|
this.options = options;
|
|
46
19
|
}
|
|
47
|
-
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
for (
|
|
51
|
-
|
|
52
|
-
this.fieldConstraints.push(fieldConstraint);
|
|
20
|
+
add(...args) {
|
|
21
|
+
if (args[0] instanceof Validator) {
|
|
22
|
+
let validator = args[0];
|
|
23
|
+
for (let propertyConstraint of validator.entries) {
|
|
24
|
+
this.entries.push(propertyConstraint);
|
|
53
25
|
}
|
|
54
26
|
}
|
|
55
27
|
else {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
28
|
+
let properties = typeof args[0] == 'string' ? [args[0]] : args[0];
|
|
29
|
+
let constraint = undefined;
|
|
30
|
+
let validator = undefined;
|
|
31
|
+
let condition;
|
|
32
|
+
if (typeof args[1] == 'string') {
|
|
33
|
+
constraint = new QuickConstraint_1.QuickConstraint(args[1], args[2]);
|
|
34
|
+
condition = args.length > 3 ? args[3] : undefined;
|
|
35
|
+
}
|
|
36
|
+
else if (args[1] instanceof Constraint_1.Constraint) {
|
|
37
|
+
constraint = args[1];
|
|
38
|
+
condition = args.length > 2 ? args[2] : undefined;
|
|
39
|
+
}
|
|
40
|
+
else if (args[1] instanceof Validator) {
|
|
41
|
+
validator = args[1];
|
|
42
|
+
condition = args.length > 2 ? args[2] : undefined;
|
|
70
43
|
}
|
|
71
44
|
else {
|
|
72
45
|
throw new Error('Wrong parameters');
|
|
73
46
|
}
|
|
74
|
-
this.
|
|
47
|
+
this.entries.push({
|
|
48
|
+
properties: properties,
|
|
49
|
+
constraint: constraint,
|
|
50
|
+
validator: validator,
|
|
51
|
+
condition: condition
|
|
52
|
+
});
|
|
75
53
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return fields;
|
|
90
|
-
},
|
|
91
|
-
enumerable: false,
|
|
92
|
-
configurable: true
|
|
93
|
-
});
|
|
94
|
-
Object.defineProperty(Validator.prototype, "singleFields", {
|
|
95
|
-
get: function () {
|
|
96
|
-
var fields = [];
|
|
97
|
-
for (var _i = 0, _a = this.fieldConstraints; _i < _a.length; _i++) {
|
|
98
|
-
var fieldConstraint = _a[_i];
|
|
99
|
-
if (fieldConstraint.field != undefined && fields.indexOf(fieldConstraint.field) == -1) {
|
|
100
|
-
fields.push(fieldConstraint.field);
|
|
54
|
+
}
|
|
55
|
+
validate(object, options) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
options = options || this.options;
|
|
58
|
+
let misfits = [];
|
|
59
|
+
let misfittingProperties = [];
|
|
60
|
+
for (let entry of this.entries) {
|
|
61
|
+
let propertyAlreadyHasAMisfit = false;
|
|
62
|
+
for (let property of entry.properties) {
|
|
63
|
+
if (misfittingProperties.indexOf(property) > -1) {
|
|
64
|
+
propertyAlreadyHasAMisfit = true;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
101
67
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
},
|
|
105
|
-
enumerable: false,
|
|
106
|
-
configurable: true
|
|
107
|
-
});
|
|
108
|
-
Object.defineProperty(Validator.prototype, "combinedFields", {
|
|
109
|
-
get: function () {
|
|
110
|
-
var fields = [];
|
|
111
|
-
var _loop_1 = function (fieldConstraint) {
|
|
112
|
-
if (fieldConstraint.fields != undefined && !fields.some(function (fields) { return fieldsEqual_1.default(fields, fieldConstraint.fields); })) {
|
|
113
|
-
fields.push(fieldConstraint.fields);
|
|
68
|
+
if (propertyAlreadyHasAMisfit) {
|
|
69
|
+
continue;
|
|
114
70
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
var fieldConstraint = _a[_i];
|
|
118
|
-
_loop_1(fieldConstraint);
|
|
119
|
-
}
|
|
120
|
-
return fields;
|
|
121
|
-
},
|
|
122
|
-
enumerable: false,
|
|
123
|
-
configurable: true
|
|
124
|
-
});
|
|
125
|
-
Validator.prototype.constraints = function (field) {
|
|
126
|
-
var fieldConstraints = [];
|
|
127
|
-
for (var _i = 0, _a = this.fieldConstraints; _i < _a.length; _i++) {
|
|
128
|
-
var fieldConstraint = _a[_i];
|
|
129
|
-
if (field === fieldConstraint.field) {
|
|
130
|
-
fieldConstraints.push(fieldConstraint);
|
|
131
|
-
}
|
|
132
|
-
else if (field instanceof Array && fieldConstraint.fields && fieldsEqual_1.default(field, fieldConstraint.fields)) {
|
|
133
|
-
fieldConstraints.push(fieldConstraint);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return fieldConstraints;
|
|
137
|
-
};
|
|
138
|
-
Validator.prototype.validate = function (object, options) {
|
|
139
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
140
|
-
var misfits, misfittingFields, _i, _a, field, constraints, _b, constraints_1, constraint, _c, misfit, fieldValue, subMisfits, _d, subMisfits_1, misfit, _e, _f, fields, oneOfTheFieldsAlreadyHasAMisfit, _g, fields_1, field, atLeastOneOfTheFieldsMissingInObject, _h, fields_2, field, constraints, _j, constraints_2, constraint, _k, misfit;
|
|
141
|
-
return __generator(this, function (_l) {
|
|
142
|
-
switch (_l.label) {
|
|
143
|
-
case 0:
|
|
144
|
-
options = options || this.options;
|
|
145
|
-
misfits = [];
|
|
146
|
-
misfittingFields = [];
|
|
147
|
-
_i = 0, _a = this.singleFields;
|
|
148
|
-
_l.label = 1;
|
|
149
|
-
case 1:
|
|
150
|
-
if (!(_i < _a.length)) return [3 /*break*/, 10];
|
|
151
|
-
field = _a[_i];
|
|
152
|
-
if (options && options.include instanceof Array && !containsField(options.include, field)) {
|
|
153
|
-
return [3 /*break*/, 9];
|
|
154
|
-
}
|
|
155
|
-
if (options && options.exclude instanceof Array && containsFieldWithoutConstraints(options.exclude, field)) {
|
|
156
|
-
return [3 /*break*/, 9];
|
|
157
|
-
}
|
|
158
|
-
if (object[field] === undefined && options && options.checkOnlyWhatIsThere) {
|
|
159
|
-
return [3 /*break*/, 9];
|
|
160
|
-
}
|
|
161
|
-
constraints = this.constraints(field);
|
|
162
|
-
_b = 0, constraints_1 = constraints;
|
|
163
|
-
_l.label = 2;
|
|
164
|
-
case 2:
|
|
165
|
-
if (!(_b < constraints_1.length)) return [3 /*break*/, 9];
|
|
166
|
-
constraint = constraints_1[_b];
|
|
167
|
-
if (options && options.include instanceof Array && !(containsFieldWithoutConstraints(options.include, field) || containsFieldAndConstraint(options.include, field, constraint))) {
|
|
168
|
-
return [3 /*break*/, 8];
|
|
169
|
-
}
|
|
170
|
-
if (options && options.exclude instanceof Array && containsFieldAndConstraint(options.exclude, field, constraint)) {
|
|
171
|
-
return [3 /*break*/, 8];
|
|
172
|
-
}
|
|
173
|
-
_c = constraint.condition != undefined;
|
|
174
|
-
if (!_c) return [3 /*break*/, 4];
|
|
175
|
-
return [4 /*yield*/, constraint.condition(object)];
|
|
176
|
-
case 3:
|
|
177
|
-
_c = !(_l.sent());
|
|
178
|
-
_l.label = 4;
|
|
179
|
-
case 4:
|
|
180
|
-
if (_c) {
|
|
181
|
-
return [3 /*break*/, 8];
|
|
182
|
-
}
|
|
183
|
-
if (!(constraint.constraint != undefined)) return [3 /*break*/, 6];
|
|
184
|
-
return [4 /*yield*/, constraint.validateConstraint(object, field)];
|
|
185
|
-
case 5:
|
|
186
|
-
misfit = _l.sent();
|
|
187
|
-
if (misfit) {
|
|
188
|
-
misfit.field = field;
|
|
189
|
-
if (misfit.constraint === undefined) {
|
|
190
|
-
misfit.constraint = constraint.constraint.name;
|
|
191
|
-
}
|
|
192
|
-
misfittingFields.push(field);
|
|
193
|
-
misfits.push(misfit);
|
|
194
|
-
return [3 /*break*/, 9];
|
|
195
|
-
}
|
|
196
|
-
return [3 /*break*/, 8];
|
|
197
|
-
case 6:
|
|
198
|
-
if (!(constraint.validator != undefined)) return [3 /*break*/, 8];
|
|
199
|
-
fieldValue = object[field];
|
|
200
|
-
if (fieldValue == undefined) {
|
|
201
|
-
return [3 /*break*/, 8];
|
|
202
|
-
}
|
|
203
|
-
return [4 /*yield*/, constraint.validateValidator(fieldValue)];
|
|
204
|
-
case 7:
|
|
205
|
-
subMisfits = _l.sent();
|
|
206
|
-
for (_d = 0, subMisfits_1 = subMisfits; _d < subMisfits_1.length; _d++) {
|
|
207
|
-
misfit = subMisfits_1[_d];
|
|
208
|
-
misfit.field = field + '.' + misfit.field;
|
|
209
|
-
}
|
|
210
|
-
misfits.push.apply(misfits, subMisfits);
|
|
211
|
-
_l.label = 8;
|
|
212
|
-
case 8:
|
|
213
|
-
_b++;
|
|
214
|
-
return [3 /*break*/, 2];
|
|
215
|
-
case 9:
|
|
216
|
-
_i++;
|
|
217
|
-
return [3 /*break*/, 1];
|
|
218
|
-
case 10:
|
|
219
|
-
_e = 0, _f = this.combinedFields;
|
|
220
|
-
_l.label = 11;
|
|
221
|
-
case 11:
|
|
222
|
-
if (!(_e < _f.length)) return [3 /*break*/, 18];
|
|
223
|
-
fields = _f[_e];
|
|
224
|
-
if (options && options.include instanceof Array && !containsField(options.include, fields)) {
|
|
225
|
-
return [3 /*break*/, 17];
|
|
226
|
-
}
|
|
227
|
-
if (options && options.exclude instanceof Array && containsFieldWithoutConstraints(options.exclude, fields)) {
|
|
228
|
-
return [3 /*break*/, 17];
|
|
229
|
-
}
|
|
230
|
-
oneOfTheFieldsAlreadyHasAMisfit = false;
|
|
231
|
-
for (_g = 0, fields_1 = fields; _g < fields_1.length; _g++) {
|
|
232
|
-
field = fields_1[_g];
|
|
233
|
-
if (misfittingFields.indexOf(field) > -1) {
|
|
234
|
-
oneOfTheFieldsAlreadyHasAMisfit = true;
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
if (oneOfTheFieldsAlreadyHasAMisfit) {
|
|
239
|
-
return [3 /*break*/, 17];
|
|
240
|
-
}
|
|
241
|
-
atLeastOneOfTheFieldsMissingInObject = false;
|
|
242
|
-
for (_h = 0, fields_2 = fields; _h < fields_2.length; _h++) {
|
|
243
|
-
field = fields_2[_h];
|
|
244
|
-
if (object[field] === undefined) {
|
|
245
|
-
atLeastOneOfTheFieldsMissingInObject = true;
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (atLeastOneOfTheFieldsMissingInObject && options && options.checkOnlyWhatIsThere) {
|
|
250
|
-
return [3 /*break*/, 17];
|
|
251
|
-
}
|
|
252
|
-
constraints = this.constraints(fields);
|
|
253
|
-
_j = 0, constraints_2 = constraints;
|
|
254
|
-
_l.label = 12;
|
|
255
|
-
case 12:
|
|
256
|
-
if (!(_j < constraints_2.length)) return [3 /*break*/, 17];
|
|
257
|
-
constraint = constraints_2[_j];
|
|
258
|
-
if (options && options.include instanceof Array && !(containsFieldWithoutConstraints(options.include, fields) || containsFieldAndConstraint(options.include, fields, constraint))) {
|
|
259
|
-
return [3 /*break*/, 16];
|
|
260
|
-
}
|
|
261
|
-
if (options && options.exclude instanceof Array && containsFieldAndConstraint(options.exclude, fields, constraint)) {
|
|
262
|
-
return [3 /*break*/, 16];
|
|
263
|
-
}
|
|
264
|
-
_k = constraint.condition != undefined;
|
|
265
|
-
if (!_k) return [3 /*break*/, 14];
|
|
266
|
-
return [4 /*yield*/, constraint.condition(object)];
|
|
267
|
-
case 13:
|
|
268
|
-
_k = !(_l.sent());
|
|
269
|
-
_l.label = 14;
|
|
270
|
-
case 14:
|
|
271
|
-
if (_k) {
|
|
272
|
-
return [3 /*break*/, 16];
|
|
273
|
-
}
|
|
274
|
-
if (!(constraint.constraint != undefined)) return [3 /*break*/, 16];
|
|
275
|
-
return [4 /*yield*/, constraint.validateConstraint(object, fields)];
|
|
276
|
-
case 15:
|
|
277
|
-
misfit = _l.sent();
|
|
278
|
-
if (misfit) {
|
|
279
|
-
misfit.fields = fields;
|
|
280
|
-
if (misfit.constraint === undefined) {
|
|
281
|
-
misfit.constraint = constraint.constraint.name;
|
|
282
|
-
}
|
|
283
|
-
misfits.push(misfit);
|
|
284
|
-
return [3 /*break*/, 17];
|
|
285
|
-
}
|
|
286
|
-
_l.label = 16;
|
|
287
|
-
case 16:
|
|
288
|
-
_j++;
|
|
289
|
-
return [3 /*break*/, 12];
|
|
290
|
-
case 17:
|
|
291
|
-
_e++;
|
|
292
|
-
return [3 /*break*/, 11];
|
|
293
|
-
case 18: return [2 /*return*/, misfits];
|
|
71
|
+
if (entry.condition && !(yield entry.condition(object))) {
|
|
72
|
+
continue;
|
|
294
73
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
var FieldConstraint = /** @class */ (function () {
|
|
302
|
-
function FieldConstraint(field, constraintOrValidator, condition) {
|
|
303
|
-
this.field = typeof field == 'string' ? field : undefined;
|
|
304
|
-
this.fields = field instanceof Array ? field : undefined;
|
|
305
|
-
this.constraint = constraintOrValidator instanceof Constraint_1.default ? constraintOrValidator : undefined;
|
|
306
|
-
this.validator = constraintOrValidator instanceof Validator ? constraintOrValidator : undefined;
|
|
307
|
-
this.condition = condition;
|
|
308
|
-
}
|
|
309
|
-
FieldConstraint.prototype.validateConstraint = function (value, object) {
|
|
310
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
311
|
-
return __generator(this, function (_a) {
|
|
312
|
-
if (this.constraint == undefined) {
|
|
313
|
-
throw new Error('Could not validate constraint because it is not set');
|
|
74
|
+
let atLeastOnePropertyExists = false;
|
|
75
|
+
for (let property of entry.properties) {
|
|
76
|
+
if (object[property] !== undefined) {
|
|
77
|
+
atLeastOnePropertyExists = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
314
80
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
});
|
|
318
|
-
};
|
|
319
|
-
FieldConstraint.prototype.validateValidator = function (value) {
|
|
320
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
321
|
-
return __generator(this, function (_a) {
|
|
322
|
-
if (this.validator == undefined) {
|
|
323
|
-
throw new Error('Could not validate with validator because it is not set');
|
|
81
|
+
if (!atLeastOnePropertyExists && options && options.checkOnlyWhatIsThere) {
|
|
82
|
+
continue;
|
|
324
83
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
function containsField(fieldsAndConstraints, field) {
|
|
332
|
-
for (var _i = 0, fieldsAndConstraints_1 = fieldsAndConstraints; _i < fieldsAndConstraints_1.length; _i++) {
|
|
333
|
-
var fieldAndConstraint = fieldsAndConstraints_1[_i];
|
|
334
|
-
if (typeof fieldAndConstraint == 'string') {
|
|
335
|
-
if (fieldAndConstraint == field) {
|
|
336
|
-
return true;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
else if (fieldAndConstraint instanceof Array) {
|
|
340
|
-
if (field instanceof Array && fieldsEqual_1.default(fieldAndConstraint, field)) {
|
|
341
|
-
return true;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
else if (typeof fieldAndConstraint == 'object' && 'field' in fieldAndConstraint) {
|
|
345
|
-
if (containsField([fieldAndConstraint.field], field)) {
|
|
346
|
-
return true;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
return false;
|
|
351
|
-
}
|
|
352
|
-
function containsFieldWithoutConstraints(fieldsAndConstraints, field) {
|
|
353
|
-
for (var _i = 0, fieldsAndConstraints_2 = fieldsAndConstraints; _i < fieldsAndConstraints_2.length; _i++) {
|
|
354
|
-
var fieldAndConstraint = fieldsAndConstraints_2[_i];
|
|
355
|
-
if (typeof fieldAndConstraint == 'string') {
|
|
356
|
-
if (fieldAndConstraint == field) {
|
|
357
|
-
return true;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
else if (fieldAndConstraint instanceof Array) {
|
|
361
|
-
if (field instanceof Array && fieldsEqual_1.default(fieldAndConstraint, field)) {
|
|
362
|
-
return true;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
else if (typeof fieldAndConstraint == 'object' && 'field' in fieldAndConstraint) {
|
|
366
|
-
if (containsField([fieldAndConstraint.field], field)) {
|
|
367
|
-
return fieldAndConstraint.constraint == undefined;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return false;
|
|
372
|
-
}
|
|
373
|
-
function containsFieldAndConstraint(fieldsAndConstraints, field, constraint) {
|
|
374
|
-
for (var _i = 0, fieldsAndConstraints_3 = fieldsAndConstraints; _i < fieldsAndConstraints_3.length; _i++) {
|
|
375
|
-
var fieldAndConstraint = fieldsAndConstraints_3[_i];
|
|
376
|
-
if (typeof fieldAndConstraint == 'object' && 'field' in fieldAndConstraint && 'constraint' in fieldAndConstraint) {
|
|
377
|
-
if (typeof fieldAndConstraint.constraint == 'string') {
|
|
378
|
-
if (constraintNamesEqual(fieldAndConstraint.constraint, constraint)) {
|
|
379
|
-
if (typeof fieldAndConstraint.field == 'string' && fieldAndConstraint.field === field) {
|
|
380
|
-
return true;
|
|
84
|
+
if (entry.constraint != undefined) {
|
|
85
|
+
let misfit;
|
|
86
|
+
if (entry.properties.length == 1) {
|
|
87
|
+
let property = entry.properties[0];
|
|
88
|
+
let value = object[property];
|
|
89
|
+
misfit = yield entry.constraint.validate(value);
|
|
381
90
|
}
|
|
382
|
-
|
|
383
|
-
|
|
91
|
+
else {
|
|
92
|
+
misfit = yield entry.constraint.validate(object);
|
|
93
|
+
}
|
|
94
|
+
if (misfit) {
|
|
95
|
+
if (misfit.constraint === undefined) {
|
|
96
|
+
misfit.constraint = entry.constraint.name;
|
|
97
|
+
}
|
|
98
|
+
misfit.properties = entry.properties;
|
|
99
|
+
misfittingProperties.push(...entry.properties);
|
|
100
|
+
misfits.push(misfit);
|
|
384
101
|
}
|
|
385
102
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
103
|
+
else if (entry.validator != undefined) {
|
|
104
|
+
if (entry.properties.length != 1) {
|
|
105
|
+
throw new Error('Using another validator only works for one property');
|
|
106
|
+
}
|
|
107
|
+
let property = entry.properties[0];
|
|
108
|
+
let value = object[property];
|
|
109
|
+
if (typeof value != 'object' || value === null) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (value instanceof Array) {
|
|
113
|
+
for (let i = 0; i < value.length; i++) {
|
|
114
|
+
let subMisfits = yield entry.validator.validate(value[i], options);
|
|
115
|
+
if (subMisfits.length > 0) {
|
|
116
|
+
for (let misfit of subMisfits) {
|
|
117
|
+
misfit.addPrefix(`${property}[${i}].`);
|
|
118
|
+
}
|
|
119
|
+
misfittingProperties.push(...entry.properties);
|
|
120
|
+
misfits.push(...subMisfits);
|
|
121
|
+
}
|
|
393
122
|
}
|
|
394
|
-
|
|
395
|
-
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
let subMisfits = yield entry.validator.validate(value, options);
|
|
126
|
+
if (subMisfits.length > 0) {
|
|
127
|
+
for (let misfit of subMisfits) {
|
|
128
|
+
misfit.addPrefix(property + '.');
|
|
129
|
+
}
|
|
130
|
+
misfittingProperties.push(...entry.properties);
|
|
131
|
+
misfits.push(...subMisfits);
|
|
396
132
|
}
|
|
397
133
|
}
|
|
398
134
|
}
|
|
399
135
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return false;
|
|
403
|
-
}
|
|
404
|
-
function constraintNamesEqual(constraint1, constraint2) {
|
|
405
|
-
var _a, _b;
|
|
406
|
-
if (constraint1 === constraint2) {
|
|
407
|
-
return true;
|
|
408
|
-
}
|
|
409
|
-
if (constraint1 == undefined || constraint2 == undefined) {
|
|
410
|
-
return false;
|
|
411
|
-
}
|
|
412
|
-
var constraintName1;
|
|
413
|
-
var constraintName2;
|
|
414
|
-
if (typeof constraint1 == 'string') {
|
|
415
|
-
constraintName1 = constraint1;
|
|
416
|
-
}
|
|
417
|
-
else if (constraint1 instanceof Constraint_1.default) {
|
|
418
|
-
constraintName1 = constraint1.name;
|
|
419
|
-
}
|
|
420
|
-
else if (constraint1 instanceof FieldConstraint) {
|
|
421
|
-
constraintName1 = (_a = constraint1.constraint) === null || _a === void 0 ? void 0 : _a.name;
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
throw new Error('Unexpected constraint type');
|
|
425
|
-
}
|
|
426
|
-
if (typeof constraint2 == 'string') {
|
|
427
|
-
constraintName2 = constraint2;
|
|
428
|
-
}
|
|
429
|
-
else if (constraint2 instanceof Constraint_1.default) {
|
|
430
|
-
constraintName2 = constraint2.name;
|
|
431
|
-
}
|
|
432
|
-
else if (constraint2 instanceof FieldConstraint) {
|
|
433
|
-
constraintName2 = (_b = constraint2.constraint) === null || _b === void 0 ? void 0 : _b.name;
|
|
434
|
-
}
|
|
435
|
-
else {
|
|
436
|
-
throw new Error('Unexpected constraint type');
|
|
136
|
+
return misfits;
|
|
137
|
+
});
|
|
437
138
|
}
|
|
438
|
-
return constraintName1 === constraintName2;
|
|
439
139
|
}
|
|
140
|
+
exports.Validator = Validator;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import Constraint from '../Constraint';
|
|
2
|
-
import Misfit from '../Misfit';
|
|
3
|
-
export
|
|
4
|
-
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface AbsentMisfitValues {
|
|
4
|
+
actual: any;
|
|
5
|
+
}
|
|
6
|
+
export declare class Absent extends Constraint<any, AbsentMisfitValues> {
|
|
7
|
+
validate(value: any): Promise<Misfit<AbsentMisfitValues> | null>;
|
|
5
8
|
}
|
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
16
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
17
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -21,67 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
21
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
22
9
|
});
|
|
23
10
|
};
|
|
24
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
25
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
26
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
27
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
28
|
-
function step(op) {
|
|
29
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
30
|
-
while (_) try {
|
|
31
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
32
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
33
|
-
switch (op[0]) {
|
|
34
|
-
case 0: case 1: t = op; break;
|
|
35
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
36
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
37
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
38
|
-
default:
|
|
39
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
40
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
41
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
42
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
43
|
-
if (t[2]) _.ops.pop();
|
|
44
|
-
_.trys.pop(); continue;
|
|
45
|
-
}
|
|
46
|
-
op = body.call(thisArg, _);
|
|
47
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
48
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
Absent.prototype.validate = function (obj, field) {
|
|
60
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
61
|
-
var value, _i, field_1, fld, value;
|
|
62
|
-
return __generator(this, function (_a) {
|
|
63
|
-
if (typeof field == 'string') {
|
|
64
|
-
value = obj[field];
|
|
65
|
-
if (!Constraint_1.default.absent(value)) {
|
|
66
|
-
return [2 /*return*/, new Misfit_1.default];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
else if (field instanceof Array) {
|
|
70
|
-
for (_i = 0, field_1 = field; _i < field_1.length; _i++) {
|
|
71
|
-
fld = field_1[_i];
|
|
72
|
-
value = obj[fld];
|
|
73
|
-
if (!Constraint_1.default.absent(value)) {
|
|
74
|
-
return [2 /*return*/, new Misfit_1.default];
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
throw new Error('Parameter field was neither of type string nor instance of Array');
|
|
80
|
-
}
|
|
81
|
-
return [2 /*return*/];
|
|
82
|
-
});
|
|
12
|
+
exports.Absent = void 0;
|
|
13
|
+
const Constraint_1 = require("../Constraint");
|
|
14
|
+
const Misfit_1 = require("../Misfit");
|
|
15
|
+
class Absent extends Constraint_1.Constraint {
|
|
16
|
+
validate(value) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
return value !== undefined ? new Misfit_1.Misfit(this.name, { actual: value }) : null;
|
|
83
19
|
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
exports.default = Absent;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Absent = Absent;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Constraint } from '../Constraint';
|
|
2
|
+
import { Misfit } from '../Misfit';
|
|
3
|
+
export interface BoundsMisfitValues {
|
|
4
|
+
actual: any;
|
|
5
|
+
greaterThan?: number;
|
|
6
|
+
greaterThanEqual?: number;
|
|
7
|
+
lesserThan?: number;
|
|
8
|
+
lesserThanEqual?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class Bounds extends Constraint<any, BoundsMisfitValues> {
|
|
11
|
+
greaterThan?: number;
|
|
12
|
+
greaterThanEqual?: number;
|
|
13
|
+
lesserThan?: number;
|
|
14
|
+
lesserThanEqual?: number;
|
|
15
|
+
constructor(constraints: Partial<Bounds>);
|
|
16
|
+
validate(value: any): Promise<Misfit<BoundsMisfitValues> | null>;
|
|
17
|
+
private _createMisfit;
|
|
18
|
+
}
|