knight-validation 3.1.1 → 3.3.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/lib/lib/Validator.d.ts +2 -0
- package/lib/lib/Validator.js +66 -13
- package/lib/lib/constraints/TypeOf.js +1 -1
- package/package.json +5 -1
package/lib/lib/Validator.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export declare class Validator<T = any> {
|
|
|
13
13
|
options?: ValidatorOptions;
|
|
14
14
|
entries: ValidatorEntry<T>[];
|
|
15
15
|
constructor(options?: ValidatorOptions);
|
|
16
|
+
add(constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
|
|
17
|
+
add(constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
16
18
|
add(property: string, constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
|
|
17
19
|
add(property: string, constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
18
20
|
add(properties: string[], constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
|
package/lib/lib/Validator.js
CHANGED
|
@@ -10,9 +10,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Validator = void 0;
|
|
13
|
+
const knight_log_1 = require("knight-log");
|
|
13
14
|
const Constraint_1 = require("./Constraint");
|
|
14
15
|
const DotNotation_1 = require("./DotNotation");
|
|
15
16
|
const QuickConstraint_1 = require("./constraints/QuickConstraint");
|
|
17
|
+
let log = new knight_log_1.Log('knight-log/Validator.ts');
|
|
16
18
|
class Validator {
|
|
17
19
|
constructor(options) {
|
|
18
20
|
this.entries = [];
|
|
@@ -25,7 +27,21 @@ class Validator {
|
|
|
25
27
|
this.entries.push(propertyConstraint);
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
|
-
else {
|
|
30
|
+
else if (args[0] instanceof Constraint_1.Constraint) {
|
|
31
|
+
this.entries.push({
|
|
32
|
+
properties: [],
|
|
33
|
+
constraint: args[0],
|
|
34
|
+
condition: args.length > 1 ? args[1] : undefined
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else if (typeof args[0] == 'string' && typeof args[1] == 'function') {
|
|
38
|
+
this.entries.push({
|
|
39
|
+
properties: [],
|
|
40
|
+
constraint: new QuickConstraint_1.QuickConstraint(args[0], args[1]),
|
|
41
|
+
condition: args.length > 2 ? args[2] : undefined
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
else if (typeof args[0] == 'string' || Array.isArray(args[0])) {
|
|
29
45
|
let properties = typeof args[0] == 'string' ? [args[0]] : args[0];
|
|
30
46
|
let constraint = undefined;
|
|
31
47
|
let validator = undefined;
|
|
@@ -60,10 +76,16 @@ class Validator {
|
|
|
60
76
|
}
|
|
61
77
|
validate(object, options) {
|
|
62
78
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
var _a;
|
|
80
|
+
let l = log.mt('validate');
|
|
81
|
+
l.param('object', object);
|
|
82
|
+
l.param('options', options);
|
|
63
83
|
options = options || this.options;
|
|
64
84
|
let misfits = [];
|
|
65
85
|
let misfittingProperties = [];
|
|
66
86
|
for (let entry of this.entries) {
|
|
87
|
+
let constraintOrValidatorName = entry.constraint ? (_a = entry.constraint) === null || _a === void 0 ? void 0 : _a.name : entry.validator ? entry.validator.constructor.name : '';
|
|
88
|
+
l.location = ['' + entry.properties + ' ' + constraintOrValidatorName];
|
|
67
89
|
let propertyAlreadyHasAMisfit = false;
|
|
68
90
|
for (let property of entry.properties) {
|
|
69
91
|
if (misfittingProperties.indexOf(property) > -1) {
|
|
@@ -72,35 +94,56 @@ class Validator {
|
|
|
72
94
|
}
|
|
73
95
|
}
|
|
74
96
|
if (propertyAlreadyHasAMisfit) {
|
|
97
|
+
l.dev('Property already has misfit. Skipping...');
|
|
75
98
|
continue;
|
|
76
99
|
}
|
|
77
100
|
if (entry.condition && !(yield entry.condition(object))) {
|
|
101
|
+
l.dev('The given condition is not met. Skipping...');
|
|
78
102
|
continue;
|
|
79
103
|
}
|
|
80
104
|
let atLeastOnePropertyExists = false;
|
|
105
|
+
l.creator('Check if at least one property exists...');
|
|
81
106
|
for (let property of entry.properties) {
|
|
82
|
-
let
|
|
83
|
-
if (
|
|
107
|
+
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
108
|
+
if (dotNotation.exists(object)) {
|
|
109
|
+
l.creator('Property exists', property);
|
|
84
110
|
atLeastOnePropertyExists = true;
|
|
85
111
|
break;
|
|
86
112
|
}
|
|
113
|
+
l.creator('Property does not exist', property);
|
|
87
114
|
}
|
|
88
|
-
if (!atLeastOnePropertyExists && options && options.checkOnlyWhatIsThere) {
|
|
115
|
+
if (!atLeastOnePropertyExists && options && options.checkOnlyWhatIsThere && misfittingProperties.length > 0) {
|
|
116
|
+
l.dev('Not one of the given properties exist but it should only be checked what is there. Skipping...');
|
|
89
117
|
continue;
|
|
90
118
|
}
|
|
91
119
|
if (entry.constraint != undefined) {
|
|
120
|
+
l.dev('Property will be checked by a single constraint');
|
|
92
121
|
let misfit;
|
|
93
|
-
if (entry.properties.length ==
|
|
122
|
+
if (entry.properties.length == 0) {
|
|
123
|
+
l.dev('Constraint is to be applied to the whole object');
|
|
124
|
+
l.calling('entry.constraint.validate', object);
|
|
125
|
+
misfit = yield entry.constraint.validate(object);
|
|
126
|
+
l.called('entry.constraint.validate');
|
|
127
|
+
}
|
|
128
|
+
else if (entry.properties.length == 1) {
|
|
129
|
+
l.dev('Constraint is to be applied to one property. Fetching its value...');
|
|
94
130
|
let property = entry.properties[0];
|
|
95
|
-
let
|
|
96
|
-
let value =
|
|
131
|
+
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
132
|
+
let value = dotNotation.get(object);
|
|
133
|
+
l.calling('entry.constraint.validate', value);
|
|
97
134
|
misfit = yield entry.constraint.validate(value);
|
|
135
|
+
l.called('entry.constraint.validate');
|
|
98
136
|
}
|
|
99
137
|
else {
|
|
138
|
+
l.dev('Constraint is to be applied tu multiple properties');
|
|
139
|
+
l.calling('entry.constraint.validateMultipleProperties', object);
|
|
100
140
|
misfit = yield entry.constraint.validateMultipleProperties(object, entry.properties);
|
|
141
|
+
l.called('entry.constraint.validateMultipleProperties');
|
|
101
142
|
}
|
|
102
143
|
if (misfit) {
|
|
144
|
+
l.dev('Misfit was returned', misfit);
|
|
103
145
|
if (misfit.constraint === undefined) {
|
|
146
|
+
l.creator('Setting the constraint name on the misfit');
|
|
104
147
|
misfit.constraint = entry.constraint.name;
|
|
105
148
|
}
|
|
106
149
|
misfit.properties = entry.properties.slice();
|
|
@@ -109,19 +152,23 @@ class Validator {
|
|
|
109
152
|
}
|
|
110
153
|
}
|
|
111
154
|
else if (entry.validator != undefined) {
|
|
155
|
+
l.dev('Property will be checked by a validator');
|
|
112
156
|
if (entry.properties.length != 1) {
|
|
113
|
-
|
|
157
|
+
l.error('Cannot apply validator because multiple properties were given');
|
|
158
|
+
throw new Error('Using a whole validator only works for one property');
|
|
114
159
|
}
|
|
115
160
|
let property = entry.properties[0];
|
|
116
|
-
let
|
|
117
|
-
let value =
|
|
118
|
-
if (value === undefined) {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
161
|
+
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
162
|
+
let value = dotNotation.get(object);
|
|
121
163
|
if (value instanceof Array) {
|
|
164
|
+
l.dev('Value of the property is an array. Iterating its elements...');
|
|
122
165
|
for (let i = 0; i < value.length; i++) {
|
|
166
|
+
l.calling('entry.validator.validate', value[i], options);
|
|
123
167
|
let subMisfits = yield entry.validator.validate(value[i], options);
|
|
168
|
+
l.called('entry.validator.validate');
|
|
124
169
|
if (subMisfits.length > 0) {
|
|
170
|
+
l.dev('Validator returned misfits', subMisfits);
|
|
171
|
+
l.creator('Adding prefix to misfit properties...', `${property}[${i}].`);
|
|
125
172
|
for (let misfit of subMisfits) {
|
|
126
173
|
misfit.addPrefix(`${property}[${i}].`);
|
|
127
174
|
}
|
|
@@ -131,8 +178,13 @@ class Validator {
|
|
|
131
178
|
}
|
|
132
179
|
}
|
|
133
180
|
else {
|
|
181
|
+
l.dev('Value of the property is not an array');
|
|
182
|
+
l.calling('entry.validator.validate', value, options);
|
|
134
183
|
let subMisfits = yield entry.validator.validate(value, options);
|
|
184
|
+
l.called('entry.validator.validate');
|
|
135
185
|
if (subMisfits.length > 0) {
|
|
186
|
+
l.dev('Validator returned misfits', subMisfits);
|
|
187
|
+
l.creator('Adding prefix to misfit properties...', property + '.');
|
|
136
188
|
for (let misfit of subMisfits) {
|
|
137
189
|
misfit.addPrefix(property + '.');
|
|
138
190
|
}
|
|
@@ -142,6 +194,7 @@ class Validator {
|
|
|
142
194
|
}
|
|
143
195
|
}
|
|
144
196
|
}
|
|
197
|
+
l.returning('Returning misfits', misfits);
|
|
145
198
|
return misfits;
|
|
146
199
|
});
|
|
147
200
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "knight-validation",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "A validation lib",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -28,11 +28,15 @@
|
|
|
28
28
|
"@types/mocha": "^10.0.10",
|
|
29
29
|
"@types/node": "^24.0.4",
|
|
30
30
|
"chai": "^5.2.0",
|
|
31
|
+
"knight-log": "^2.0.0-rc.3",
|
|
31
32
|
"mocha": "^11.7.1",
|
|
32
33
|
"ts-node": "^10.9.2",
|
|
33
34
|
"typescript": "^5.8.3"
|
|
34
35
|
},
|
|
35
36
|
"dependencies": {
|
|
36
37
|
"knight-misfit": "^1.0.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"knight-log": "^2.0.0-rc.3"
|
|
37
41
|
}
|
|
38
42
|
}
|