@wabot-dev/framework 0.1.0-beta.15 → 0.1.0-beta.16

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.
@@ -1072,7 +1072,7 @@ declare class RestControllerMetadataStore {
1072
1072
  }[];
1073
1073
  }
1074
1074
 
1075
- declare function runRestControllers(controllers: IConstructor<any>[], container: DependencyContainer$1): void;
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
 
@@ -1125,14 +1125,20 @@ interface IModelValidationError extends IValidationError {
1125
1125
  errors: IValidationError[];
1126
1126
  }[];
1127
1127
  }
1128
- interface IValidationResult<V> {
1129
- value?: V;
1130
- error?: IValidationError;
1131
- }
1132
- interface IModelValidationResult<V> {
1133
- value?: V;
1134
- error?: IModelValidationError;
1135
- }
1128
+ type IValidationResult<V> = {
1129
+ value: V;
1130
+ error?: undefined;
1131
+ } | {
1132
+ value?: undefined;
1133
+ error: IValidationError;
1134
+ };
1135
+ type IModelValidationResult<V> = {
1136
+ value: V;
1137
+ error?: undefined;
1138
+ } | {
1139
+ value?: undefined;
1140
+ error: IModelValidationError;
1141
+ };
1136
1142
  type IValidator = (value: any, options: any) => IValidationResult<any>;
1137
1143
  interface IPropertyValidatorInfo {
1138
1144
  propertyName: string;
@@ -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, container) {
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);
@@ -1,43 +1,34 @@
1
1
  function validateModel(value, info) {
2
- const result = {};
3
- if (typeof value !== 'object') {
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
- const validatedValue = new info.modelConstructor();
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
- validatedValue[propertyName] = value[propertyName] ?? validatedValue[propertyName];
15
- if (validatedValue[propertyName] == null && propertyInfo.isOptional) {
16
- validatedValue[propertyName] = null;
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(validatedValue[propertyName], propertyValidatorInfo.validatorOptions);
21
- validatedValue[propertyName] = propertyValidatorResult.value;
16
+ const propertyValidatorResult = propertyValidatorInfo.validator(resultValue[propertyName], propertyValidatorInfo.validatorOptions);
17
+ resultValue[propertyName] = propertyValidatorResult.value;
22
18
  if (propertyValidatorResult.error) {
23
- let resultError = result.error;
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);
19
+ let propertyErrors = propertiesErrors.find((x) => x.name === propertyName);
29
20
  if (!propertyErrors) {
30
21
  propertyErrors = { name: propertyName, errors: [] };
31
- resultError.properties.push(propertyErrors);
22
+ propertiesErrors.push(propertyErrors);
32
23
  }
33
24
  propertyErrors.errors.push(propertyValidatorResult.error);
34
25
  }
35
26
  }
36
27
  }
37
- if (!result.error) {
38
- result.value = validatedValue;
28
+ if (propertiesErrors.length > 0) {
29
+ return { error: { description: 'Invalid properties', properties: propertiesErrors } };
39
30
  }
40
- return result;
31
+ return { value: resultValue };
41
32
  }
42
33
 
43
34
  export { validateModel };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.1.0-beta.15",
3
+ "version": "0.1.0-beta.16",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",