knight-validation 3.2.0 → 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.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 = [];
|
|
@@ -74,10 +76,16 @@ class Validator {
|
|
|
74
76
|
}
|
|
75
77
|
validate(object, options) {
|
|
76
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);
|
|
77
83
|
options = options || this.options;
|
|
78
84
|
let misfits = [];
|
|
79
85
|
let misfittingProperties = [];
|
|
80
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];
|
|
81
89
|
let propertyAlreadyHasAMisfit = false;
|
|
82
90
|
for (let property of entry.properties) {
|
|
83
91
|
if (misfittingProperties.indexOf(property) > -1) {
|
|
@@ -86,38 +94,56 @@ class Validator {
|
|
|
86
94
|
}
|
|
87
95
|
}
|
|
88
96
|
if (propertyAlreadyHasAMisfit) {
|
|
97
|
+
l.dev('Property already has misfit. Skipping...');
|
|
89
98
|
continue;
|
|
90
99
|
}
|
|
91
100
|
if (entry.condition && !(yield entry.condition(object))) {
|
|
101
|
+
l.dev('The given condition is not met. Skipping...');
|
|
92
102
|
continue;
|
|
93
103
|
}
|
|
94
104
|
let atLeastOnePropertyExists = false;
|
|
105
|
+
l.creator('Check if at least one property exists...');
|
|
95
106
|
for (let property of entry.properties) {
|
|
96
107
|
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
97
108
|
if (dotNotation.exists(object)) {
|
|
109
|
+
l.creator('Property exists', property);
|
|
98
110
|
atLeastOnePropertyExists = true;
|
|
99
111
|
break;
|
|
100
112
|
}
|
|
113
|
+
l.creator('Property does not exist', property);
|
|
101
114
|
}
|
|
102
|
-
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...');
|
|
103
117
|
continue;
|
|
104
118
|
}
|
|
105
119
|
if (entry.constraint != undefined) {
|
|
120
|
+
l.dev('Property will be checked by a single constraint');
|
|
106
121
|
let misfit;
|
|
107
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);
|
|
108
125
|
misfit = yield entry.constraint.validate(object);
|
|
126
|
+
l.called('entry.constraint.validate');
|
|
109
127
|
}
|
|
110
128
|
else if (entry.properties.length == 1) {
|
|
129
|
+
l.dev('Constraint is to be applied to one property. Fetching its value...');
|
|
111
130
|
let property = entry.properties[0];
|
|
112
131
|
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
113
132
|
let value = dotNotation.get(object);
|
|
133
|
+
l.calling('entry.constraint.validate', value);
|
|
114
134
|
misfit = yield entry.constraint.validate(value);
|
|
135
|
+
l.called('entry.constraint.validate');
|
|
115
136
|
}
|
|
116
137
|
else {
|
|
138
|
+
l.dev('Constraint is to be applied tu multiple properties');
|
|
139
|
+
l.calling('entry.constraint.validateMultipleProperties', object);
|
|
117
140
|
misfit = yield entry.constraint.validateMultipleProperties(object, entry.properties);
|
|
141
|
+
l.called('entry.constraint.validateMultipleProperties');
|
|
118
142
|
}
|
|
119
143
|
if (misfit) {
|
|
144
|
+
l.dev('Misfit was returned', misfit);
|
|
120
145
|
if (misfit.constraint === undefined) {
|
|
146
|
+
l.creator('Setting the constraint name on the misfit');
|
|
121
147
|
misfit.constraint = entry.constraint.name;
|
|
122
148
|
}
|
|
123
149
|
misfit.properties = entry.properties.slice();
|
|
@@ -126,19 +152,23 @@ class Validator {
|
|
|
126
152
|
}
|
|
127
153
|
}
|
|
128
154
|
else if (entry.validator != undefined) {
|
|
155
|
+
l.dev('Property will be checked by a validator');
|
|
129
156
|
if (entry.properties.length != 1) {
|
|
130
|
-
|
|
157
|
+
l.error('Cannot apply validator because multiple properties were given');
|
|
158
|
+
throw new Error('Using a whole validator only works for one property');
|
|
131
159
|
}
|
|
132
160
|
let property = entry.properties[0];
|
|
133
161
|
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
134
162
|
let value = dotNotation.get(object);
|
|
135
|
-
if (value === undefined) {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
163
|
if (value instanceof Array) {
|
|
164
|
+
l.dev('Value of the property is an array. Iterating its elements...');
|
|
139
165
|
for (let i = 0; i < value.length; i++) {
|
|
166
|
+
l.calling('entry.validator.validate', value[i], options);
|
|
140
167
|
let subMisfits = yield entry.validator.validate(value[i], options);
|
|
168
|
+
l.called('entry.validator.validate');
|
|
141
169
|
if (subMisfits.length > 0) {
|
|
170
|
+
l.dev('Validator returned misfits', subMisfits);
|
|
171
|
+
l.creator('Adding prefix to misfit properties...', `${property}[${i}].`);
|
|
142
172
|
for (let misfit of subMisfits) {
|
|
143
173
|
misfit.addPrefix(`${property}[${i}].`);
|
|
144
174
|
}
|
|
@@ -148,8 +178,13 @@ class Validator {
|
|
|
148
178
|
}
|
|
149
179
|
}
|
|
150
180
|
else {
|
|
181
|
+
l.dev('Value of the property is not an array');
|
|
182
|
+
l.calling('entry.validator.validate', value, options);
|
|
151
183
|
let subMisfits = yield entry.validator.validate(value, options);
|
|
184
|
+
l.called('entry.validator.validate');
|
|
152
185
|
if (subMisfits.length > 0) {
|
|
186
|
+
l.dev('Validator returned misfits', subMisfits);
|
|
187
|
+
l.creator('Adding prefix to misfit properties...', property + '.');
|
|
153
188
|
for (let misfit of subMisfits) {
|
|
154
189
|
misfit.addPrefix(property + '.');
|
|
155
190
|
}
|
|
@@ -159,6 +194,7 @@ class Validator {
|
|
|
159
194
|
}
|
|
160
195
|
}
|
|
161
196
|
}
|
|
197
|
+
l.returning('Returning misfits', misfits);
|
|
162
198
|
return misfits;
|
|
163
199
|
});
|
|
164
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
|
}
|