@tsed/ajv 7.0.0-alpha.2 → 7.0.0-beta.1

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.
Files changed (61) hide show
  1. package/lib/cjs/decorators/formats.js +22 -0
  2. package/lib/cjs/decorators/formats.js.map +1 -0
  3. package/lib/cjs/decorators/keyword.js +21 -0
  4. package/lib/cjs/decorators/keyword.js.map +1 -0
  5. package/lib/cjs/errors/AjvValidationError.js +13 -0
  6. package/lib/cjs/errors/AjvValidationError.js.map +1 -0
  7. package/lib/cjs/index.js +11 -0
  8. package/lib/cjs/index.js.map +1 -0
  9. package/lib/cjs/interfaces/FormatsMethods.js +3 -0
  10. package/lib/cjs/interfaces/FormatsMethods.js.map +1 -0
  11. package/lib/cjs/interfaces/IAjvSettings.js +3 -0
  12. package/lib/cjs/interfaces/IAjvSettings.js.map +1 -0
  13. package/lib/cjs/interfaces/KeywordMethods.js +3 -0
  14. package/lib/cjs/interfaces/KeywordMethods.js.map +1 -0
  15. package/lib/cjs/services/Ajv.js +68 -0
  16. package/lib/cjs/services/Ajv.js.map +1 -0
  17. package/lib/cjs/services/AjvService.js +91 -0
  18. package/lib/cjs/services/AjvService.js.map +1 -0
  19. package/lib/cjs/utils/defaultErrorFormatter.js +24 -0
  20. package/lib/cjs/utils/defaultErrorFormatter.js.map +1 -0
  21. package/lib/cjs/utils/getPath.js +9 -0
  22. package/lib/cjs/utils/getPath.js.map +1 -0
  23. package/lib/esm/decorators/formats.js +18 -0
  24. package/lib/esm/decorators/formats.js.map +1 -0
  25. package/lib/esm/decorators/keyword.js +17 -0
  26. package/lib/esm/decorators/keyword.js.map +1 -0
  27. package/lib/esm/errors/AjvValidationError.js +9 -0
  28. package/lib/esm/errors/AjvValidationError.js.map +1 -0
  29. package/lib/esm/index.js +8 -0
  30. package/lib/esm/index.js.map +1 -0
  31. package/lib/esm/interfaces/FormatsMethods.js +2 -0
  32. package/lib/esm/interfaces/FormatsMethods.js.map +1 -0
  33. package/lib/esm/interfaces/IAjvSettings.js +2 -0
  34. package/lib/esm/interfaces/IAjvSettings.js.map +1 -0
  35. package/lib/esm/interfaces/KeywordMethods.js +2 -0
  36. package/lib/esm/interfaces/KeywordMethods.js.map +1 -0
  37. package/lib/esm/services/Ajv.js +65 -0
  38. package/lib/esm/services/Ajv.js.map +1 -0
  39. package/lib/esm/services/AjvService.js +88 -0
  40. package/lib/esm/services/AjvService.js.map +1 -0
  41. package/lib/esm/utils/defaultErrorFormatter.js +20 -0
  42. package/lib/esm/utils/defaultErrorFormatter.js.map +1 -0
  43. package/lib/esm/utils/getPath.js +5 -0
  44. package/lib/esm/utils/getPath.js.map +1 -0
  45. package/lib/{decorators → types/decorators}/formats.d.ts +0 -0
  46. package/lib/{decorators → types/decorators}/keyword.d.ts +0 -0
  47. package/lib/{errors → types/errors}/AjvValidationError.d.ts +0 -0
  48. package/lib/{index.d.ts → types/index.d.ts} +0 -0
  49. package/lib/{interfaces → types/interfaces}/FormatsMethods.d.ts +0 -0
  50. package/lib/{interfaces → types/interfaces}/IAjvSettings.d.ts +0 -0
  51. package/lib/{interfaces → types/interfaces}/KeywordMethods.d.ts +0 -0
  52. package/lib/{services → types/services}/Ajv.d.ts +0 -0
  53. package/lib/{services → types/services}/AjvService.d.ts +0 -0
  54. package/lib/{utils → types/utils}/defaultErrorFormatter.d.ts +0 -0
  55. package/lib/{utils → types/utils}/getPath.d.ts +0 -0
  56. package/package.json +21 -17
  57. package/tsconfig.compile.esm.json +23 -0
  58. package/lib/index.js +0 -302
  59. package/lib/index.js.map +0 -1
  60. package/lib/index.modern.js +0 -295
  61. package/lib/index.modern.js.map +0 -1
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Formats = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ const di_1 = require("@tsed/di");
6
+ /**
7
+ * Create a new custom formats validator
8
+ * @param name
9
+ * @param options
10
+ * @decorator
11
+ * @ajv
12
+ */
13
+ function Formats(name, options = {}) {
14
+ return (0, core_1.useDecorators)((0, di_1.Injectable)({
15
+ type: "ajv:formats"
16
+ }), (0, core_1.StoreSet)("ajv:formats", {
17
+ name,
18
+ options
19
+ }));
20
+ }
21
+ exports.Formats = Formats;
22
+ //# sourceMappingURL=formats.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formats.js","sourceRoot":"","sources":["../../../src/decorators/formats.ts"],"names":[],"mappings":";;;AAAA,qCAAmD;AACnD,iCAAoC;AAKpC;;;;;;GAMG;AACH,SAAgB,OAAO,CAAC,IAAY,EAAE,UAA0B,EAAE;IAChE,OAAO,IAAA,oBAAa,EAClB,IAAA,eAAU,EAAC;QACT,IAAI,EAAE,aAAa;KACpB,CAAC,EACF,IAAA,eAAQ,EAAC,aAAa,EAAE;QACtB,IAAI;QACJ,OAAO;KACR,CAAC,CACH,CAAC;AACJ,CAAC;AAVD,0BAUC","sourcesContent":["import {StoreSet, useDecorators} from \"@tsed/core\";\nimport {Injectable} from \"@tsed/di\";\nimport {AsyncFormatDefinition, FormatDefinition} from \"ajv\";\n\nexport type FormatsOptions = Omit<FormatDefinition<any>, \"validate\" | \"compare\"> | Omit<AsyncFormatDefinition<any>, \"validate\" | \"compare\">;\n\n/**\n * Create a new custom formats validator\n * @param name\n * @param options\n * @decorator\n * @ajv\n */\nexport function Formats(name: string, options: FormatsOptions = {}): ClassDecorator {\n return useDecorators(\n Injectable({\n type: \"ajv:formats\"\n }),\n StoreSet(\"ajv:formats\", {\n name,\n options\n })\n );\n}\n"]}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keyword = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ const di_1 = require("@tsed/di");
6
+ /**
7
+ * Create new keyword custom validator
8
+ * @param options
9
+ * @decorator
10
+ * @ajv
11
+ */
12
+ function Keyword(options) {
13
+ return (0, core_1.useDecorators)((0, di_1.Injectable)({
14
+ type: "ajv:keyword"
15
+ }), (0, core_1.StoreSet)("ajv:keyword", {
16
+ ...options,
17
+ metaSchema: options.metaSchema && options.metaSchema.toJSON ? options.metaSchema.toJSON() : options.metaSchema
18
+ }));
19
+ }
20
+ exports.Keyword = Keyword;
21
+ //# sourceMappingURL=keyword.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyword.js","sourceRoot":"","sources":["../../../src/decorators/keyword.ts"],"names":[],"mappings":";;;AAAA,qCAAmD;AACnD,iCAAoC;AAQpC;;;;;GAKG;AACH,SAAgB,OAAO,CAAC,OAAuB;IAC7C,OAAO,IAAA,oBAAa,EAClB,IAAA,eAAU,EAAC;QACT,IAAI,EAAE,aAAa;KACpB,CAAC,EACF,IAAA,eAAQ,EAAC,aAAa,EAAE;QACtB,GAAG,OAAO;QACV,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU;KAC/G,CAAC,CACH,CAAC;AACJ,CAAC;AAVD,0BAUC","sourcesContent":["import {StoreSet, useDecorators} from \"@tsed/core\";\nimport {Injectable} from \"@tsed/di\";\nimport {JsonSchema} from \"@tsed/schema\";\nimport {AnySchemaObject, KeywordDefinition} from \"ajv\";\n\nexport type KeywordOptions = Partial<Omit<KeywordDefinition, \"metaSchema\">> & {\n metaSchema?: AnySchemaObject | JsonSchema;\n};\n\n/**\n * Create new keyword custom validator\n * @param options\n * @decorator\n * @ajv\n */\nexport function Keyword(options: KeywordOptions): ClassDecorator {\n return useDecorators(\n Injectable({\n type: \"ajv:keyword\"\n }),\n StoreSet(\"ajv:keyword\", {\n ...options,\n metaSchema: options.metaSchema && options.metaSchema.toJSON ? options.metaSchema.toJSON() : options.metaSchema\n })\n );\n}\n"]}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AjvValidationError = void 0;
4
+ const exceptions_1 = require("@tsed/exceptions");
5
+ class AjvValidationError extends exceptions_1.BadRequest {
6
+ constructor(message, errors = []) {
7
+ super(message);
8
+ this.name = "AJV_VALIDATION_ERROR";
9
+ this.errors = errors;
10
+ }
11
+ }
12
+ exports.AjvValidationError = AjvValidationError;
13
+ //# sourceMappingURL=AjvValidationError.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./errors/AjvValidationError"), exports);
5
+ (0, tslib_1.__exportStar)(require("./services/AjvService"), exports);
6
+ (0, tslib_1.__exportStar)(require("./services/Ajv"), exports);
7
+ (0, tslib_1.__exportStar)(require("./decorators/keyword"), exports);
8
+ (0, tslib_1.__exportStar)(require("./interfaces/KeywordMethods"), exports);
9
+ (0, tslib_1.__exportStar)(require("./decorators/formats"), exports);
10
+ (0, tslib_1.__exportStar)(require("./interfaces/FormatsMethods"), exports);
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,2EAA4C;AAC5C,qEAAsC;AACtC,8DAA+B;AAC/B,oEAAqC;AACrC,2EAA4C;AAC5C,oEAAqC;AACrC,2EAA4C","sourcesContent":["export * from \"./errors/AjvValidationError\";\nexport * from \"./services/AjvService\";\nexport * from \"./services/Ajv\";\nexport * from \"./decorators/keyword\";\nexport * from \"./interfaces/KeywordMethods\";\nexport * from \"./decorators/formats\";\nexport * from \"./interfaces/FormatsMethods\";\n\nimport {IAjvSettings} from \"./interfaces/IAjvSettings\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n // @ts-ignore\n ajv?: IAjvSettings;\n }\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=FormatsMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FormatsMethods.js","sourceRoot":"","sources":["../../../src/interfaces/FormatsMethods.ts"],"names":[],"mappings":"","sourcesContent":["export interface FormatsMethods<T extends string | number> {\n compare?: (data1: T, data2: T) => number | undefined;\n\n validate(data: T): boolean;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IAjvSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IAjvSettings.js","sourceRoot":"","sources":["../../../src/interfaces/IAjvSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {ErrorObject, Options} from \"ajv\";\n\n/**\n *\n */\nexport interface AjvErrorObject extends ErrorObject {\n modelName: string;\n collectionName?: string;\n}\n\n/**\n *\n */\nexport type ErrorFormatter = (error: AjvErrorObject) => string;\n\n/**\n *\n */\nexport interface IAjvSettings extends Options {\n Ajv?: any;\n errorFormatter?: ErrorFormatter;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=KeywordMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeywordMethods.js","sourceRoot":"","sources":["../../../src/interfaces/KeywordMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {AnySchemaObject, KeywordCxt, SchemaObjCxt, ValidateFunction} from \"ajv\";\n\nexport interface AjvDataValidationCtx<T extends string | number = string | number> {\n dataPath: string;\n parentData: {[K in T]: any}; // object or array\n parentDataProperty: T; // string or number\n rootData: Record<string, any> | any[];\n dynamicAnchors: {[Ref in string]?: ValidateFunction};\n}\n\nexport interface AjvDataValidateFunction {\n (...args: Parameters<ValidateFunction>): boolean | Promise<any>;\n}\n\nexport interface AjvSchemaValidateFunction {\n (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: AjvDataValidationCtx): boolean | Promise<any>;\n}\n\nexport interface KeywordMethods {\n code?: (cxt: KeywordCxt, ruleType?: string) => void;\n compile?: (schema: any, parentSchema: AnySchemaObject, it: SchemaObjCxt) => AjvDataValidateFunction;\n validate?: AjvSchemaValidateFunction | AjvDataValidateFunction;\n}\n"]}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const core_1 = require("@tsed/core");
5
+ const di_1 = require("@tsed/di");
6
+ const ajv_1 = (0, tslib_1.__importDefault)(require("ajv"));
7
+ const ajv_formats_1 = (0, tslib_1.__importDefault)(require("ajv-formats"));
8
+ function getHandler(key, service) {
9
+ if (service[key]) {
10
+ return service[key].bind(service);
11
+ }
12
+ }
13
+ function getKeywordProviders(injector) {
14
+ return injector.getProviders("ajv:keyword");
15
+ }
16
+ function bindKeywords(injector) {
17
+ return getKeywordProviders(injector).map((provider) => {
18
+ const options = provider.store.get("ajv:keyword", {});
19
+ const service = injector.invoke(provider.token);
20
+ return (0, core_1.cleanObject)({
21
+ ...options,
22
+ validate: getHandler("validate", service),
23
+ compile: getHandler("compile", service),
24
+ code: getHandler("code", service),
25
+ macro: getHandler("macro", service)
26
+ });
27
+ });
28
+ }
29
+ function getFormatsProviders(injector) {
30
+ return injector.getProviders("ajv:formats");
31
+ }
32
+ function getFormats(injector) {
33
+ return getFormatsProviders(injector).map((provider) => {
34
+ var _a;
35
+ const { name, options } = provider.store.get("ajv:formats", {});
36
+ const service = injector.invoke(provider.token);
37
+ return {
38
+ name,
39
+ options: {
40
+ ...options,
41
+ validate: service.validate.bind(service),
42
+ compare: (_a = service.compare) === null || _a === void 0 ? void 0 : _a.bind(service)
43
+ }
44
+ };
45
+ });
46
+ }
47
+ (0, di_1.registerProvider)({
48
+ provide: ajv_1.default,
49
+ deps: [di_1.Configuration, di_1.InjectorService],
50
+ scope: di_1.ProviderScope.SINGLETON,
51
+ useFactory(configuration, injector) {
52
+ const { errorFormatter, keywords = [], ...props } = configuration.get("ajv") || {};
53
+ const options = {
54
+ verbose: false,
55
+ coerceTypes: true,
56
+ strict: false,
57
+ keywords: [...keywords, ...bindKeywords(injector)],
58
+ ...props
59
+ };
60
+ const ajv = new ajv_1.default(options);
61
+ (0, ajv_formats_1.default)(ajv);
62
+ getFormats(injector).forEach(({ name, options }) => {
63
+ ajv.addFormat(name, options);
64
+ });
65
+ return ajv;
66
+ }
67
+ });
68
+ //# sourceMappingURL=Ajv.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Ajv.js","sourceRoot":"","sources":["../../../src/services/Ajv.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,iCAAyF;AACzF,2DAA+D;AAC/D,2EAAqC;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,QAA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,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 ...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"]}
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AjvService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const core_1 = require("@tsed/core");
6
+ const di_1 = require("@tsed/di");
7
+ const schema_1 = require("@tsed/schema");
8
+ const ajv_1 = (0, tslib_1.__importDefault)(require("ajv"));
9
+ const AjvValidationError_1 = require("../errors/AjvValidationError");
10
+ const defaultErrorFormatter_1 = require("../utils/defaultErrorFormatter");
11
+ require("./Ajv");
12
+ const getPath_1 = require("../utils/getPath");
13
+ let AjvService = class AjvService {
14
+ 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
+ if (schema) {
18
+ 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 || [], {
22
+ type,
23
+ collectionType,
24
+ async: true,
25
+ value: localValue
26
+ });
27
+ }
28
+ }
29
+ return value;
30
+ }
31
+ mapOptions(options) {
32
+ if (options instanceof schema_1.JsonSchema) {
33
+ return {
34
+ schema: options.toJSON({ customKeys: true })
35
+ };
36
+ }
37
+ return options;
38
+ }
39
+ mapErrors(errors, options) {
40
+ const { type, collectionType, value } = options;
41
+ const message = errors
42
+ .map((error) => {
43
+ if (collectionType) {
44
+ error.collectionName = (0, core_1.nameOf)(collectionType);
45
+ }
46
+ const dataPath = (0, getPath_1.getPath)(error);
47
+ if (!error.data) {
48
+ if (dataPath) {
49
+ error.data = (0, core_1.getValue)(value, dataPath.replace(/^\./, ""));
50
+ }
51
+ else if (error.schemaPath !== "#/required") {
52
+ error.data = value;
53
+ }
54
+ }
55
+ if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {
56
+ error.data = "[REDACTED]";
57
+ }
58
+ if (type) {
59
+ error.modelName = (0, core_1.nameOf)(type);
60
+ error.message = this.mapClassError(error, type);
61
+ }
62
+ return this.errorFormatter.call(this, error, {});
63
+ })
64
+ .join("\n");
65
+ return new AjvValidationError_1.AjvValidationError(message, errors);
66
+ }
67
+ mapClassError(error, targetType) {
68
+ const propertyKey = (0, core_1.getValue)(error, "params.missingProperty");
69
+ if (propertyKey) {
70
+ const store = schema_1.JsonEntityStore.from((0, core_1.prototypeOf)(targetType), propertyKey);
71
+ if (store) {
72
+ (0, core_1.setValue)(error, "params.missingProperty", store.name || propertyKey);
73
+ return error.message.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);
74
+ }
75
+ }
76
+ return error.message;
77
+ }
78
+ };
79
+ (0, tslib_1.__decorate)([
80
+ (0, di_1.Constant)("ajv.errorFormatter", defaultErrorFormatter_1.defaultErrorFormatter),
81
+ (0, tslib_1.__metadata)("design:type", Function)
82
+ ], AjvService.prototype, "errorFormatter", void 0);
83
+ (0, tslib_1.__decorate)([
84
+ (0, di_1.Inject)(),
85
+ (0, tslib_1.__metadata)("design:type", ajv_1.default)
86
+ ], AjvService.prototype, "ajv", void 0);
87
+ AjvService = (0, tslib_1.__decorate)([
88
+ (0, di_1.Injectable)()
89
+ ], AjvService);
90
+ exports.AjvService = AjvService;
91
+ //# sourceMappingURL=AjvService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AjvService.js","sourceRoot":"","sources":["../../../src/services/AjvService.ts"],"names":[],"mappings":";;;;AAAA,qCAAoF;AACpF,iCAAsD;AACtD,yCAA0F;AAC1F,2DAAqC;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;2CACM,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"]}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultErrorFormatter = void 0;
4
+ const getPath_1 = require("./getPath");
5
+ function defaultErrorFormatter(error) {
6
+ const value = JSON.stringify(error.data === undefined ? "undefined" : error.data);
7
+ const join = (list) => list.filter(Boolean).join("").trim();
8
+ error.dataPath = (0, getPath_1.getPath)(error);
9
+ const [, indexPath, ...paths] = error.dataPath.split(".");
10
+ const deepPaths = paths.length ? "." + paths.join(".") : "";
11
+ if (error.collectionName) {
12
+ switch (error.collectionName) {
13
+ case "Array":
14
+ return join([`${error.modelName || ""}[${indexPath}]${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
15
+ case "Map":
16
+ return join([`Map<${indexPath}, ${error.modelName || ""}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
17
+ case "Set":
18
+ return join([`Set<${indexPath}, ${error.modelName || ""}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
19
+ }
20
+ }
21
+ return join([!error.modelName && "Value", `${error.modelName || ""}`, error.dataPath, ` ${error.message}. Given value: ${value}`]);
22
+ }
23
+ exports.defaultErrorFormatter = defaultErrorFormatter;
24
+ //# sourceMappingURL=defaultErrorFormatter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaultErrorFormatter.js","sourceRoot":"","sources":["../../../src/utils/defaultErrorFormatter.ts"],"names":[],"mappings":";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E,IAAI,KAAK,CAAC,OAAO,kBAAkB,KAAK,EAAE,CAAC,CAAC,CAAC;SACxH;KACF;IAED,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,IAAI,OAAO,EAAE,GAAG,KAAK,CAAC,SAAS,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,QAAQ,EAAE,IAAI,KAAK,CAAC,OAAO,kBAAkB,KAAK,EAAE,CAAC,CAAC,CAAC;AACrI,CAAC;AArBD,sDAqBC","sourcesContent":["import {AjvErrorObject} from \"../interfaces/IAjvSettings\";\nimport {getPath} from \"./getPath\";\n\nexport function defaultErrorFormatter(error: AjvErrorObject & {dataPath: string}) {\n const value = JSON.stringify(error.data === undefined ? \"undefined\" : error.data);\n const join = (list: any[]): string => list.filter(Boolean).join(\"\").trim();\n\n error.dataPath = getPath(error);\n\n const [, indexPath, ...paths] = error.dataPath.split(\".\");\n const deepPaths = paths.length ? \".\" + paths.join(\".\") : \"\";\n\n if (error.collectionName) {\n switch (error.collectionName) {\n case \"Array\":\n return join([`${error.modelName || \"\"}[${indexPath}]${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n case \"Map\":\n return join([`Map<${indexPath}, ${error.modelName || \"\"}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n case \"Set\":\n return join([`Set<${indexPath}, ${error.modelName || \"\"}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n }\n }\n\n return join([!error.modelName && \"Value\", `${error.modelName || \"\"}`, error.dataPath, ` ${error.message}. Given value: ${value}`]);\n}\n"]}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPath = void 0;
4
+ const core_1 = require("@tsed/core");
5
+ function getPath(error) {
6
+ return (0, core_1.getValue)(error, "dataPath", (0, core_1.getValue)(error, "instancePath", "")).replace(/\//gi, ".");
7
+ }
8
+ exports.getPath = getPath;
9
+ //# sourceMappingURL=getPath.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getPath.js","sourceRoot":"","sources":["../../../src/utils/getPath.ts"],"names":[],"mappings":";;;AAAA,qCAAoC;AAEpC,SAAgB,OAAO,CAAC,KAAU;IAChC,OAAO,IAAA,eAAQ,EAAC,KAAK,EAAE,UAAU,EAAE,IAAA,eAAQ,EAAC,KAAK,EAAE,cAAc,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC/F,CAAC;AAFD,0BAEC","sourcesContent":["import {getValue} from \"@tsed/core\";\n\nexport function getPath(error: any) {\n return getValue(error, \"dataPath\", getValue(error, \"instancePath\", \"\")).replace(/\\//gi, \".\");\n}\n"]}
@@ -0,0 +1,18 @@
1
+ import { StoreSet, useDecorators } from "@tsed/core";
2
+ import { Injectable } from "@tsed/di";
3
+ /**
4
+ * Create a new custom formats validator
5
+ * @param name
6
+ * @param options
7
+ * @decorator
8
+ * @ajv
9
+ */
10
+ export function Formats(name, options = {}) {
11
+ return useDecorators(Injectable({
12
+ type: "ajv:formats"
13
+ }), StoreSet("ajv:formats", {
14
+ name,
15
+ options
16
+ }));
17
+ }
18
+ //# sourceMappingURL=formats.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formats.js","sourceRoot":"","sources":["../../../src/decorators/formats.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACnD,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAKpC;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAC,IAAY,EAAE,UAA0B,EAAE;IAChE,OAAO,aAAa,CAClB,UAAU,CAAC;QACT,IAAI,EAAE,aAAa;KACpB,CAAC,EACF,QAAQ,CAAC,aAAa,EAAE;QACtB,IAAI;QACJ,OAAO;KACR,CAAC,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {StoreSet, useDecorators} from \"@tsed/core\";\nimport {Injectable} from \"@tsed/di\";\nimport {AsyncFormatDefinition, FormatDefinition} from \"ajv\";\n\nexport type FormatsOptions = Omit<FormatDefinition<any>, \"validate\" | \"compare\"> | Omit<AsyncFormatDefinition<any>, \"validate\" | \"compare\">;\n\n/**\n * Create a new custom formats validator\n * @param name\n * @param options\n * @decorator\n * @ajv\n */\nexport function Formats(name: string, options: FormatsOptions = {}): ClassDecorator {\n return useDecorators(\n Injectable({\n type: \"ajv:formats\"\n }),\n StoreSet(\"ajv:formats\", {\n name,\n options\n })\n );\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import { StoreSet, useDecorators } from "@tsed/core";
2
+ import { Injectable } from "@tsed/di";
3
+ /**
4
+ * Create new keyword custom validator
5
+ * @param options
6
+ * @decorator
7
+ * @ajv
8
+ */
9
+ export function Keyword(options) {
10
+ return useDecorators(Injectable({
11
+ type: "ajv:keyword"
12
+ }), StoreSet("ajv:keyword", {
13
+ ...options,
14
+ metaSchema: options.metaSchema && options.metaSchema.toJSON ? options.metaSchema.toJSON() : options.metaSchema
15
+ }));
16
+ }
17
+ //# sourceMappingURL=keyword.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyword.js","sourceRoot":"","sources":["../../../src/decorators/keyword.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACnD,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAQpC;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,OAAuB;IAC7C,OAAO,aAAa,CAClB,UAAU,CAAC;QACT,IAAI,EAAE,aAAa;KACpB,CAAC,EACF,QAAQ,CAAC,aAAa,EAAE;QACtB,GAAG,OAAO;QACV,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU;KAC/G,CAAC,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {StoreSet, useDecorators} from \"@tsed/core\";\nimport {Injectable} from \"@tsed/di\";\nimport {JsonSchema} from \"@tsed/schema\";\nimport {AnySchemaObject, KeywordDefinition} from \"ajv\";\n\nexport type KeywordOptions = Partial<Omit<KeywordDefinition, \"metaSchema\">> & {\n metaSchema?: AnySchemaObject | JsonSchema;\n};\n\n/**\n * Create new keyword custom validator\n * @param options\n * @decorator\n * @ajv\n */\nexport function Keyword(options: KeywordOptions): ClassDecorator {\n return useDecorators(\n Injectable({\n type: \"ajv:keyword\"\n }),\n StoreSet(\"ajv:keyword\", {\n ...options,\n metaSchema: options.metaSchema && options.metaSchema.toJSON ? options.metaSchema.toJSON() : options.metaSchema\n })\n );\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import { BadRequest } from "@tsed/exceptions";
2
+ export class AjvValidationError extends BadRequest {
3
+ constructor(message, errors = []) {
4
+ super(message);
5
+ this.name = "AJV_VALIDATION_ERROR";
6
+ this.errors = errors;
7
+ }
8
+ }
9
+ //# sourceMappingURL=AjvValidationError.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,8 @@
1
+ export * from "./errors/AjvValidationError";
2
+ export * from "./services/AjvService";
3
+ export * from "./services/Ajv";
4
+ export * from "./decorators/keyword";
5
+ export * from "./interfaces/KeywordMethods";
6
+ export * from "./decorators/formats";
7
+ export * from "./interfaces/FormatsMethods";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC","sourcesContent":["export * from \"./errors/AjvValidationError\";\nexport * from \"./services/AjvService\";\nexport * from \"./services/Ajv\";\nexport * from \"./decorators/keyword\";\nexport * from \"./interfaces/KeywordMethods\";\nexport * from \"./decorators/formats\";\nexport * from \"./interfaces/FormatsMethods\";\n\nimport {IAjvSettings} from \"./interfaces/IAjvSettings\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n // @ts-ignore\n ajv?: IAjvSettings;\n }\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=FormatsMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FormatsMethods.js","sourceRoot":"","sources":["../../../src/interfaces/FormatsMethods.ts"],"names":[],"mappings":"","sourcesContent":["export interface FormatsMethods<T extends string | number> {\n compare?: (data1: T, data2: T) => number | undefined;\n\n validate(data: T): boolean;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=IAjvSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IAjvSettings.js","sourceRoot":"","sources":["../../../src/interfaces/IAjvSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {ErrorObject, Options} from \"ajv\";\n\n/**\n *\n */\nexport interface AjvErrorObject extends ErrorObject {\n modelName: string;\n collectionName?: string;\n}\n\n/**\n *\n */\nexport type ErrorFormatter = (error: AjvErrorObject) => string;\n\n/**\n *\n */\nexport interface IAjvSettings extends Options {\n Ajv?: any;\n errorFormatter?: ErrorFormatter;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=KeywordMethods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeywordMethods.js","sourceRoot":"","sources":["../../../src/interfaces/KeywordMethods.ts"],"names":[],"mappings":"","sourcesContent":["import {AnySchemaObject, KeywordCxt, SchemaObjCxt, ValidateFunction} from \"ajv\";\n\nexport interface AjvDataValidationCtx<T extends string | number = string | number> {\n dataPath: string;\n parentData: {[K in T]: any}; // object or array\n parentDataProperty: T; // string or number\n rootData: Record<string, any> | any[];\n dynamicAnchors: {[Ref in string]?: ValidateFunction};\n}\n\nexport interface AjvDataValidateFunction {\n (...args: Parameters<ValidateFunction>): boolean | Promise<any>;\n}\n\nexport interface AjvSchemaValidateFunction {\n (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: AjvDataValidationCtx): boolean | Promise<any>;\n}\n\nexport interface KeywordMethods {\n code?: (cxt: KeywordCxt, ruleType?: string) => void;\n compile?: (schema: any, parentSchema: AnySchemaObject, it: SchemaObjCxt) => AjvDataValidateFunction;\n validate?: AjvSchemaValidateFunction | AjvDataValidateFunction;\n}\n"]}
@@ -0,0 +1,65 @@
1
+ import { cleanObject } from "@tsed/core";
2
+ import { Configuration, InjectorService, ProviderScope, registerProvider } from "@tsed/di";
3
+ import Ajv from "ajv";
4
+ import AjvFormats from "ajv-formats";
5
+ function getHandler(key, service) {
6
+ if (service[key]) {
7
+ return service[key].bind(service);
8
+ }
9
+ }
10
+ function getKeywordProviders(injector) {
11
+ return injector.getProviders("ajv:keyword");
12
+ }
13
+ function bindKeywords(injector) {
14
+ return getKeywordProviders(injector).map((provider) => {
15
+ const options = provider.store.get("ajv:keyword", {});
16
+ const service = injector.invoke(provider.token);
17
+ return cleanObject({
18
+ ...options,
19
+ validate: getHandler("validate", service),
20
+ compile: getHandler("compile", service),
21
+ code: getHandler("code", service),
22
+ macro: getHandler("macro", service)
23
+ });
24
+ });
25
+ }
26
+ function getFormatsProviders(injector) {
27
+ return injector.getProviders("ajv:formats");
28
+ }
29
+ function getFormats(injector) {
30
+ return getFormatsProviders(injector).map((provider) => {
31
+ var _a;
32
+ const { name, options } = provider.store.get("ajv:formats", {});
33
+ const service = injector.invoke(provider.token);
34
+ return {
35
+ name,
36
+ options: {
37
+ ...options,
38
+ validate: service.validate.bind(service),
39
+ compare: (_a = service.compare) === null || _a === void 0 ? void 0 : _a.bind(service)
40
+ }
41
+ };
42
+ });
43
+ }
44
+ registerProvider({
45
+ provide: Ajv,
46
+ deps: [Configuration, InjectorService],
47
+ scope: ProviderScope.SINGLETON,
48
+ useFactory(configuration, injector) {
49
+ const { errorFormatter, keywords = [], ...props } = configuration.get("ajv") || {};
50
+ const options = {
51
+ verbose: false,
52
+ coerceTypes: true,
53
+ strict: false,
54
+ keywords: [...keywords, ...bindKeywords(injector)],
55
+ ...props
56
+ };
57
+ const ajv = new Ajv(options);
58
+ AjvFormats(ajv);
59
+ getFormats(injector).forEach(({ name, options }) => {
60
+ ajv.addFormat(name, options);
61
+ });
62
+ return ajv;
63
+ }
64
+ });
65
+ //# sourceMappingURL=Ajv.js.map
@@ -0,0 +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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AO,EAAE;gBACP,GAAG,OAAO;gBACV,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;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 ...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"]}