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

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 (69) 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 +18 -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/interfaces/interfaces.js +3 -0
  16. package/lib/cjs/interfaces/interfaces.js.map +1 -0
  17. package/lib/cjs/package.json +3 -0
  18. package/lib/cjs/services/Ajv.js +69 -0
  19. package/lib/cjs/services/Ajv.js.map +1 -0
  20. package/lib/cjs/services/AjvService.js +93 -0
  21. package/lib/cjs/services/AjvService.js.map +1 -0
  22. package/lib/cjs/utils/defaultErrorFormatter.js +24 -0
  23. package/lib/cjs/utils/defaultErrorFormatter.js.map +1 -0
  24. package/lib/cjs/utils/getPath.js +9 -0
  25. package/lib/cjs/utils/getPath.js.map +1 -0
  26. package/lib/esm/decorators/formats.js +18 -0
  27. package/lib/esm/decorators/formats.js.map +1 -0
  28. package/lib/esm/decorators/keyword.js +17 -0
  29. package/lib/esm/decorators/keyword.js.map +1 -0
  30. package/lib/esm/errors/AjvValidationError.js +9 -0
  31. package/lib/esm/errors/AjvValidationError.js.map +1 -0
  32. package/lib/esm/index.js +15 -0
  33. package/lib/esm/index.js.map +1 -0
  34. package/lib/esm/interfaces/FormatsMethods.js +2 -0
  35. package/lib/esm/interfaces/FormatsMethods.js.map +1 -0
  36. package/lib/esm/interfaces/IAjvSettings.js +2 -0
  37. package/lib/esm/interfaces/IAjvSettings.js.map +1 -0
  38. package/lib/esm/interfaces/KeywordMethods.js +2 -0
  39. package/lib/esm/interfaces/KeywordMethods.js.map +1 -0
  40. package/lib/esm/interfaces/interfaces.js +2 -0
  41. package/lib/esm/interfaces/interfaces.js.map +1 -0
  42. package/lib/esm/package.json +3 -0
  43. package/lib/esm/services/Ajv.js +66 -0
  44. package/lib/esm/services/Ajv.js.map +1 -0
  45. package/lib/esm/services/AjvService.js +90 -0
  46. package/lib/esm/services/AjvService.js.map +1 -0
  47. package/lib/esm/utils/defaultErrorFormatter.js +20 -0
  48. package/lib/esm/utils/defaultErrorFormatter.js.map +1 -0
  49. package/lib/esm/utils/getPath.js +5 -0
  50. package/lib/esm/utils/getPath.js.map +1 -0
  51. package/lib/{decorators → types/decorators}/formats.d.ts +0 -0
  52. package/lib/{decorators → types/decorators}/keyword.d.ts +0 -0
  53. package/lib/{errors → types/errors}/AjvValidationError.d.ts +0 -0
  54. package/lib/{index.d.ts → types/index.d.ts} +12 -13
  55. package/lib/{interfaces → types/interfaces}/FormatsMethods.d.ts +0 -0
  56. package/lib/{interfaces → types/interfaces}/IAjvSettings.d.ts +0 -0
  57. package/lib/{interfaces → types/interfaces}/KeywordMethods.d.ts +0 -0
  58. package/lib/types/interfaces/interfaces.d.ts +8 -0
  59. package/lib/{services → types/services}/Ajv.d.ts +0 -0
  60. package/lib/{services → types/services}/AjvService.d.ts +0 -0
  61. package/lib/{utils → types/utils}/defaultErrorFormatter.d.ts +0 -0
  62. package/lib/{utils → types/utils}/getPath.d.ts +0 -0
  63. package/package.json +42 -22
  64. package/readme.md +30 -40
  65. package/tsconfig.compile.esm.json +12 -0
  66. package/lib/index.js +0 -302
  67. package/lib/index.js.map +0 -1
  68. package/lib/index.modern.js +0 -295
  69. 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,18 @@
1
+ "use strict";
2
+ /**
3
+ * @file Automatically generated by barrelsby.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const tslib_1 = require("tslib");
7
+ tslib_1.__exportStar(require("./decorators/formats"), exports);
8
+ tslib_1.__exportStar(require("./decorators/keyword"), exports);
9
+ tslib_1.__exportStar(require("./errors/AjvValidationError"), exports);
10
+ tslib_1.__exportStar(require("./interfaces/FormatsMethods"), exports);
11
+ tslib_1.__exportStar(require("./interfaces/IAjvSettings"), exports);
12
+ tslib_1.__exportStar(require("./interfaces/KeywordMethods"), exports);
13
+ tslib_1.__exportStar(require("./interfaces/interfaces"), exports);
14
+ tslib_1.__exportStar(require("./services/Ajv"), exports);
15
+ tslib_1.__exportStar(require("./services/AjvService"), exports);
16
+ tslib_1.__exportStar(require("./utils/defaultErrorFormatter"), exports);
17
+ tslib_1.__exportStar(require("./utils/getPath"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,+DAAqC;AACrC,+DAAqC;AACrC,sEAA4C;AAC5C,sEAA4C;AAC5C,oEAA0C;AAC1C,sEAA4C;AAC5C,kEAAwC;AACxC,yDAA+B;AAC/B,gEAAsC;AACtC,wEAA8C;AAC9C,0DAAgC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/formats\";\nexport * from \"./decorators/keyword\";\nexport * from \"./errors/AjvValidationError\";\nexport * from \"./interfaces/FormatsMethods\";\nexport * from \"./interfaces/IAjvSettings\";\nexport * from \"./interfaces/KeywordMethods\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./services/Ajv\";\nexport * from \"./services/AjvService\";\nexport * from \"./utils/defaultErrorFormatter\";\nexport * from \"./utils/getPath\";\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,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import {IAjvSettings} from \"./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
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,69 @@
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 = tslib_1.__importDefault(require("ajv"));
7
+ const ajv_formats_1 = 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
+ coerceTypes: "array",
22
+ ...options,
23
+ validate: getHandler("validate", service),
24
+ compile: getHandler("compile", service),
25
+ code: getHandler("code", service),
26
+ macro: getHandler("macro", service)
27
+ });
28
+ });
29
+ }
30
+ function getFormatsProviders(injector) {
31
+ return injector.getProviders("ajv:formats");
32
+ }
33
+ function getFormats(injector) {
34
+ return getFormatsProviders(injector).map((provider) => {
35
+ var _a;
36
+ const { name, options } = provider.store.get("ajv:formats", {});
37
+ const service = injector.invoke(provider.token);
38
+ return {
39
+ name,
40
+ options: {
41
+ ...options,
42
+ validate: service.validate.bind(service),
43
+ compare: (_a = service.compare) === null || _a === void 0 ? void 0 : _a.bind(service)
44
+ }
45
+ };
46
+ });
47
+ }
48
+ (0, di_1.registerProvider)({
49
+ provide: ajv_1.default,
50
+ deps: [di_1.Configuration, di_1.InjectorService],
51
+ scope: di_1.ProviderScope.SINGLETON,
52
+ useFactory(configuration, injector) {
53
+ const { errorFormatter, keywords = [], ...props } = configuration.get("ajv") || {};
54
+ const options = {
55
+ verbose: false,
56
+ coerceTypes: true,
57
+ strict: false,
58
+ keywords: [...keywords, ...bindKeywords(injector)],
59
+ ...props
60
+ };
61
+ const ajv = new ajv_1.default(options);
62
+ (0, ajv_formats_1.default)(ajv);
63
+ getFormats(injector).forEach(({ name, options }) => {
64
+ ajv.addFormat(name, options);
65
+ });
66
+ return ajv;
67
+ }
68
+ });
69
+ //# 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,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,mBAAmB,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QACpD,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAqC,aAAa,EAAE,EAAE,CAAE,CAAC;QAC3F,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEhD,OAA0B,IAAA,kBAAW,EAAC;YACpC,WAAW,EAAE,OAAO;YACpB,GAAG,OAAO;YACV,QAAQ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C;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"]}
@@ -0,0 +1,93 @@
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 = 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: defaultSchema, type, collectionType, ...additionalOptions } = this.mapOptions(options);
16
+ const schema = defaultSchema || (0, schema_1.getJsonSchema)(type, { ...additionalOptions, customKeys: true });
17
+ if (schema) {
18
+ const localValue = (0, core_1.deepClone)(value);
19
+ const validate = this.ajv.compile(schema);
20
+ const valid = await validate(localValue);
21
+ const { errors } = validate;
22
+ if (!valid && errors) {
23
+ throw this.mapErrors(errors, {
24
+ type,
25
+ collectionType,
26
+ async: true,
27
+ value: localValue
28
+ });
29
+ }
30
+ }
31
+ return value;
32
+ }
33
+ mapOptions(options) {
34
+ if (options instanceof schema_1.JsonSchema) {
35
+ return {
36
+ schema: options.toJSON({ customKeys: true })
37
+ };
38
+ }
39
+ return options;
40
+ }
41
+ mapErrors(errors, options) {
42
+ const { type, collectionType, value } = options;
43
+ const message = errors
44
+ .map((error) => {
45
+ if (collectionType) {
46
+ error.collectionName = (0, core_1.nameOf)(collectionType);
47
+ }
48
+ const dataPath = (0, getPath_1.getPath)(error);
49
+ if (!error.data) {
50
+ if (dataPath) {
51
+ error.data = (0, core_1.getValue)(value, dataPath.replace(/^\./, ""));
52
+ }
53
+ else if (error.schemaPath !== "#/required") {
54
+ error.data = value;
55
+ }
56
+ }
57
+ if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {
58
+ error.data = "[REDACTED]";
59
+ }
60
+ if (type) {
61
+ error.modelName = (0, core_1.nameOf)(type);
62
+ error.message = this.mapClassError(error, type);
63
+ }
64
+ return this.errorFormatter.call(this, error, {});
65
+ })
66
+ .join("\n");
67
+ return new AjvValidationError_1.AjvValidationError(message, errors);
68
+ }
69
+ mapClassError(error, targetType) {
70
+ const propertyKey = (0, core_1.getValue)(error, "params.missingProperty");
71
+ if (propertyKey) {
72
+ const store = schema_1.JsonEntityStore.from((0, core_1.prototypeOf)(targetType), propertyKey);
73
+ if (store) {
74
+ (0, core_1.setValue)(error, "params.missingProperty", store.name || propertyKey);
75
+ return error.message.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);
76
+ }
77
+ }
78
+ return error.message;
79
+ }
80
+ };
81
+ tslib_1.__decorate([
82
+ (0, di_1.Constant)("ajv.errorFormatter", defaultErrorFormatter_1.defaultErrorFormatter),
83
+ tslib_1.__metadata("design:type", Function)
84
+ ], AjvService.prototype, "errorFormatter", void 0);
85
+ tslib_1.__decorate([
86
+ (0, di_1.Inject)(),
87
+ tslib_1.__metadata("design:type", ajv_1.default)
88
+ ], AjvService.prototype, "ajv", void 0);
89
+ AjvService = tslib_1.__decorate([
90
+ (0, di_1.Injectable)()
91
+ ], AjvService);
92
+ exports.AjvService = AjvService;
93
+ //# 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,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,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,CAAC,KAAK,IAAI,MAAM,EAAE;gBACpB,MAAM,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;oBAC3B,IAAI;oBACJ,cAAc;oBACd,KAAK,EAAE,IAAI;oBACX,KAAK,EAAE,UAAU;iBAClB,CAAC,CAAC;aACJ;SACF;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAES,UAAU,CAAC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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"]}
@@ -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":";;;AACA,uCAAkC;AAElC,SAAgB,qBAAqB,CAAC,KAA0C;IAC9E,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAClF,MAAM,IAAI,GAAG,CAAC,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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,15 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./decorators/formats.js";
5
+ export * from "./decorators/keyword.js";
6
+ export * from "./errors/AjvValidationError.js";
7
+ export * from "./interfaces/FormatsMethods.js";
8
+ export * from "./interfaces/IAjvSettings.js";
9
+ export * from "./interfaces/KeywordMethods.js";
10
+ export * from "./interfaces/interfaces.js";
11
+ export * from "./services/Ajv.js";
12
+ export * from "./services/AjvService.js";
13
+ export * from "./utils/defaultErrorFormatter.js";
14
+ export * from "./utils/getPath.js";
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iBAAiB,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./decorators/formats\";\nexport * from \"./decorators/keyword\";\nexport * from \"./errors/AjvValidationError\";\nexport * from \"./interfaces/FormatsMethods\";\nexport * from \"./interfaces/IAjvSettings\";\nexport * from \"./interfaces/KeywordMethods\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./services/Ajv\";\nexport * from \"./services/AjvService\";\nexport * from \"./utils/defaultErrorFormatter\";\nexport * from \"./utils/getPath\";\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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"","sourcesContent":["import {IAjvSettings} from \"./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
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,66 @@
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
+ coerceTypes: "array",
19
+ ...options,
20
+ validate: getHandler("validate", service),
21
+ compile: getHandler("compile", service),
22
+ code: getHandler("code", service),
23
+ macro: getHandler("macro", service)
24
+ });
25
+ });
26
+ }
27
+ function getFormatsProviders(injector) {
28
+ return injector.getProviders("ajv:formats");
29
+ }
30
+ function getFormats(injector) {
31
+ return getFormatsProviders(injector).map((provider) => {
32
+ var _a;
33
+ const { name, options } = provider.store.get("ajv:formats", {});
34
+ const service = injector.invoke(provider.token);
35
+ return {
36
+ name,
37
+ options: {
38
+ ...options,
39
+ validate: service.validate.bind(service),
40
+ compare: (_a = service.compare) === null || _a === void 0 ? void 0 : _a.bind(service)
41
+ }
42
+ };
43
+ });
44
+ }
45
+ registerProvider({
46
+ provide: Ajv,
47
+ deps: [Configuration, InjectorService],
48
+ scope: ProviderScope.SINGLETON,
49
+ useFactory(configuration, injector) {
50
+ const { errorFormatter, keywords = [], ...props } = configuration.get("ajv") || {};
51
+ const options = {
52
+ verbose: false,
53
+ coerceTypes: true,
54
+ strict: false,
55
+ keywords: [...keywords, ...bindKeywords(injector)],
56
+ ...props
57
+ };
58
+ const ajv = new Ajv(options);
59
+ AjvFormats(ajv);
60
+ getFormats(injector).forEach(({ name, options }) => {
61
+ ajv.addFormat(name, options);
62
+ });
63
+ return ajv;
64
+ }
65
+ });
66
+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B,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"]}