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

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.
@@ -8,6 +8,7 @@ class CustomError extends Error {
8
8
  this.humanMessage = data.humanMessage;
9
9
  this.code = data.code;
10
10
  this.httpCode = data.httpCode;
11
+ this.info = data.info;
11
12
  }
12
13
  }
13
14
 
@@ -1121,9 +1121,8 @@ interface IValidationError {
1121
1121
  }
1122
1122
  interface IModelValidationError extends IValidationError {
1123
1123
  properties: {
1124
- name: string;
1125
- errors: IValidationError[];
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 = modelValidators.properties[propertyName];
44
+ let propertyInfo = properties[propertyName];
30
45
  if (!propertyInfo) {
31
46
  propertyInfo = {};
32
- modelValidators.properties[propertyName] = propertyInfo;
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 modelValidators;
63
+ return properties;
49
64
  }
50
65
  };
51
66
  ValidationMetadataStore = __decorate([
@@ -1,31 +1,31 @@
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];
9
9
  const propertyValidators = propertyInfo.validators ?? [];
10
10
  resultValue[propertyName] = value[propertyName] ?? resultValue[propertyName];
11
11
  if (resultValue[propertyName] == null && propertyInfo.isOptional) {
12
- resultValue[propertyName] = null;
12
+ resultValue[propertyName] = undefined;
13
13
  continue;
14
14
  }
15
15
  for (let propertyValidatorInfo of propertyValidators) {
16
16
  const propertyValidatorResult = propertyValidatorInfo.validator(resultValue[propertyName], propertyValidatorInfo.validatorOptions);
17
17
  resultValue[propertyName] = propertyValidatorResult.value;
18
18
  if (propertyValidatorResult.error) {
19
- let propertyErrors = propertiesErrors.find((x) => x.name === propertyName);
19
+ let propertyErrors = propertiesErrors[propertyName];
20
20
  if (!propertyErrors) {
21
- propertyErrors = { name: propertyName, errors: [] };
22
- propertiesErrors.push(propertyErrors);
21
+ propertyErrors = [];
22
+ propertiesErrors[propertyName] = propertyErrors;
23
23
  }
24
- propertyErrors.errors.push(propertyValidatorResult.error);
24
+ propertyErrors.push(propertyValidatorResult.error.description);
25
25
  }
26
26
  }
27
27
  }
28
- if (propertiesErrors.length > 0) {
28
+ if (Object.keys(propertiesErrors).length > 0) {
29
29
  return { error: { description: 'Invalid properties', properties: propertiesErrors } };
30
30
  }
31
31
  return { value: resultValue };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wabot-dev/framework",
3
- "version": "0.1.0-beta.16",
3
+ "version": "0.1.0-beta.18",
4
4
  "description": "Framework for IA Chat Bots",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",