@tsed/ajv 7.0.0-beta.9 → 7.0.0-rc.3

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/jest.config.js ADDED
@@ -0,0 +1,14 @@
1
+ // For a detailed explanation regarding each configuration property, visit:
2
+ // https://jestjs.io/docs/en/configuration.html
3
+
4
+ module.exports = {
5
+ ...require("@tsed/jest-config")(__dirname, "ajv"),
6
+ coverageThreshold: {
7
+ global: {
8
+ branches: 85.18,
9
+ functions: 100,
10
+ lines: 99,
11
+ statements: 99.03
12
+ }
13
+ }
14
+ };
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AjvValidationError = void 0;
4
4
  const exceptions_1 = require("@tsed/exceptions");
5
5
  class AjvValidationError extends exceptions_1.BadRequest {
6
+ name = "AJV_VALIDATION_ERROR";
7
+ errors;
6
8
  constructor(message, errors = []) {
7
9
  super(message);
8
- this.name = "AJV_VALIDATION_ERROR";
9
10
  this.errors = errors;
10
11
  }
11
12
  }
@@ -1 +1 @@
1
- {"version":3,"file":"AjvValidationError.js","sourceRoot":"","sources":["../../../src/errors/AjvValidationError.ts"],"names":[],"mappings":";;;AAAA,iDAA4C;AAE5C,MAAa,kBAAmB,SAAQ,uBAAU;IAIhD,YAAY,OAAe,EAAE,SAAgB,EAAE;QAC7C,KAAK,CAAC,OAAO,CAAC,CAAC;QAJV,SAAI,GAAW,sBAAsB,CAAC;QAK3C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AARD,gDAQC","sourcesContent":["import {BadRequest} from \"@tsed/exceptions\";\n\nexport class AjvValidationError extends BadRequest {\n public name: string = \"AJV_VALIDATION_ERROR\";\n public errors: any[];\n\n constructor(message: string, errors: any[] = []) {\n super(message);\n this.errors = errors;\n }\n}\n"]}
1
+ {"version":3,"file":"AjvValidationError.js","sourceRoot":"","sources":["../../../src/errors/AjvValidationError.ts"],"names":[],"mappings":";;;AAAA,iDAA4C;AAE5C,MAAa,kBAAmB,SAAQ,uBAAU;IACzC,IAAI,GAAW,sBAAsB,CAAC;IACtC,MAAM,CAAQ;IAErB,YAAY,OAAe,EAAE,SAAgB,EAAE;QAC7C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AARD,gDAQC","sourcesContent":["import {BadRequest} from \"@tsed/exceptions\";\n\nexport class AjvValidationError extends BadRequest {\n public name: string = \"AJV_VALIDATION_ERROR\";\n public errors: any[];\n\n constructor(message: string, errors: any[] = []) {\n super(message);\n this.errors = errors;\n }\n}\n"]}
@@ -32,7 +32,6 @@ function getFormatsProviders(injector) {
32
32
  }
33
33
  function getFormats(injector) {
34
34
  return getFormatsProviders(injector).map((provider) => {
35
- var _a;
36
35
  const { name, options } = provider.store.get("ajv:formats", {});
37
36
  const service = injector.invoke(provider.token);
38
37
  return {
@@ -40,7 +39,7 @@ function getFormats(injector) {
40
39
  options: {
41
40
  ...options,
42
41
  validate: service.validate.bind(service),
43
- compare: (_a = service.compare) === null || _a === void 0 ? void 0 : _a.bind(service)
42
+ compare: service.compare?.bind(service)
44
43
  }
45
44
  };
46
45
  });
@@ -1 +1 @@
1
- {"version":3,"file":"Ajv.js","sourceRoot":"","sources":["../../../src/services/Ajv.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,iCAAyF;AACzF,sDAA+D;AAC/D,sEAAqC;AAIrC,SAAS,UAAU,CAAC,GAAW,EAAE,OAAY;IAC3C,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE;QAChB,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;KACnC;AACH,CAAC;AAED,SAAS,mBAAmB,CAAC,QAAyB;IACpD,OAAO,QAAQ,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;AAC9C,CAAC;AAED,SAAS,YAAY,CAAC,QAAyB;IAC7C,OAAO,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C,CAAC,CAAC;AACL,CAAC;AAED,IAAA,qBAAgB,EAAC;IACf,OAAO,EAAE,aAAG;IACZ,IAAI,EAAE,CAAC,kBAAa,EAAE,oBAAe,CAAC;IACtC,KAAK,EAAE,kBAAa,CAAC,SAAS;IAC9B,UAAU,CAAC,aAA4B,EAAE,QAAyB;QAChE,MAAM,EAAC,cAAc,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,KAAK,EAAC,GAAG,aAAa,CAAC,GAAG,CAAe,KAAK,CAAC,IAAI,EAAE,CAAC;QAC/F,MAAM,OAAO,GAAG;YACd,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;YACjB,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,CAAC,GAAG,QAAQ,EAAE,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;YAClD,GAAG,KAAK;SACT,CAAC;QAEF,MAAM,GAAG,GAAG,IAAI,aAAG,CAAC,OAAO,CAAC,CAAC;QAE7B,IAAA,qBAAU,EAAC,GAAU,CAAC,CAAC;QAEvB,UAAU,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,EAAC,IAAI,EAAE,OAAO,EAAC,EAAE,EAAE;YAC/C,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACb,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {cleanObject} from \"@tsed/core\";\nimport {Configuration, InjectorService, ProviderScope, registerProvider} from \"@tsed/di\";\nimport Ajv, {Format, KeywordDefinition, Vocabulary} from \"ajv\";\nimport AjvFormats from \"ajv-formats\";\nimport {FormatsMethods} from \"../interfaces/FormatsMethods\";\nimport {IAjvSettings} from \"../interfaces/IAjvSettings\";\n\nfunction getHandler(key: string, service: any) {\n if (service[key]) {\n return service[key].bind(service);\n }\n}\n\nfunction getKeywordProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:keyword\");\n}\n\nfunction bindKeywords(injector: InjectorService): Vocabulary {\n return getKeywordProviders(injector).map((provider) => {\n const options = provider.store.get<Omit<KeywordDefinition, \"compile\">>(\"ajv:keyword\", {})!;\n const service = injector.invoke(provider.token);\n\n return <KeywordDefinition>cleanObject({\n coerceTypes: \"array\",\n ...options,\n validate: getHandler(\"validate\", service),\n compile: getHandler(\"compile\", service),\n code: getHandler(\"code\", service),\n macro: getHandler(\"macro\", service)\n });\n });\n}\n\nfunction getFormatsProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:formats\");\n}\n\nfunction getFormats(injector: InjectorService): {name: string; options: Format}[] {\n return getFormatsProviders(injector).map((provider) => {\n const {name, options} = provider.store.get<any>(\"ajv:formats\", {})!;\n const service = injector.invoke<FormatsMethods<any>>(provider.token);\n\n return {\n name,\n options: {\n ...options,\n validate: service.validate.bind(service),\n compare: service.compare?.bind(service)\n }\n };\n });\n}\n\nregisterProvider({\n provide: Ajv,\n deps: [Configuration, InjectorService],\n scope: ProviderScope.SINGLETON,\n useFactory(configuration: Configuration, injector: InjectorService) {\n const {errorFormatter, keywords = [], ...props} = configuration.get<IAjvSettings>(\"ajv\") || {};\n const options = {\n verbose: false,\n coerceTypes: true,\n strict: false,\n keywords: [...keywords, ...bindKeywords(injector)],\n ...props\n };\n\n const ajv = new Ajv(options);\n\n AjvFormats(ajv as any);\n\n getFormats(injector).forEach(({name, options}) => {\n ajv.addFormat(name, options);\n });\n\n return ajv;\n }\n});\n"]}
1
+ {"version":3,"file":"Ajv.js","sourceRoot":"","sources":["../../../src/services/Ajv.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,iCAAyF;AACzF,sDAA+D;AAC/D,sEAAqC;AAIrC,SAAS,UAAU,CAAC,GAAW,EAAE,OAAY;IAC3C,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE;QAChB,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;KACnC;AACH,CAAC;AAED,SAAS,m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CAAC,IAAI,CAAC,OAAO,CAAC;gBACxC,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC;aACxC;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,IAAA,qBAAgB,EAAC;IACf,OAAO,EAAE,aAAG;IACZ,IAAI,EAAE,CAAC,kBAAa,EAAE,oBAAe,CAAC;IACtC,KAAK,EAAE,kBAAa,CAAC,SAAS;IAC9B,UAAU,CAAC,aAA4B,EAAE,QAAyB;QAChE,MAAM,EAAC,cAAc,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,KAAK,EAAC,GAAG,aAAa,CAAC,GAAG,CAAe,KAAK,CAAC,IAAI,EAAE,CAAC;QAC/F,MAAM,OAAO,GAAG;YACd,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;YACjB,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,CAAC,GAAG,QAAQ,EAAE,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;YAClD,GAAG,KAAK;SACT,CAAC;QAEF,MAAM,GAAG,GAAG,IAAI,aAAG,CAAC,OAAO,CAAC,CAAC;QAE7B,IAAA,qBAAU,EAAC,GAAU,CAAC,CAAC;QAEvB,UAAU,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,EAAC,IAAI,EAAE,OAAO,EAAC,EAAE,EAAE;YAC/C,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACb,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {cleanObject} from \"@tsed/core\";\nimport {Configuration, InjectorService, ProviderScope, registerProvider} from \"@tsed/di\";\nimport Ajv, {Format, KeywordDefinition, Vocabulary} from \"ajv\";\nimport AjvFormats from \"ajv-formats\";\nimport {FormatsMethods} from \"../interfaces/FormatsMethods\";\nimport {IAjvSettings} from \"../interfaces/IAjvSettings\";\n\nfunction getHandler(key: string, service: any) {\n if (service[key]) {\n return service[key].bind(service);\n }\n}\n\nfunction getKeywordProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:keyword\");\n}\n\nfunction bindKeywords(injector: InjectorService): Vocabulary {\n return getKeywordProviders(injector).map((provider) => {\n const options = provider.store.get<Omit<KeywordDefinition, \"compile\">>(\"ajv:keyword\", {})!;\n const service = injector.invoke(provider.token);\n\n return <KeywordDefinition>cleanObject({\n coerceTypes: \"array\",\n ...options,\n validate: getHandler(\"validate\", service),\n compile: getHandler(\"compile\", service),\n code: getHandler(\"code\", service),\n macro: getHandler(\"macro\", service)\n });\n });\n}\n\nfunction getFormatsProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:formats\");\n}\n\nfunction getFormats(injector: InjectorService): {name: string; options: Format}[] {\n return getFormatsProviders(injector).map((provider) => {\n const {name, options} = provider.store.get<any>(\"ajv:formats\", {})!;\n const service = injector.invoke<FormatsMethods<any>>(provider.token);\n\n return {\n name,\n options: {\n ...options,\n validate: service.validate.bind(service),\n compare: service.compare?.bind(service)\n }\n };\n });\n}\n\nregisterProvider({\n provide: Ajv,\n deps: [Configuration, InjectorService],\n scope: ProviderScope.SINGLETON,\n useFactory(configuration: Configuration, injector: InjectorService) {\n const {errorFormatter, keywords = [], ...props} = configuration.get<IAjvSettings>(\"ajv\") || {};\n const options = {\n verbose: false,\n coerceTypes: true,\n strict: false,\n keywords: [...keywords, ...bindKeywords(injector)],\n ...props\n };\n\n const ajv = new Ajv(options);\n\n AjvFormats(ajv as any);\n\n getFormats(injector).forEach(({name, options}) => {\n ajv.addFormat(name, options);\n });\n\n return ajv;\n }\n});\n"]}
@@ -11,14 +11,18 @@ const defaultErrorFormatter_1 = require("../utils/defaultErrorFormatter");
11
11
  require("./Ajv");
12
12
  const getPath_1 = require("../utils/getPath");
13
13
  let AjvService = class AjvService {
14
+ errorFormatter;
15
+ ajv;
14
16
  async validate(value, options) {
15
- let { schema, type, collectionType, ...additionalOptions } = this.mapOptions(options);
16
- schema = schema || (0, schema_1.getJsonSchema)(type, { ...additionalOptions, customKeys: true });
17
+ let { schema: defaultSchema, type, collectionType, ...additionalOptions } = this.mapOptions(options);
18
+ const schema = defaultSchema || (0, schema_1.getJsonSchema)(type, { ...additionalOptions, customKeys: true });
17
19
  if (schema) {
18
20
  const localValue = (0, core_1.deepClone)(value);
19
- const valid = await this.ajv.validate(schema, localValue);
20
- if (!valid && this.ajv.errors) {
21
- throw this.mapErrors(this.ajv.errors || [], {
21
+ const validate = this.ajv.compile(schema);
22
+ const valid = await validate(localValue);
23
+ const { errors } = validate;
24
+ if (!valid && errors) {
25
+ throw this.mapErrors(errors, {
22
26
  type,
23
27
  collectionType,
24
28
  async: true,
@@ -1 +1 @@
1
- {"version":3,"file":"AjvService.js","sourceRoot":"","sources":["../../../src/services/AjvService.ts"],"names":[],"mappings":";;;;AAAA,qCAAoF;AACpF,iCAAsD;AACtD,yCAA0F;AAC1F,sDAAqC;AACrC,qEAAgE;AAEhE,0EAAqE;AACrE,iBAAe;AACf,8CAAyC;AASzC,IAAa,UAAU,GAAvB,MAAa,UAAU;IAOrB,KAAK,CAAC,QAAQ,CAAC,KAAU,EAAE,OAAwC;QACjE,IAAI,EAAC,MAAM,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,iBAAiB,EAAC,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAEpF,MAAM,GAAG,MAAM,IAAI,IAAA,sBAAa,EAAC,IAAI,EAAE,EAAC,GAAG,iBAAiB,EAAE,UAAU,EAAE,IAAI,EAAC,CAAC,CAAC;QAEjF,IAAI,MAAM,EAAE;YACV,MAAM,UAAU,GAAG,IAAA,gBAAS,EAAC,KAAK,CAAC,CAAC;YACpC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAa,EAAE,UAAU,CAAC,CAAC;YACjE,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,GAAG,CAA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I,EAAE;gBACR,KAAK,CAAC,SAAS,GAAG,IAAA,aAAM,EAAC,IAAI,CAAC,CAAC;gBAC/B,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;aACjD;YAED,OAAO,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,OAAO,IAAI,uCAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACjD,CAAC;IAES,aAAa,CAAC,KAAqB,EAAE,UAAqB;QAClE,MAAM,WAAW,GAAG,IAAA,eAAQ,EAAC,KAAK,EAAE,wBAAwB,CAAC,CAAC;QAE9D,IAAI,WAAW,EAAE;YACf,MAAM,KAAK,GAAG,wBAAe,CAAC,IAAI,CAAkB,IAAA,kBAAW,EAAC,UAAU,CAAC,EAAE,WAAW,CAAC,CAAC;YAE1F,IAAI,KAAK,EAAE;gBACT,IAAA,eAAQ,EAAC,KAAK,EAAE,wBAAwB,EAAE,KAAK,CAAC,IAAI,IAAI,WAAW,CAAC,CAAC;gBAErE,OAAO,KAAK,CAAC,OAAQ,CAAC,OAAO,CAAC,IAAI,WAAW,GAAG,EAAE,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,GAAG,CAAC,CAAC;aACrF;SACF;QAED,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;CACF,CAAA;AAtFC;IADC,IAAA,aAAQ,EAAC,oBAAoB,EAAE,6CAAqB,CAAC;;kDACb;AAGzC;IADC,IAAA,WAAM,GAAE;sCACM,aAAG;uCAAC;AALR,UAAU;IADtB,IAAA,eAAU,GAAE;GACA,UAAU,CAwFtB;AAxFY,gCAAU","sourcesContent":["import {deepClone, getValue, nameOf, prototypeOf, setValue, Type} from \"@tsed/core\";\nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {getJsonSchema, JsonEntityStore, JsonSchema, JsonSchemaObject} from \"@tsed/schema\";\nimport Ajv, {ErrorObject} from \"ajv\";\nimport {AjvValidationError} from \"../errors/AjvValidationError\";\nimport {AjvErrorObject, ErrorFormatter} from \"../interfaces/IAjvSettings\";\nimport {defaultErrorFormatter} from \"../utils/defaultErrorFormatter\";\nimport \"./Ajv\";\nimport {getPath} from \"../utils/getPath\";\n\nexport interface AjvValidateOptions extends Record<string, any> {\n schema?: JsonSchema | Partial<JsonSchemaObject>;\n type?: Type<any> | any;\n collectionType?: Type<any> | any;\n}\n\n@Injectable()\nexport class AjvService {\n @Constant(\"ajv.errorFormatter\", defaultErrorFormatter)\n protected errorFormatter: ErrorFormatter;\n\n @Inject()\n protected ajv: Ajv;\n\n async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {\n let {schema, type, collectionType, ...additionalOptions} = this.mapOptions(options);\n\n schema = schema || getJsonSchema(type, {...additionalOptions, customKeys: true});\n\n if (schema) {\n const localValue = deepClone(value);\n const valid = await this.ajv.validate(schema as any, localValue);\n if (!valid && this.ajv.errors) {\n throw this.mapErrors(this.ajv.errors || [], {\n type,\n collectionType,\n async: true,\n value: localValue\n });\n }\n }\n\n return value;\n }\n\n protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions {\n if (options instanceof JsonSchema) {\n return {\n schema: options.toJSON({customKeys: true})\n };\n }\n\n return options;\n }\n\n protected mapErrors(errors: ErrorObject[], options: any) {\n const {type, collectionType, value} = options;\n\n const message = errors\n .map((error: AjvErrorObject) => {\n if (collectionType) {\n error.collectionName = nameOf(collectionType);\n }\n\n const dataPath = getPath(error);\n\n if (!error.data) {\n if (dataPath) {\n error.data = getValue(value, dataPath.replace(/^\\./, \"\"));\n } else if (error.schemaPath !== \"#/required\") {\n error.data = value;\n }\n }\n\n if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {\n error.data = \"[REDACTED]\";\n }\n\n if (type) {\n error.modelName = nameOf(type);\n error.message = this.mapClassError(error, type);\n }\n\n return this.errorFormatter.call(this, error, {});\n })\n .join(\"\\n\");\n\n return new AjvValidationError(message, errors);\n }\n\n protected mapClassError(error: AjvErrorObject, targetType: Type<any>) {\n const propertyKey = getValue(error, \"params.missingProperty\");\n\n if (propertyKey) {\n const store = JsonEntityStore.from<JsonEntityStore>(prototypeOf(targetType), propertyKey);\n\n if (store) {\n setValue(error, \"params.missingProperty\", store.name || propertyKey);\n\n return error.message!.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);\n }\n }\n\n return error.message;\n }\n}\n"]}
1
+ {"version":3,"file":"AjvService.js","sourceRoot":"","sources":["../../../src/services/AjvService.ts"],"names":[],"mappings":";;;;AAAA,qCAAoF;AACpF,iCAAsD;AACtD,yCAA0F;AAC1F,sDAAqC;AACrC,qEAAgE;AAEhE,0EAAqE;AACrE,iBAAe;AACf,8CAAyC;AASzC,IAAa,UAAU,GAAvB,MAAa,UAAU;IAEX,cAAc,CAAiB;IAG/B,GAAG,CAAM;IAEnB,KAAK,CAAC,QAAQ,CAAC,KAAU,EAAE,OAAwC;QACjE,IAAI,EAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,cAAc,EAAE,GAAG,iBAAiB,EAAC,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAEnG,MAAM,MAAM,GAAG,aAAa,IAAI,IAAA,sBAAa,EAAC,IAAI,EAAE,EAAC,GAAG,iBAAiB,EAAE,UAAU,EAAE,IAAI,EAAC,CAAC,CAAC;QAE9F,IAAI,MAAM,EAAE;YACV,MAAM,UAAU,GAAG,IAAA,gBAAS,EAAC,KAAK,CAAC,CAAC;YACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAE1C,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,CAAC;YACzC,MAAM,EAAC,MAAM,EAAC,GAAG,QAAQ,CAAC;YAE1B,IAAI,CAA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,SAAS,GAAG,IAAA,aAAM,EAAC,IAAI,CAAC,CAAC;gBAC/B,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;aACjD;YAED,OAAO,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,OAAO,IAAI,uCAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACjD,CAAC;IAES,aAAa,CAAC,KAAqB,EAAE,UAAqB;QAClE,MAAM,WAAW,GAAG,IAAA,eAAQ,EAAC,KAAK,EAAE,wBAAwB,CAAC,CAAC;QAE9D,IAAI,WAAW,EAAE;YACf,MAAM,KAAK,GAAG,wBAAe,CAAC,IAAI,CAAkB,IAAA,kBAAW,EAAC,UAAU,CAAC,EAAE,WAAW,CAAC,CAAC;YAE1F,IAAI,KAAK,EAAE;gBACT,IAAA,eAAQ,EAAC,KAAK,EAAE,wBAAwB,EAAE,KAAK,CAAC,IAAI,IAAI,WAAW,CAAC,CAAC;gBAErE,OAAO,KAAK,CAAC,OAAQ,CAAC,OAAO,CAAC,IAAI,WAAW,GAAG,EAAE,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,GAAG,CAAC,CAAC;aACrF;SACF;QAED,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;CACF,CAAA;AA1FC;IADC,IAAA,aAAQ,EAAC,oBAAoB,EAAE,6CAAqB,CAAC;;kDACb;AAGzC;IADC,IAAA,WAAM,GAAE;sCACM,aAAG;uCAAC;AALR,UAAU;IADtB,IAAA,eAAU,GAAE;GACA,UAAU,CA4FtB;AA5FY,gCAAU","sourcesContent":["import {deepClone, getValue, nameOf, prototypeOf, setValue, Type} from \"@tsed/core\";\nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {getJsonSchema, JsonEntityStore, JsonSchema, JsonSchemaObject} from \"@tsed/schema\";\nimport Ajv, {ErrorObject} from \"ajv\";\nimport {AjvValidationError} from \"../errors/AjvValidationError\";\nimport {AjvErrorObject, ErrorFormatter} from \"../interfaces/IAjvSettings\";\nimport {defaultErrorFormatter} from \"../utils/defaultErrorFormatter\";\nimport \"./Ajv\";\nimport {getPath} from \"../utils/getPath\";\n\nexport interface AjvValidateOptions extends Record<string, any> {\n schema?: JsonSchema | Partial<JsonSchemaObject>;\n type?: Type<any> | any;\n collectionType?: Type<any> | any;\n}\n\n@Injectable()\nexport class AjvService {\n @Constant(\"ajv.errorFormatter\", defaultErrorFormatter)\n protected errorFormatter: ErrorFormatter;\n\n @Inject()\n protected ajv: Ajv;\n\n async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {\n let {schema: defaultSchema, type, collectionType, ...additionalOptions} = this.mapOptions(options);\n\n const schema = defaultSchema || getJsonSchema(type, {...additionalOptions, customKeys: true});\n\n if (schema) {\n const localValue = deepClone(value);\n const validate = this.ajv.compile(schema);\n\n const valid = await validate(localValue);\n const {errors} = validate;\n\n if (!valid && errors) {\n throw this.mapErrors(errors, {\n type,\n collectionType,\n async: true,\n value: localValue\n });\n }\n }\n\n return value;\n }\n\n protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions {\n if (options instanceof JsonSchema) {\n return {\n schema: options.toJSON({customKeys: true})\n };\n }\n\n return options;\n }\n\n protected mapErrors(errors: ErrorObject[], options: any) {\n const {type, collectionType, value} = options;\n\n const message = errors\n .map((error: AjvErrorObject) => {\n if (collectionType) {\n error.collectionName = nameOf(collectionType);\n }\n\n const dataPath = getPath(error);\n\n if (!error.data) {\n if (dataPath) {\n error.data = getValue(value, dataPath.replace(/^\\./, \"\"));\n } else if (error.schemaPath !== \"#/required\") {\n error.data = value;\n }\n }\n\n if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {\n error.data = \"[REDACTED]\";\n }\n\n if (type) {\n error.modelName = nameOf(type);\n error.message = this.mapClassError(error, type);\n }\n\n return this.errorFormatter.call(this, error, {});\n })\n .join(\"\\n\");\n\n return new AjvValidationError(message, errors);\n }\n\n protected mapClassError(error: AjvErrorObject, targetType: Type<any>) {\n const propertyKey = getValue(error, \"params.missingProperty\");\n\n if (propertyKey) {\n const store = JsonEntityStore.from<JsonEntityStore>(prototypeOf(targetType), propertyKey);\n\n if (store) {\n setValue(error, \"params.missingProperty\", store.name || propertyKey);\n\n return error.message!.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);\n }\n }\n\n return error.message;\n }\n}\n"]}
@@ -1,8 +1,9 @@
1
1
  import { BadRequest } from "@tsed/exceptions";
2
2
  export class AjvValidationError extends BadRequest {
3
+ name = "AJV_VALIDATION_ERROR";
4
+ errors;
3
5
  constructor(message, errors = []) {
4
6
  super(message);
5
- this.name = "AJV_VALIDATION_ERROR";
6
7
  this.errors = errors;
7
8
  }
8
9
  }
@@ -1 +1 @@
1
- {"version":3,"file":"AjvValidationError.js","sourceRoot":"","sources":["../../../src/errors/AjvValidationError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAE5C,MAAM,OAAO,kBAAmB,SAAQ,UAAU;IAIhD,YAAY,OAAe,EAAE,SAAgB,EAAE;QAC7C,KAAK,CAAC,OAAO,CAAC,CAAC;QAJV,SAAI,GAAW,sBAAsB,CAAC;QAK3C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF","sourcesContent":["import {BadRequest} from \"@tsed/exceptions\";\n\nexport class AjvValidationError extends BadRequest {\n public name: string = \"AJV_VALIDATION_ERROR\";\n public errors: any[];\n\n constructor(message: string, errors: any[] = []) {\n super(message);\n this.errors = errors;\n }\n}\n"]}
1
+ {"version":3,"file":"AjvValidationError.js","sourceRoot":"","sources":["../../../src/errors/AjvValidationError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAE5C,MAAM,OAAO,kBAAmB,SAAQ,UAAU;IACzC,IAAI,GAAW,sBAAsB,CAAC;IACtC,MAAM,CAAQ;IAErB,YAAY,OAAe,EAAE,SAAgB,EAAE;QAC7C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF","sourcesContent":["import {BadRequest} from \"@tsed/exceptions\";\n\nexport class AjvValidationError extends BadRequest {\n public name: string = \"AJV_VALIDATION_ERROR\";\n public errors: any[];\n\n constructor(message: string, errors: any[] = []) {\n super(message);\n this.errors = errors;\n }\n}\n"]}
@@ -29,7 +29,6 @@ function getFormatsProviders(injector) {
29
29
  }
30
30
  function getFormats(injector) {
31
31
  return getFormatsProviders(injector).map((provider) => {
32
- var _a;
33
32
  const { name, options } = provider.store.get("ajv:formats", {});
34
33
  const service = injector.invoke(provider.token);
35
34
  return {
@@ -37,7 +36,7 @@ function getFormats(injector) {
37
36
  options: {
38
37
  ...options,
39
38
  validate: service.validate.bind(service),
40
- compare: (_a = service.compare) === null || _a === void 0 ? void 0 : _a.bind(service)
39
+ compare: service.compare?.bind(service)
41
40
  }
42
41
  };
43
42
  });
@@ -1 +1 @@
1
- {"version":3,"file":"Ajv.js","sourceRoot":"","sources":["../../../src/services/Ajv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,YAAY,CAAC;AACvC,OAAO,EAAC,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,gBAAgB,EAAC,MAAM,UAAU,CAAC;AACzF,OAAO,GAA4C,MAAM,KAAK,CAAC;AAC/D,OAAO,UAAU,MAAM,aAAa,CAAC;AAIrC,SAAS,UAAU,CAAC,GAAW,EAAE,OAAY;IAC3C,IAAI,OAAO,CAAC,GAAG,CAAC,EAAE;QAChB,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;KACnC;AACH,CAAC;AAED,SAAS,mBAAmB,CAAC,QAAyB;I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C;gBACxC,OAAO,EAAE,MAAA,OAAO,CAAC,OAAO,0CAAE,IAAI,CAAC,OAAO,CAAC;aACxC;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,gBAAgB,CAAC;IACf,OAAO,EAAE,GAAG;IACZ,IAAI,EAAE,CAAC,aAAa,EAAE,eAAe,CAAC;IACtC,KAAK,EAAE,aAAa,CAAC,SAAS;IAC9B,UAAU,CAAC,aAA4B,EAAE,QAAyB;QAChE,MAAM,EAAC,cAAc,EAAE,QAAQ,GAAG,EAAE,EAAE,GAAG,KAAK,EAAC,GAAG,aAAa,CAAC,GAAG,CAAe,KAAK,CAAC,IAAI,EAAE,CAAC;QAC/F,MAAM,OAAO,GAAG;YACd,OAAO,EAAE,KAAK;YACd,WAAW,EAAE,IAAI;YACjB,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,CAAC,GAAG,QAAQ,EAAE,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;YAClD,GAAG,KAAK;SACT,CAAC;QAEF,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;QAE7B,UAAU,CAAC,GAAU,CAAC,CAAC;QAEvB,UAAU,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,EAAC,IAAI,EAAE,OAAO,EAAC,EAAE,EAAE;YAC/C,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;QAEH,OAAO,GAAG,CAAC;IACb,CAAC;CACF,CAAC,CAAC","sourcesContent":["import {cleanObject} from \"@tsed/core\";\nimport {Configuration, InjectorService, ProviderScope, registerProvider} from \"@tsed/di\";\nimport Ajv, {Format, KeywordDefinition, Vocabulary} from \"ajv\";\nimport AjvFormats from \"ajv-formats\";\nimport {FormatsMethods} from \"../interfaces/FormatsMethods\";\nimport {IAjvSettings} from \"../interfaces/IAjvSettings\";\n\nfunction getHandler(key: string, service: any) {\n if (service[key]) {\n return service[key].bind(service);\n }\n}\n\nfunction getKeywordProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:keyword\");\n}\n\nfunction bindKeywords(injector: InjectorService): Vocabulary {\n return getKeywordProviders(injector).map((provider) => {\n const options = provider.store.get<Omit<KeywordDefinition, \"compile\">>(\"ajv:keyword\", {})!;\n const service = injector.invoke(provider.token);\n\n return <KeywordDefinition>cleanObject({\n coerceTypes: \"array\",\n ...options,\n validate: getHandler(\"validate\", service),\n compile: getHandler(\"compile\", service),\n code: getHandler(\"code\", service),\n macro: getHandler(\"macro\", service)\n });\n });\n}\n\nfunction getFormatsProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:formats\");\n}\n\nfunction getFormats(injector: InjectorService): {name: string; options: Format}[] {\n return getFormatsProviders(injector).map((provider) => {\n const {name, options} = provider.store.get<any>(\"ajv:formats\", {})!;\n const service = injector.invoke<FormatsMethods<any>>(provider.token);\n\n return {\n name,\n options: {\n ...options,\n validate: service.validate.bind(service),\n compare: service.compare?.bind(service)\n }\n };\n });\n}\n\nregisterProvider({\n provide: Ajv,\n deps: [Configuration, InjectorService],\n scope: ProviderScope.SINGLETON,\n useFactory(configuration: Configuration, injector: InjectorService) {\n const {errorFormatter, keywords = [], ...props} = configuration.get<IAjvSettings>(\"ajv\") || {};\n const options = {\n verbose: false,\n coerceTypes: true,\n strict: false,\n keywords: [...keywords, ...bindKeywords(injector)],\n ...props\n };\n\n const ajv = new Ajv(options);\n\n AjvFormats(ajv as any);\n\n getFormats(injector).forEach(({name, options}) => {\n ajv.addFormat(name, options);\n });\n\n return ajv;\n }\n});\n"]}
1
+ {"version":3,"file":"Ajv.js","sourceRoot":"","sources":["../../../src/services/Ajv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,YAAY,CAAC;AACvC,OAAO,EAAC,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,gBAAgB,EAAC,MAAM,UAAU,CAAC;AACzF,OAAO,GAA4C,MAAM,KA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ject} from \"@tsed/core\";\nimport {Configuration, InjectorService, ProviderScope, registerProvider} from \"@tsed/di\";\nimport Ajv, {Format, KeywordDefinition, Vocabulary} from \"ajv\";\nimport AjvFormats from \"ajv-formats\";\nimport {FormatsMethods} from \"../interfaces/FormatsMethods\";\nimport {IAjvSettings} from \"../interfaces/IAjvSettings\";\n\nfunction getHandler(key: string, service: any) {\n if (service[key]) {\n return service[key].bind(service);\n }\n}\n\nfunction getKeywordProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:keyword\");\n}\n\nfunction bindKeywords(injector: InjectorService): Vocabulary {\n return getKeywordProviders(injector).map((provider) => {\n const options = provider.store.get<Omit<KeywordDefinition, \"compile\">>(\"ajv:keyword\", {})!;\n const service = injector.invoke(provider.token);\n\n return <KeywordDefinition>cleanObject({\n coerceTypes: \"array\",\n ...options,\n validate: getHandler(\"validate\", service),\n compile: getHandler(\"compile\", service),\n code: getHandler(\"code\", service),\n macro: getHandler(\"macro\", service)\n });\n });\n}\n\nfunction getFormatsProviders(injector: InjectorService) {\n return injector.getProviders(\"ajv:formats\");\n}\n\nfunction getFormats(injector: InjectorService): {name: string; options: Format}[] {\n return getFormatsProviders(injector).map((provider) => {\n const {name, options} = provider.store.get<any>(\"ajv:formats\", {})!;\n const service = injector.invoke<FormatsMethods<any>>(provider.token);\n\n return {\n name,\n options: {\n ...options,\n validate: service.validate.bind(service),\n compare: service.compare?.bind(service)\n }\n };\n });\n}\n\nregisterProvider({\n provide: Ajv,\n deps: [Configuration, InjectorService],\n scope: ProviderScope.SINGLETON,\n useFactory(configuration: Configuration, injector: InjectorService) {\n const {errorFormatter, keywords = [], ...props} = configuration.get<IAjvSettings>(\"ajv\") || {};\n const options = {\n verbose: false,\n coerceTypes: true,\n strict: false,\n keywords: [...keywords, ...bindKeywords(injector)],\n ...props\n };\n\n const ajv = new Ajv(options);\n\n AjvFormats(ajv as any);\n\n getFormats(injector).forEach(({name, options}) => {\n ajv.addFormat(name, options);\n });\n\n return ajv;\n }\n});\n"]}
@@ -8,14 +8,18 @@ import { defaultErrorFormatter } from "../utils/defaultErrorFormatter.js";
8
8
  import "./Ajv.js";
9
9
  import { getPath } from "../utils/getPath.js";
10
10
  let AjvService = class AjvService {
11
+ errorFormatter;
12
+ ajv;
11
13
  async validate(value, options) {
12
- let { schema, type, collectionType, ...additionalOptions } = this.mapOptions(options);
13
- schema = schema || getJsonSchema(type, { ...additionalOptions, customKeys: true });
14
+ let { schema: defaultSchema, type, collectionType, ...additionalOptions } = this.mapOptions(options);
15
+ const schema = defaultSchema || getJsonSchema(type, { ...additionalOptions, customKeys: true });
14
16
  if (schema) {
15
17
  const localValue = deepClone(value);
16
- const valid = await this.ajv.validate(schema, localValue);
17
- if (!valid && this.ajv.errors) {
18
- throw this.mapErrors(this.ajv.errors || [], {
18
+ const validate = this.ajv.compile(schema);
19
+ const valid = await validate(localValue);
20
+ const { errors } = validate;
21
+ if (!valid && errors) {
22
+ throw this.mapErrors(errors, {
19
23
  type,
20
24
  collectionType,
21
25
  async: true,
@@ -1 +1 @@
1
- {"version":3,"file":"AjvService.js","sourceRoot":"","sources":["../../../src/services/AjvService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAO,MAAM,YAAY,CAAC;AACpF,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AACtD,OAAO,EAAC,aAAa,EAAE,eAAe,EAAE,UAAU,EAAmB,MAAM,cAAc,CAAC;AAC1F,OAAO,GAAkB,MAAM,KAAK,CAAC;AACrC,OAAO,EAAC,kBAAkB,EAAC,MAAM,8BAA8B,CAAC;AAEhE,OAAO,EAAC,qBAAqB,EAAC,MAAM,gCAAgC,CAAC;AACrE,OAAO,OAAO,CAAC;AACf,OAAO,EAAC,OAAO,EAAC,MAAM,kBAAkB,CAAC;AASzC,IAAa,UAAU,GAAvB,MAAa,UAAU;IAOrB,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AAC,OAAQ,CAAC,OAAO,CAAC,IAAI,WAAW,GAAG,EAAE,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,GAAG,CAAC,CAAC;aACrF;SACF;QAED,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;CACF,CAAA;AAtFC;IADC,QAAQ,CAAC,oBAAoB,EAAE,qBAAqB,CAAC;;kDACb;AAGzC;IADC,MAAM,EAAE;8BACM,GAAG;uCAAC;AALR,UAAU;IADtB,UAAU,EAAE;GACA,UAAU,CAwFtB;SAxFY,UAAU","sourcesContent":["import {deepClone, getValue, nameOf, prototypeOf, setValue, Type} from \"@tsed/core\";\nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {getJsonSchema, JsonEntityStore, JsonSchema, JsonSchemaObject} from \"@tsed/schema\";\nimport Ajv, {ErrorObject} from \"ajv\";\nimport {AjvValidationError} from \"../errors/AjvValidationError\";\nimport {AjvErrorObject, ErrorFormatter} from \"../interfaces/IAjvSettings\";\nimport {defaultErrorFormatter} from \"../utils/defaultErrorFormatter\";\nimport \"./Ajv\";\nimport {getPath} from \"../utils/getPath\";\n\nexport interface AjvValidateOptions extends Record<string, any> {\n schema?: JsonSchema | Partial<JsonSchemaObject>;\n type?: Type<any> | any;\n collectionType?: Type<any> | any;\n}\n\n@Injectable()\nexport class AjvService {\n @Constant(\"ajv.errorFormatter\", defaultErrorFormatter)\n protected errorFormatter: ErrorFormatter;\n\n @Inject()\n protected ajv: Ajv;\n\n async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {\n let {schema, type, collectionType, ...additionalOptions} = this.mapOptions(options);\n\n schema = schema || getJsonSchema(type, {...additionalOptions, customKeys: true});\n\n if (schema) {\n const localValue = deepClone(value);\n const valid = await this.ajv.validate(schema as any, localValue);\n if (!valid && this.ajv.errors) {\n throw this.mapErrors(this.ajv.errors || [], {\n type,\n collectionType,\n async: true,\n value: localValue\n });\n }\n }\n\n return value;\n }\n\n protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions {\n if (options instanceof JsonSchema) {\n return {\n schema: options.toJSON({customKeys: true})\n };\n }\n\n return options;\n }\n\n protected mapErrors(errors: ErrorObject[], options: any) {\n const {type, collectionType, value} = options;\n\n const message = errors\n .map((error: AjvErrorObject) => {\n if (collectionType) {\n error.collectionName = nameOf(collectionType);\n }\n\n const dataPath = getPath(error);\n\n if (!error.data) {\n if (dataPath) {\n error.data = getValue(value, dataPath.replace(/^\\./, \"\"));\n } else if (error.schemaPath !== \"#/required\") {\n error.data = value;\n }\n }\n\n if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {\n error.data = \"[REDACTED]\";\n }\n\n if (type) {\n error.modelName = nameOf(type);\n error.message = this.mapClassError(error, type);\n }\n\n return this.errorFormatter.call(this, error, {});\n })\n .join(\"\\n\");\n\n return new AjvValidationError(message, errors);\n }\n\n protected mapClassError(error: AjvErrorObject, targetType: Type<any>) {\n const propertyKey = getValue(error, \"params.missingProperty\");\n\n if (propertyKey) {\n const store = JsonEntityStore.from<JsonEntityStore>(prototypeOf(targetType), propertyKey);\n\n if (store) {\n setValue(error, \"params.missingProperty\", store.name || propertyKey);\n\n return error.message!.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);\n }\n }\n\n return error.message;\n }\n}\n"]}
1
+ {"version":3,"file":"AjvService.js","sourceRoot":"","sources":["../../../src/services/AjvService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAO,MAAM,YAAY,CAAC;AACpF,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC;AACtD,OAAO,EAAC,aAAa,EAAE,eAAe,EAAE,UAAU,EAAmB,MAAM,cAAc,CAAC;AAC1F,OAAO,GAAkB,MAAM,KAAK,CAAC;AACrC,OAAO,EAAC,kBAAkB,EAAC,MAAM,8BAA8B,CAAC;AAEhE,OAAO,EAAC,qBA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AW,EAAE;YACf,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAkB,WAAW,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,CAAC;YAE1F,IAAI,KAAK,EAAE;gBACT,QAAQ,CAAC,KAAK,EAAE,wBAAwB,EAAE,KAAK,CAAC,IAAI,IAAI,WAAW,CAAC,CAAC;gBAErE,OAAO,KAAK,CAAC,OAAQ,CAAC,OAAO,CAAC,IAAI,WAAW,GAAG,EAAE,IAAI,KAAK,CAAC,IAAI,IAAI,WAAW,GAAG,CAAC,CAAC;aACrF;SACF;QAED,OAAO,KAAK,CAAC,OAAO,CAAC;IACvB,CAAC;CACF,CAAA;AA1FC;IADC,QAAQ,CAAC,oBAAoB,EAAE,qBAAqB,CAAC;;kDACb;AAGzC;IADC,MAAM,EAAE;8BACM,GAAG;uCAAC;AALR,UAAU;IADtB,UAAU,EAAE;GACA,UAAU,CA4FtB;SA5FY,UAAU","sourcesContent":["import {deepClone, getValue, nameOf, prototypeOf, setValue, Type} from \"@tsed/core\";\nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {getJsonSchema, JsonEntityStore, JsonSchema, JsonSchemaObject} from \"@tsed/schema\";\nimport Ajv, {ErrorObject} from \"ajv\";\nimport {AjvValidationError} from \"../errors/AjvValidationError\";\nimport {AjvErrorObject, ErrorFormatter} from \"../interfaces/IAjvSettings\";\nimport {defaultErrorFormatter} from \"../utils/defaultErrorFormatter\";\nimport \"./Ajv\";\nimport {getPath} from \"../utils/getPath\";\n\nexport interface AjvValidateOptions extends Record<string, any> {\n schema?: JsonSchema | Partial<JsonSchemaObject>;\n type?: Type<any> | any;\n collectionType?: Type<any> | any;\n}\n\n@Injectable()\nexport class AjvService {\n @Constant(\"ajv.errorFormatter\", defaultErrorFormatter)\n protected errorFormatter: ErrorFormatter;\n\n @Inject()\n protected ajv: Ajv;\n\n async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {\n let {schema: defaultSchema, type, collectionType, ...additionalOptions} = this.mapOptions(options);\n\n const schema = defaultSchema || getJsonSchema(type, {...additionalOptions, customKeys: true});\n\n if (schema) {\n const localValue = deepClone(value);\n const validate = this.ajv.compile(schema);\n\n const valid = await validate(localValue);\n const {errors} = validate;\n\n if (!valid && errors) {\n throw this.mapErrors(errors, {\n type,\n collectionType,\n async: true,\n value: localValue\n });\n }\n }\n\n return value;\n }\n\n protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions {\n if (options instanceof JsonSchema) {\n return {\n schema: options.toJSON({customKeys: true})\n };\n }\n\n return options;\n }\n\n protected mapErrors(errors: ErrorObject[], options: any) {\n const {type, collectionType, value} = options;\n\n const message = errors\n .map((error: AjvErrorObject) => {\n if (collectionType) {\n error.collectionName = nameOf(collectionType);\n }\n\n const dataPath = getPath(error);\n\n if (!error.data) {\n if (dataPath) {\n error.data = getValue(value, dataPath.replace(/^\\./, \"\"));\n } else if (error.schemaPath !== \"#/required\") {\n error.data = value;\n }\n }\n\n if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {\n error.data = \"[REDACTED]\";\n }\n\n if (type) {\n error.modelName = nameOf(type);\n error.message = this.mapClassError(error, type);\n }\n\n return this.errorFormatter.call(this, error, {});\n })\n .join(\"\\n\");\n\n return new AjvValidationError(message, errors);\n }\n\n protected mapClassError(error: AjvErrorObject, targetType: Type<any>) {\n const propertyKey = getValue(error, \"params.missingProperty\");\n\n if (propertyKey) {\n const store = JsonEntityStore.from<JsonEntityStore>(prototypeOf(targetType), propertyKey);\n\n if (store) {\n setValue(error, \"params.missingProperty\", store.name || propertyKey);\n\n return error.message!.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);\n }\n }\n\n return error.message;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/ajv",
3
- "version": "7.0.0-beta.9",
3
+ "version": "7.0.0-rc.3",
4
4
  "description": "AJV package for Ts.ED framework",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/cjs/index.js",
@@ -16,24 +16,25 @@
16
16
  "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
17
17
  "build:cjs": "tsc --build tsconfig.compile.json",
18
18
  "build:esm": "tsc --build tsconfig.compile.esm.json",
19
- "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\""
19
+ "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
20
+ "test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
20
21
  },
21
22
  "dependencies": {
22
23
  "ajv-formats": "2.1.1",
23
- "tslib": "2.3.1"
24
+ "tslib": "2.4.0"
24
25
  },
25
26
  "devDependencies": {
26
- "@tsed/core": "7.0.0-beta.9",
27
- "@tsed/di": "7.0.0-beta.9",
28
- "@tsed/exceptions": "7.0.0-beta.9",
29
- "@tsed/schema": "7.0.0-beta.9",
27
+ "@tsed/core": "7.0.0-rc.3",
28
+ "@tsed/di": "7.0.0-rc.3",
29
+ "@tsed/exceptions": "7.0.0-rc.3",
30
+ "@tsed/schema": "7.0.0-rc.3",
30
31
  "ajv": "8.11.0"
31
32
  },
32
33
  "peerDependencies": {
33
- "@tsed/core": "^7.0.0-beta.9",
34
- "@tsed/di": "^7.0.0-beta.9",
35
- "@tsed/exceptions": "^7.0.0-beta.9",
36
- "@tsed/schema": "^7.0.0-beta.9",
34
+ "@tsed/core": "^7.0.0-rc.3",
35
+ "@tsed/di": "^7.0.0-rc.3",
36
+ "@tsed/exceptions": "^7.0.0-rc.3",
37
+ "@tsed/schema": "^7.0.0-rc.3",
37
38
  "ajv": ">=8.9.0"
38
39
  },
39
40
  "peerDependenciesMeta": {
@@ -54,7 +55,10 @@
54
55
  "bugs": {
55
56
  "url": "https://github.com/tsedio/tsed/issues"
56
57
  },
57
- "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/specs/ajv",
58
+ "homepage": "https://github.com/tsedio/tsed/tree/rc/packages/specs/ajv",
58
59
  "author": "Romain Lenzotti",
59
- "license": "MIT"
60
+ "license": "MIT",
61
+ "publishConfig": {
62
+ "tag": "rc"
63
+ }
60
64
  }
package/readme.md CHANGED
@@ -11,7 +11,8 @@
11
11
  [![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon)
12
12
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
13
13
  [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
14
- [![backers](https://opencollective.com/tsed/tiers/badge.svg)](https://opencollective.com/tsed)
14
+ [![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
15
+ [![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed)
15
16
 
16
17
  </div>
17
18
 
@@ -116,7 +117,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
116
117
 
117
118
  The MIT License (MIT)
118
119
 
119
- Copyright (c) 2016 - 2018 Romain Lenzotti
120
+ Copyright (c) 2016 - 2022 Romain Lenzotti
120
121
 
121
122
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
122
123