@tsed/ajv 7.13.2 → 7.13.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.
@@ -89,7 +89,9 @@ tslib_1.__decorate([
89
89
  tslib_1.__metadata("design:type", ajv_1.default)
90
90
  ], AjvService.prototype, "ajv", void 0);
91
91
  AjvService = tslib_1.__decorate([
92
- (0, di_1.Injectable)()
92
+ (0, di_1.Injectable)({
93
+ type: "validator:service"
94
+ })
93
95
  ], AjvService);
94
96
  exports.AjvService = AjvService;
95
97
  //# sourceMappingURL=AjvService.js.map
@@ -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;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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I,IAAI,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;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
+ {"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;AAWzC,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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";\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({\n type: \"validator:service\"\n})\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"]}
@@ -86,7 +86,9 @@ __decorate([
86
86
  __metadata("design:type", Ajv)
87
87
  ], AjvService.prototype, "ajv", void 0);
88
88
  AjvService = __decorate([
89
- Injectable()
89
+ Injectable({
90
+ type: "validator:service"
91
+ })
90
92
  ], AjvService);
91
93
  export { AjvService };
92
94
  //# sourceMappingURL=AjvService.js.map
@@ -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,U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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"]}
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,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AAC;IAES,aAAa,CAAC,KAAqB,EAAE,UAAqB;QAClE,MAAM,WAAW,GAAG,QAAQ,CAAC,KAAK,EAAE,wBAAwB,CAAC,CAAC;QAE9D,IAAI,WAAW,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;IAHtB,UAAU,CAAC;QACV,IAAI,EAAE,mBAAmB;KAC1B,CAAC;GACW,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({\n type: \"validator:service\"\n})\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.13.2",
3
+ "version": "7.13.3",
4
4
  "description": "AJV package for Ts.ED framework",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/cjs/index.js",
@@ -25,17 +25,17 @@
25
25
  "tslib": "2.4.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@tsed/core": "7.13.2",
29
- "@tsed/di": "7.13.2",
30
- "@tsed/exceptions": "7.13.2",
31
- "@tsed/schema": "7.13.2",
28
+ "@tsed/core": "7.13.3",
29
+ "@tsed/di": "7.13.3",
30
+ "@tsed/exceptions": "7.13.3",
31
+ "@tsed/schema": "7.13.3",
32
32
  "ajv": "8.11.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@tsed/core": "^7.13.2",
36
- "@tsed/di": "^7.13.2",
37
- "@tsed/exceptions": "^7.13.2",
38
- "@tsed/schema": "^7.13.2",
35
+ "@tsed/core": "^7.13.3",
36
+ "@tsed/di": "^7.13.3",
37
+ "@tsed/exceptions": "^7.13.3",
38
+ "@tsed/schema": "^7.13.3",
39
39
  "ajv": ">=8.9.0",
40
40
  "ajv-errors": ">=3.0.0"
41
41
  },