@wabot-dev/framework 0.1.0-beta.16 → 0.1.0-beta.17
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/dist/src/index.d.ts
CHANGED
|
@@ -1121,9 +1121,8 @@ interface IValidationError {
|
|
|
1121
1121
|
}
|
|
1122
1122
|
interface IModelValidationError extends IValidationError {
|
|
1123
1123
|
properties: {
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
}[];
|
|
1124
|
+
[key: string]: string[];
|
|
1125
|
+
};
|
|
1127
1126
|
}
|
|
1128
1127
|
type IValidationResult<V> = {
|
|
1129
1128
|
value: V;
|
|
@@ -1166,6 +1165,7 @@ declare class ValidationMetadataStore {
|
|
|
1166
1165
|
private validators;
|
|
1167
1166
|
saveValidatorMetadata(validatorMetadata: IValidatorMetadata): void;
|
|
1168
1167
|
getModelValidatorsInfo<V>(modelConstructor: IConstructor<V>): IModelValidatorsInfo<V>;
|
|
1168
|
+
private getConstructorPropertiesValidatorsInfo;
|
|
1169
1169
|
}
|
|
1170
1170
|
|
|
1171
1171
|
declare function validateIsBoolean(value: any): IValidationResult<boolean>;
|
|
@@ -2,6 +2,15 @@ import { __decorate } from 'tslib';
|
|
|
2
2
|
import { singleton } from '../../injection/index.js';
|
|
3
3
|
import { _IS_OPTIONAL_DUMMY_VALIDATOR_ } from '../validators/validateIsOptional.js';
|
|
4
4
|
|
|
5
|
+
function getClassHierarchy(cls) {
|
|
6
|
+
const classes = [];
|
|
7
|
+
let proto = Object.getPrototypeOf(cls.prototype);
|
|
8
|
+
while (proto && proto.constructor !== Object) {
|
|
9
|
+
classes.push(proto.constructor);
|
|
10
|
+
proto = Object.getPrototypeOf(proto);
|
|
11
|
+
}
|
|
12
|
+
return classes;
|
|
13
|
+
}
|
|
5
14
|
let ValidationMetadataStore = class ValidationMetadataStore {
|
|
6
15
|
validators = new Map();
|
|
7
16
|
saveValidatorMetadata(validatorMetadata) {
|
|
@@ -17,19 +26,25 @@ let ValidationMetadataStore = class ValidationMetadataStore {
|
|
|
17
26
|
propertyValidators.unshift(validatorMetadata);
|
|
18
27
|
}
|
|
19
28
|
getModelValidatorsInfo(modelConstructor) {
|
|
29
|
+
const constructors = getClassHierarchy(modelConstructor);
|
|
30
|
+
constructors.unshift(modelConstructor);
|
|
20
31
|
const modelValidators = {
|
|
21
32
|
modelConstructor: modelConstructor,
|
|
22
|
-
properties: {},
|
|
33
|
+
properties: Object.assign({}, ...constructors.map((x) => this.getConstructorPropertiesValidatorsInfo(x))),
|
|
23
34
|
};
|
|
35
|
+
return modelValidators;
|
|
36
|
+
}
|
|
37
|
+
getConstructorPropertiesValidatorsInfo(modelConstructor) {
|
|
38
|
+
const properties = {};
|
|
24
39
|
[...(this.validators.get(modelConstructor)?.values() ?? [])].forEach((propertyValidatorsMetadata) => {
|
|
25
40
|
const propertyName = propertyValidatorsMetadata.at(0)?.propertyName;
|
|
26
41
|
if (!propertyName) {
|
|
27
42
|
return;
|
|
28
43
|
}
|
|
29
|
-
let propertyInfo =
|
|
44
|
+
let propertyInfo = properties[propertyName];
|
|
30
45
|
if (!propertyInfo) {
|
|
31
46
|
propertyInfo = {};
|
|
32
|
-
|
|
47
|
+
properties[propertyName] = propertyInfo;
|
|
33
48
|
}
|
|
34
49
|
let validators = propertyInfo.validators;
|
|
35
50
|
if (!validators) {
|
|
@@ -45,7 +60,7 @@ let ValidationMetadataStore = class ValidationMetadataStore {
|
|
|
45
60
|
}
|
|
46
61
|
});
|
|
47
62
|
});
|
|
48
|
-
return
|
|
63
|
+
return properties;
|
|
49
64
|
}
|
|
50
65
|
};
|
|
51
66
|
ValidationMetadataStore = __decorate([
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
function validateModel(value, info) {
|
|
2
2
|
if (typeof value !== 'object' || value === null) {
|
|
3
|
-
return { error: { description: 'Invalid object', properties:
|
|
3
|
+
return { error: { description: 'Invalid object', properties: {} } };
|
|
4
4
|
}
|
|
5
|
-
let propertiesErrors =
|
|
5
|
+
let propertiesErrors = {};
|
|
6
6
|
let resultValue = new info.modelConstructor();
|
|
7
7
|
for (const propertyName in info.properties) {
|
|
8
8
|
const propertyInfo = info.properties[propertyName];
|
|
@@ -14,18 +14,20 @@ function validateModel(value, info) {
|
|
|
14
14
|
}
|
|
15
15
|
for (let propertyValidatorInfo of propertyValidators) {
|
|
16
16
|
const propertyValidatorResult = propertyValidatorInfo.validator(resultValue[propertyName], propertyValidatorInfo.validatorOptions);
|
|
17
|
-
resultValue[propertyName] = propertyValidatorResult.value;
|
|
18
17
|
if (propertyValidatorResult.error) {
|
|
19
|
-
let propertyErrors = propertiesErrors
|
|
18
|
+
let propertyErrors = propertiesErrors[propertyName];
|
|
20
19
|
if (!propertyErrors) {
|
|
21
|
-
propertyErrors =
|
|
22
|
-
propertiesErrors
|
|
20
|
+
propertyErrors = [];
|
|
21
|
+
propertiesErrors[propertyName] = propertyErrors;
|
|
23
22
|
}
|
|
24
|
-
propertyErrors.
|
|
23
|
+
propertyErrors.push(propertyValidatorResult.error.description);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
resultValue[propertyName] = propertyValidatorResult.value;
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
|
-
if (propertiesErrors.length > 0) {
|
|
30
|
+
if (Object.keys(propertiesErrors).length > 0) {
|
|
29
31
|
return { error: { description: 'Invalid properties', properties: propertiesErrors } };
|
|
30
32
|
}
|
|
31
33
|
return { value: resultValue };
|