@wabot-dev/framework 0.1.0-beta.15 → 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/error/CustomError.js +1 -0
- package/dist/src/index.d.ts +18 -12
- package/dist/src/rest-controller/runRestControllers.js +2 -2
- package/dist/src/validation/metadata/ValidationMetadataStore.js +19 -4
- package/dist/src/validation/validators/validateModel.js +18 -25
- package/package.json +1 -1
package/dist/src/index.d.ts
CHANGED
|
@@ -1072,7 +1072,7 @@ declare class RestControllerMetadataStore {
|
|
|
1072
1072
|
}[];
|
|
1073
1073
|
}
|
|
1074
1074
|
|
|
1075
|
-
declare function runRestControllers(controllers: IConstructor<any>[]
|
|
1075
|
+
declare function runRestControllers(controllers: IConstructor<any>[]): void;
|
|
1076
1076
|
|
|
1077
1077
|
declare function prepareChatContainer(container: DependencyContainer$1, context: IMessageContext, mindsetCtor?: IConstructor<IMindset>): Promise<DependencyContainer$1>;
|
|
1078
1078
|
|
|
@@ -1121,18 +1121,23 @@ interface IValidationError {
|
|
|
1121
1121
|
}
|
|
1122
1122
|
interface IModelValidationError extends IValidationError {
|
|
1123
1123
|
properties: {
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
}[];
|
|
1127
|
-
}
|
|
1128
|
-
interface IValidationResult<V> {
|
|
1129
|
-
value?: V;
|
|
1130
|
-
error?: IValidationError;
|
|
1131
|
-
}
|
|
1132
|
-
interface IModelValidationResult<V> {
|
|
1133
|
-
value?: V;
|
|
1134
|
-
error?: IModelValidationError;
|
|
1124
|
+
[key: string]: string[];
|
|
1125
|
+
};
|
|
1135
1126
|
}
|
|
1127
|
+
type IValidationResult<V> = {
|
|
1128
|
+
value: V;
|
|
1129
|
+
error?: undefined;
|
|
1130
|
+
} | {
|
|
1131
|
+
value?: undefined;
|
|
1132
|
+
error: IValidationError;
|
|
1133
|
+
};
|
|
1134
|
+
type IModelValidationResult<V> = {
|
|
1135
|
+
value: V;
|
|
1136
|
+
error?: undefined;
|
|
1137
|
+
} | {
|
|
1138
|
+
value?: undefined;
|
|
1139
|
+
error: IModelValidationError;
|
|
1140
|
+
};
|
|
1136
1141
|
type IValidator = (value: any, options: any) => IValidationResult<any>;
|
|
1137
1142
|
interface IPropertyValidatorInfo {
|
|
1138
1143
|
propertyName: string;
|
|
@@ -1160,6 +1165,7 @@ declare class ValidationMetadataStore {
|
|
|
1160
1165
|
private validators;
|
|
1161
1166
|
saveValidatorMetadata(validatorMetadata: IValidatorMetadata): void;
|
|
1162
1167
|
getModelValidatorsInfo<V>(modelConstructor: IConstructor<V>): IModelValidatorsInfo<V>;
|
|
1168
|
+
private getConstructorPropertiesValidatorsInfo;
|
|
1163
1169
|
}
|
|
1164
1170
|
|
|
1165
1171
|
declare function validateIsBoolean(value: any): IValidationResult<boolean>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '../controller/channel/ChatResolver.js';
|
|
2
2
|
import '../controller/channel/UserResolver.js';
|
|
3
|
-
import '../injection/index.js';
|
|
3
|
+
import { container } from '../injection/index.js';
|
|
4
4
|
import '../controller/metadata/ControllerMetadataStore.js';
|
|
5
5
|
import '../channels/cmd/CmdChannel.js';
|
|
6
6
|
import { ExpressProvider } from '../channels/express/ExpressProvider.js';
|
|
@@ -28,7 +28,7 @@ import { CustomError } from '../error/CustomError.js';
|
|
|
28
28
|
function buildRequest(req) {
|
|
29
29
|
return Object.assign({}, req.body, req.query, req.params);
|
|
30
30
|
}
|
|
31
|
-
function runRestControllers(controllers
|
|
31
|
+
function runRestControllers(controllers) {
|
|
32
32
|
const logger = new Logger('wabot:rest');
|
|
33
33
|
const metadataStore = container.resolve(RestControllerMetadataStore);
|
|
34
34
|
const expressProvider = container.resolve(ExpressProvider);
|
|
@@ -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,43 +1,36 @@
|
|
|
1
1
|
function validateModel(value, info) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
result.error = {
|
|
5
|
-
description: 'the value should be an object',
|
|
6
|
-
properties: [],
|
|
7
|
-
};
|
|
8
|
-
return result;
|
|
2
|
+
if (typeof value !== 'object' || value === null) {
|
|
3
|
+
return { error: { description: 'Invalid object', properties: {} } };
|
|
9
4
|
}
|
|
10
|
-
|
|
5
|
+
let propertiesErrors = {};
|
|
6
|
+
let resultValue = new info.modelConstructor();
|
|
11
7
|
for (const propertyName in info.properties) {
|
|
12
8
|
const propertyInfo = info.properties[propertyName];
|
|
13
9
|
const propertyValidators = propertyInfo.validators ?? [];
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
|
|
10
|
+
resultValue[propertyName] = value[propertyName] ?? resultValue[propertyName];
|
|
11
|
+
if (resultValue[propertyName] == null && propertyInfo.isOptional) {
|
|
12
|
+
resultValue[propertyName] = null;
|
|
17
13
|
continue;
|
|
18
14
|
}
|
|
19
15
|
for (let propertyValidatorInfo of propertyValidators) {
|
|
20
|
-
const propertyValidatorResult = propertyValidatorInfo.validator(
|
|
21
|
-
validatedValue[propertyName] = propertyValidatorResult.value;
|
|
16
|
+
const propertyValidatorResult = propertyValidatorInfo.validator(resultValue[propertyName], propertyValidatorInfo.validatorOptions);
|
|
22
17
|
if (propertyValidatorResult.error) {
|
|
23
|
-
let
|
|
24
|
-
if (!resultError) {
|
|
25
|
-
resultError = { description: `Error on properties`, properties: [] };
|
|
26
|
-
result.error = resultError;
|
|
27
|
-
}
|
|
28
|
-
let propertyErrors = resultError.properties.find((x) => x.name === propertyName);
|
|
18
|
+
let propertyErrors = propertiesErrors[propertyName];
|
|
29
19
|
if (!propertyErrors) {
|
|
30
|
-
propertyErrors =
|
|
31
|
-
|
|
20
|
+
propertyErrors = [];
|
|
21
|
+
propertiesErrors[propertyName] = propertyErrors;
|
|
32
22
|
}
|
|
33
|
-
propertyErrors.
|
|
23
|
+
propertyErrors.push(propertyValidatorResult.error.description);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
resultValue[propertyName] = propertyValidatorResult.value;
|
|
34
27
|
}
|
|
35
28
|
}
|
|
36
29
|
}
|
|
37
|
-
if (
|
|
38
|
-
|
|
30
|
+
if (Object.keys(propertiesErrors).length > 0) {
|
|
31
|
+
return { error: { description: 'Invalid properties', properties: propertiesErrors } };
|
|
39
32
|
}
|
|
40
|
-
return
|
|
33
|
+
return { value: resultValue };
|
|
41
34
|
}
|
|
42
35
|
|
|
43
36
|
export { validateModel };
|