knight-validation 3.1.0 → 3.2.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 +5 -3
- package/lib/lib/Validator.js +25 -8
- package/package.json +1 -1
package/lib/lib/Validator.d.ts
CHANGED
|
@@ -13,12 +13,14 @@ export declare class Validator<T = any> {
|
|
|
13
13
|
options?: ValidatorOptions;
|
|
14
14
|
entries: ValidatorEntry<T>[];
|
|
15
15
|
constructor(options?: ValidatorOptions);
|
|
16
|
-
add(
|
|
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;
|
|
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
|
-
add(properties: string[], constraint: Constraint
|
|
20
|
+
add(properties: string[], constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
|
|
19
21
|
add(properties: string[], constraintName: string, validate: (object: T, properties: string[]) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
|
|
20
22
|
add(property: string, validator: Validator<any>, condition?: (object: T) => Promise<boolean>): void;
|
|
21
|
-
add(validator: Validator<
|
|
23
|
+
add(validator: Validator<T>): void;
|
|
22
24
|
validate(object: T, options?: ValidatorOptions): Promise<Misfit[]>;
|
|
23
25
|
}
|
|
24
26
|
export {};
|
package/lib/lib/Validator.js
CHANGED
|
@@ -25,7 +25,21 @@ class Validator {
|
|
|
25
25
|
this.entries.push(propertyConstraint);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
else {
|
|
28
|
+
else if (args[0] instanceof Constraint_1.Constraint) {
|
|
29
|
+
this.entries.push({
|
|
30
|
+
properties: [],
|
|
31
|
+
constraint: args[0],
|
|
32
|
+
condition: args.length > 1 ? args[1] : undefined
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else if (typeof args[0] == 'string' && typeof args[1] == 'function') {
|
|
36
|
+
this.entries.push({
|
|
37
|
+
properties: [],
|
|
38
|
+
constraint: new QuickConstraint_1.QuickConstraint(args[0], args[1]),
|
|
39
|
+
condition: args.length > 2 ? args[2] : undefined
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
else if (typeof args[0] == 'string' || Array.isArray(args[0])) {
|
|
29
43
|
let properties = typeof args[0] == 'string' ? [args[0]] : args[0];
|
|
30
44
|
let constraint = undefined;
|
|
31
45
|
let validator = undefined;
|
|
@@ -79,8 +93,8 @@ class Validator {
|
|
|
79
93
|
}
|
|
80
94
|
let atLeastOnePropertyExists = false;
|
|
81
95
|
for (let property of entry.properties) {
|
|
82
|
-
let
|
|
83
|
-
if (
|
|
96
|
+
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
97
|
+
if (dotNotation.exists(object)) {
|
|
84
98
|
atLeastOnePropertyExists = true;
|
|
85
99
|
break;
|
|
86
100
|
}
|
|
@@ -90,10 +104,13 @@ class Validator {
|
|
|
90
104
|
}
|
|
91
105
|
if (entry.constraint != undefined) {
|
|
92
106
|
let misfit;
|
|
93
|
-
if (entry.properties.length ==
|
|
107
|
+
if (entry.properties.length == 0) {
|
|
108
|
+
misfit = yield entry.constraint.validate(object);
|
|
109
|
+
}
|
|
110
|
+
else if (entry.properties.length == 1) {
|
|
94
111
|
let property = entry.properties[0];
|
|
95
|
-
let
|
|
96
|
-
let value =
|
|
112
|
+
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
113
|
+
let value = dotNotation.get(object);
|
|
97
114
|
misfit = yield entry.constraint.validate(value);
|
|
98
115
|
}
|
|
99
116
|
else {
|
|
@@ -113,8 +130,8 @@ class Validator {
|
|
|
113
130
|
throw new Error('Using another validator only works for one property');
|
|
114
131
|
}
|
|
115
132
|
let property = entry.properties[0];
|
|
116
|
-
let
|
|
117
|
-
let value =
|
|
133
|
+
let dotNotation = new DotNotation_1.DotNotation(property);
|
|
134
|
+
let value = dotNotation.get(object);
|
|
118
135
|
if (value === undefined) {
|
|
119
136
|
continue;
|
|
120
137
|
}
|