@tsed/ajv 8.11.1 → 8.12.0-rc.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 (64) hide show
  1. package/lib/esm/decorators/formats.js +0 -1
  2. package/lib/esm/decorators/keyword.js +0 -1
  3. package/lib/esm/errors/AjvValidationError.js +0 -1
  4. package/lib/esm/fn/validate.js +0 -1
  5. package/lib/esm/index.js +0 -1
  6. package/lib/esm/interfaces/FormatsMethods.js +0 -1
  7. package/lib/esm/interfaces/IAjvSettings.js +0 -1
  8. package/lib/esm/interfaces/KeywordMethods.js +0 -1
  9. package/lib/esm/interfaces/interfaces.js +0 -1
  10. package/lib/esm/services/Ajv.js +0 -1
  11. package/lib/esm/services/AjvService.js +0 -1
  12. package/lib/esm/utils/defaultErrorFormatter.js +0 -1
  13. package/lib/esm/utils/getPath.js +0 -1
  14. package/package.json +12 -12
  15. package/lib/browser/decorators/formats.js +0 -17
  16. package/lib/browser/decorators/formats.js.map +0 -1
  17. package/lib/browser/decorators/keyword.js +0 -17
  18. package/lib/browser/decorators/keyword.js.map +0 -1
  19. package/lib/browser/errors/AjvValidationError.js +0 -10
  20. package/lib/browser/errors/AjvValidationError.js.map +0 -1
  21. package/lib/browser/fn/validate.js +0 -9
  22. package/lib/browser/fn/validate.js.map +0 -1
  23. package/lib/browser/index.js +0 -19
  24. package/lib/browser/index.js.map +0 -1
  25. package/lib/browser/services/Ajv.js +0 -57
  26. package/lib/browser/services/Ajv.js.map +0 -1
  27. package/lib/browser/services/AjvService.js +0 -65
  28. package/lib/browser/services/AjvService.js.map +0 -1
  29. package/lib/browser/utils/defaultErrorFormatter.js +0 -26
  30. package/lib/browser/utils/defaultErrorFormatter.js.map +0 -1
  31. package/lib/browser/utils/getPath.js +0 -12
  32. package/lib/browser/utils/getPath.js.map +0 -1
  33. package/lib/esm/decorators/formats.js.map +0 -1
  34. package/lib/esm/decorators/keyword.js.map +0 -1
  35. package/lib/esm/errors/AjvValidationError.js.map +0 -1
  36. package/lib/esm/fn/validate.js.map +0 -1
  37. package/lib/esm/index.js.map +0 -1
  38. package/lib/esm/interfaces/FormatsMethods.js.map +0 -1
  39. package/lib/esm/interfaces/IAjvSettings.js.map +0 -1
  40. package/lib/esm/interfaces/KeywordMethods.js.map +0 -1
  41. package/lib/esm/interfaces/interfaces.js.map +0 -1
  42. package/lib/esm/services/Ajv.js.map +0 -1
  43. package/lib/esm/services/AjvService.js.map +0 -1
  44. package/lib/esm/utils/defaultErrorFormatter.js.map +0 -1
  45. package/lib/esm/utils/getPath.js.map +0 -1
  46. package/src/decorators/formats.ts +0 -24
  47. package/src/decorators/keyword.ts +0 -26
  48. package/src/errors/AjvValidationError.ts +0 -11
  49. package/src/fn/validate.spec.ts +0 -20
  50. package/src/fn/validate.ts +0 -13
  51. package/src/index.ts +0 -15
  52. package/src/interfaces/FormatsMethods.ts +0 -5
  53. package/src/interfaces/IAjvSettings.ts +0 -23
  54. package/src/interfaces/KeywordMethods.ts +0 -23
  55. package/src/interfaces/interfaces.ts +0 -10
  56. package/src/services/Ajv.spec.ts +0 -39
  57. package/src/services/Ajv.ts +0 -84
  58. package/src/services/AjvService.spec.ts +0 -47
  59. package/src/services/AjvService.ts +0 -123
  60. package/src/utils/defaultErrorFormatter.spec.ts +0 -73
  61. package/src/utils/defaultErrorFormatter.ts +0 -33
  62. package/src/utils/getPath.ts +0 -13
  63. package/vite.config.mts +0 -45
  64. package/vitest.config.mts +0 -21
@@ -15,4 +15,3 @@ export function Formats(name, options = {}) {
15
15
  options
16
16
  }));
17
17
  }
18
- //# sourceMappingURL=formats.js.map
@@ -14,4 +14,3 @@ export function Keyword(options) {
14
14
  metaSchema: options.metaSchema && options.metaSchema.toJSON ? options.metaSchema.toJSON() : options.metaSchema
15
15
  }));
16
16
  }
17
- //# sourceMappingURL=keyword.js.map
@@ -6,4 +6,3 @@ export class AjvValidationError extends BadRequest {
6
6
  this.errors = errors;
7
7
  }
8
8
  }
9
- //# sourceMappingURL=AjvValidationError.js.map
@@ -8,4 +8,3 @@ import { AjvService } from "../services/AjvService.js";
8
8
  export function validate(value, options) {
9
9
  return inject(AjvService).validate(value, options);
10
10
  }
11
- //# sourceMappingURL=validate.js.map
package/lib/esm/index.js CHANGED
@@ -13,4 +13,3 @@ export * from "./services/Ajv.js";
13
13
  export * from "./services/AjvService.js";
14
14
  export * from "./utils/defaultErrorFormatter.js";
15
15
  export * from "./utils/getPath.js";
16
- //# sourceMappingURL=index.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=FormatsMethods.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=IAjvSettings.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=KeywordMethods.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=interfaces.js.map
@@ -67,4 +67,3 @@ injectable(Ajv)
67
67
  });
68
68
  return ajv;
69
69
  });
70
- //# sourceMappingURL=Ajv.js.map
@@ -84,4 +84,3 @@ export class AjvService {
84
84
  }
85
85
  }
86
86
  injectable(AjvService).type("validator:service");
87
- //# sourceMappingURL=AjvService.js.map
@@ -24,4 +24,3 @@ export function defaultErrorFormatter(error) {
24
24
  ` ${error.message}. Given value: ${value}`
25
25
  ]);
26
26
  }
27
- //# sourceMappingURL=defaultErrorFormatter.js.map
@@ -8,4 +8,3 @@ export function getPath(error) {
8
8
  export function getInstancePath(error) {
9
9
  return getValue(error, "dataPath", getValue(error, "instancePath", "")).replace(/\//gi, ".");
10
10
  }
11
- //# sourceMappingURL=getPath.js.map
package/package.json CHANGED
@@ -2,25 +2,22 @@
2
2
  "name": "@tsed/ajv",
3
3
  "description": "AJV package for Ts.ED framework",
4
4
  "type": "module",
5
- "version": "8.11.1",
5
+ "version": "8.12.0-rc.1",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
9
9
  "typings": "./lib/types/index.d.ts",
10
- "browser": "./lib/browser/index.js",
11
10
  "exports": {
12
11
  ".": {
13
12
  "tsed-source": "./src/index.ts",
14
13
  "types": "./lib/types/index.d.ts",
15
- "browser": "./lib/browser/index.js",
16
14
  "import": "./lib/esm/index.js",
17
15
  "default": "./lib/esm/index.js"
18
16
  }
19
17
  },
20
18
  "scripts": {
21
19
  "clean": "rm -rf lib",
22
- "build": "yarn clean && yarn barrels && yarn build:ts && yarn run build:browser",
23
- "build:browser": "vite build",
20
+ "build": "yarn clean && yarn barrels && yarn build:ts",
24
21
  "barrels": "barrels",
25
22
  "test": "vitest run",
26
23
  "build:ts": "tsc --build tsconfig.esm.json",
@@ -32,12 +29,12 @@
32
29
  "tslib": "2.7.0"
33
30
  },
34
31
  "devDependencies": {
35
- "@tsed/barrels": "8.11.1",
36
- "@tsed/core": "8.11.1",
37
- "@tsed/di": "8.11.1",
38
- "@tsed/exceptions": "8.11.1",
39
- "@tsed/schema": "8.11.1",
40
- "@tsed/typescript": "8.11.1",
32
+ "@tsed/barrels": "8.12.0-rc.1",
33
+ "@tsed/core": "8.12.0-rc.1",
34
+ "@tsed/di": "8.12.0-rc.1",
35
+ "@tsed/exceptions": "8.12.0-rc.1",
36
+ "@tsed/schema": "8.12.0-rc.1",
37
+ "@tsed/typescript": "8.12.0-rc.1",
41
38
  "ajv": "^8.17.1",
42
39
  "eslint": "9.12.0",
43
40
  "typescript": "5.4.5",
@@ -72,5 +69,8 @@
72
69
  },
73
70
  "homepage": "https://github.com/tsedio/tsed/tree/production/packages/specs/ajv",
74
71
  "author": "Romain Lenzotti",
75
- "license": "MIT"
72
+ "license": "MIT",
73
+ "publishConfig": {
74
+ "tag": "rc"
75
+ }
76
76
  }
@@ -1,17 +0,0 @@
1
- import { useDecorators as o, StoreSet as e } from "@tsed/core";
2
- import { Injectable as a } from "@tsed/di";
3
- function s(r, t = {}) {
4
- return o(
5
- a({
6
- type: "ajv:formats"
7
- }),
8
- e("ajv:formats", {
9
- name: r,
10
- options: t
11
- })
12
- );
13
- }
14
- export {
15
- s as Formats
16
- };
17
- //# sourceMappingURL=formats.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"formats.js","sources":["../../../src/decorators/formats.ts"],"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"],"names":["Formats","name","options","useDecorators","Injectable","StoreSet"],"mappings":";;AAaO,SAASA,EAAQC,GAAcC,IAA0B,IAAoB;AAC3E,SAAAC;AAAA,IACLC,EAAW;AAAA,MACT,MAAM;AAAA,IAAA,CACP;AAAA,IACDC,EAAS,eAAe;AAAA,MACtB,MAAAJ;AAAA,MACA,SAAAC;AAAA,IACD,CAAA;AAAA,EACH;AACF;"}
@@ -1,17 +0,0 @@
1
- import { useDecorators as a, StoreSet as m } from "@tsed/core";
2
- import { Injectable as r } from "@tsed/di";
3
- function c(e) {
4
- return a(
5
- r({
6
- type: "ajv:keyword"
7
- }),
8
- m("ajv:keyword", {
9
- ...e,
10
- metaSchema: e.metaSchema && e.metaSchema.toJSON ? e.metaSchema.toJSON() : e.metaSchema
11
- })
12
- );
13
- }
14
- export {
15
- c as Keyword
16
- };
17
- //# sourceMappingURL=keyword.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"keyword.js","sources":["../../../src/decorators/keyword.ts"],"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"],"names":["Keyword","options","useDecorators","Injectable","StoreSet"],"mappings":";;AAeO,SAASA,EAAQC,GAAyC;AACxD,SAAAC;AAAA,IACLC,EAAW;AAAA,MACT,MAAM;AAAA,IAAA,CACP;AAAA,IACDC,EAAS,eAAe;AAAA,MACtB,GAAGH;AAAA,MACH,YAAYA,EAAQ,cAAcA,EAAQ,WAAW,SAASA,EAAQ,WAAW,OAAO,IAAIA,EAAQ;AAAA,IACrG,CAAA;AAAA,EACH;AACF;"}
@@ -1,10 +0,0 @@
1
- import { BadRequest as t } from "@tsed/exceptions";
2
- class a extends t {
3
- constructor(r, s = []) {
4
- super(r), this.name = "AJV_VALIDATION_ERROR", this.errors = s;
5
- }
6
- }
7
- export {
8
- a as AjvValidationError
9
- };
10
- //# sourceMappingURL=AjvValidationError.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AjvValidationError.js","sources":["../../../src/errors/AjvValidationError.ts"],"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"],"names":["AjvValidationError","BadRequest","message","errors"],"mappings":";AAEO,MAAMA,UAA2BC,EAAW;AAAA,EAIjD,YAAYC,GAAiBC,IAAgB,IAAI;AAC/C,UAAMD,CAAO,GAJf,KAAO,OAAe,wBAKpB,KAAK,SAASC;AAAA,EAAA;AAElB;"}
@@ -1,9 +0,0 @@
1
- import { inject as e } from "@tsed/di";
2
- import { AjvService as i } from "../services/AjvService.js";
3
- function m(r, t) {
4
- return e(i).validate(r, t);
5
- }
6
- export {
7
- m as validate
8
- };
9
- //# sourceMappingURL=validate.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate.js","sources":["../../../src/fn/validate.ts"],"sourcesContent":["import {inject} from \"@tsed/di\";\nimport {JsonSchema} from \"@tsed/schema\";\n\nimport {AjvService, type AjvValidateOptions} from \"../services/AjvService.js\";\n\n/**\n * Validate a value against a JSON schema or a type.\n * @param value\n * @param options\n */\nexport function validate(value: unknown, options: AjvValidateOptions | JsonSchema) {\n return inject(AjvService).validate(value, options);\n}\n"],"names":["validate","value","options","inject","AjvService"],"mappings":";;AAUgB,SAAAA,EAASC,GAAgBC,GAA0C;AACjF,SAAOC,EAAOC,CAAU,EAAE,SAASH,GAAOC,CAAO;AACnD;"}
@@ -1,19 +0,0 @@
1
- import { Formats as e } from "./decorators/formats.js";
2
- import { Keyword as m } from "./decorators/keyword.js";
3
- import { AjvValidationError as p } from "./errors/AjvValidationError.js";
4
- import { validate as i } from "./fn/validate.js";
5
- import "./services/Ajv.js";
6
- import { AjvService as v } from "./services/AjvService.js";
7
- import { defaultErrorFormatter as n } from "./utils/defaultErrorFormatter.js";
8
- import { getInstancePath as g, getPath as h } from "./utils/getPath.js";
9
- export {
10
- v as AjvService,
11
- p as AjvValidationError,
12
- e as Formats,
13
- m as Keyword,
14
- n as defaultErrorFormatter,
15
- g as getInstancePath,
16
- h as getPath,
17
- i as validate
18
- };
19
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
@@ -1,57 +0,0 @@
1
- import { cleanObject as p } from "@tsed/core";
2
- import { injectable as f, ProviderScope as v, constant as u, inject as c, injector as i } from "@tsed/di";
3
- import { Ajv as s } from "/home/runner/work/tsed/tsed/node_modules/ajv/dist/ajv.js";
4
- import j from "/home/runner/work/tsed/tsed/node_modules/ajv-errors/dist/index.js";
5
- import l from "/home/runner/work/tsed/tsed/node_modules/ajv-formats/dist/index.js";
6
- function a(r, o) {
7
- if (o[r])
8
- return o[r].bind(o);
9
- }
10
- function y() {
11
- return i().getProviders("ajv:keyword");
12
- }
13
- function g() {
14
- return y().map((r) => {
15
- const o = r.store.get("ajv:keyword", {}), t = c(r.token);
16
- return p({
17
- coerceTypes: "array",
18
- ...o,
19
- validate: a("validate", t),
20
- compile: a("compile", t),
21
- code: a("code", t),
22
- macro: a("macro", t)
23
- });
24
- });
25
- }
26
- function b() {
27
- return i().getProviders("ajv:formats");
28
- }
29
- function w() {
30
- return b().map((r) => {
31
- var e;
32
- const { name: o, options: t } = r.store.get("ajv:formats", {}), n = c(r.token);
33
- return {
34
- name: o,
35
- options: {
36
- ...t,
37
- validate: n.validate.bind(n),
38
- compare: (e = n.compare) == null ? void 0 : e.bind(n)
39
- }
40
- };
41
- });
42
- }
43
- f(s).scope(v.SINGLETON).factory(() => {
44
- const { errorFormatter: r, keywords: o = [], ...t } = u("ajv") || {}, n = {
45
- verbose: !1,
46
- coerceTypes: !0,
47
- strict: !1,
48
- keywords: [...o, ...g()],
49
- discriminator: !0,
50
- allErrors: !0,
51
- ...t
52
- }, e = new s(n);
53
- return j(e), l(e), w().forEach(({ name: m, options: d }) => {
54
- e.addFormat(m, d);
55
- }), e;
56
- });
57
- //# sourceMappingURL=Ajv.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Ajv.js","sources":["../../../src/services/Ajv.ts"],"sourcesContent":["import {cleanObject} from \"@tsed/core\";\nimport {constant, inject, injectable, injector, ProviderScope} from \"@tsed/di\";\nimport {Ajv, Format, KeywordDefinition, Options, Vocabulary} from \"ajv\";\nimport AjvErrors from \"ajv-errors\";\nimport AjvFormats from \"ajv-formats\";\n\nimport {FormatsMethods} from \"../interfaces/FormatsMethods.js\";\nimport {IAjvSettings} from \"../interfaces/IAjvSettings.js\";\n\nfunction getHandler(key: string, service: any) {\n if (service[key]) {\n return service[key].bind(service);\n }\n}\n\nfunction getKeywordProviders() {\n return injector().getProviders(\"ajv:keyword\");\n}\n\nfunction bindKeywords(): Vocabulary {\n return getKeywordProviders().map((provider) => {\n const options = provider.store.get<Omit<KeywordDefinition, \"compile\">>(\"ajv:keyword\", {})!;\n const service = inject(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() {\n return injector().getProviders(\"ajv:formats\");\n}\n\nfunction getFormats(): {name: string; options: Format}[] {\n return getFormatsProviders().map((provider) => {\n const {name, options} = provider.store.get<any>(\"ajv:formats\", {})!;\n const service = inject<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\ninjectable(Ajv)\n .scope(ProviderScope.SINGLETON)\n .factory(() => {\n const {errorFormatter, keywords = [], ...props} = constant<IAjvSettings>(\"ajv\") || {};\n const options: Options = {\n verbose: false,\n coerceTypes: true,\n strict: false,\n keywords: [...keywords, ...bindKeywords()],\n discriminator: true,\n allErrors: true,\n ...props\n };\n\n // @ts-ignore\n const ajv = new Ajv(options);\n\n // add support for custom error messages\n // @ts-ignore\n AjvErrors(ajv);\n // @ts-ignore\n AjvFormats(ajv as any);\n\n getFormats().forEach(({name, options}) => {\n ajv.addFormat(name, options);\n });\n\n return ajv;\n });\n"],"names":["getHandler","key","service","getKeywordProviders","injector","bindKeywords","provider","options","inject","cleanObject","getFormatsProviders","getFormats","name","_a","injectable","Ajv","ProviderScope","errorFormatter","keywords","props","constant","ajv","AjvErrors","AjvFormats"],"mappings":";;;;;AASA,SAASA,EAAWC,GAAaC,G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,IACb,QAAQ;AAAA,IACR,UAAU,CAAC,GAAGW,GAAU,GAAGb,GAAc;AAAA,IACzC,eAAe;AAAA,IACf,WAAW;AAAA,IACX,GAAGc;AAAA,EACL,GAGME,IAAM,IAAIN,EAAIR,CAAO;AAI3B,SAAAe,EAAUD,CAAG,GAEbE,EAAWF,CAAU,GAErBV,EAAA,EAAa,QAAQ,CAAC,EAAC,MAAAC,GAAM,SAAAL,QAAa;AACpC,IAAAc,EAAA,UAAUT,GAAML,CAAO;AAAA,EAAA,CAC5B,GAEMc;AACT,CAAC;"}
@@ -1,65 +0,0 @@
1
- import "./Ajv.js";
2
- import { deepClone as h, nameOf as p, getValue as u, prototypeOf as y, setValue as v } from "@tsed/core";
3
- import { injectable as j, constant as d, inject as g } from "@tsed/di";
4
- import { getJsonSchema as C, JsonSchema as E, JsonEntityStore as V } from "@tsed/schema";
5
- import { Ajv as O } from "/home/runner/work/tsed/tsed/node_modules/ajv/dist/ajv.js";
6
- import { AjvValidationError as S } from "../errors/AjvValidationError.js";
7
- import { defaultErrorFormatter as w } from "../utils/defaultErrorFormatter.js";
8
- import { getPath as P } from "../utils/getPath.js";
9
- class A {
10
- constructor() {
11
- this.name = "ajv", this.errorFormatter = d("ajv.errorFormatter", w), this.returnsCoercedValues = d("ajv.returnsCoercedValues"), this.ajv = g(O);
12
- }
13
- async validate(t, i) {
14
- let {
15
- schema: a,
16
- type: s,
17
- collectionType: r,
18
- returnsCoercedValues: m = this.returnsCoercedValues,
19
- ...e
20
- } = this.mapOptions(i);
21
- const o = a || C(s, { ...e, customKeys: !0 });
22
- if (o) {
23
- const n = this.returnsCoercedValues ? t : h(t), c = this.ajv.compile(o), f = await c(n), { errors: l } = c;
24
- if (!f && l)
25
- throw this.mapErrors(l, {
26
- type: s,
27
- collectionType: r,
28
- async: !0,
29
- value: n
30
- });
31
- if (m)
32
- return n;
33
- }
34
- return t;
35
- }
36
- mapOptions(t) {
37
- return t instanceof E ? {
38
- schema: t.toJSON({ customKeys: !0 })
39
- } : t;
40
- }
41
- mapErrors(t, i) {
42
- const { type: a, collectionType: s, value: r } = i, m = t.map((e) => {
43
- s && (e.collectionName = p(s));
44
- const o = P(e);
45
- return e.data || (o ? e.data = u(r, o.replace(/^\./, "")) : e.schemaPath !== "#/required" && (e.data = r)), o && o.match(/pwd|password|mdp|secret/) && (e.data = "[REDACTED]"), a && (e.modelName = p(a), e.message = this.mapClassError(e, a)), this.errorFormatter.call(this, e, {});
46
- }).join(`
47
- `);
48
- return new S(m, t);
49
- }
50
- mapClassError(t, i) {
51
- var s;
52
- const a = u(t, "params.missingProperty");
53
- if (a) {
54
- const r = V.from(y(i), a);
55
- if (r)
56
- return v(t, "params.missingProperty", r.name || a), (s = t.message) == null ? void 0 : s.replace(`'${a}'`, `'${r.name || a}'`);
57
- }
58
- return t.message;
59
- }
60
- }
61
- j(A).type("validator:service");
62
- export {
63
- A as AjvService
64
- };
65
- //# sourceMappingURL=AjvService.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AjvService.js","sources":["../../../src/services/AjvService.ts"],"sourcesContent":["import \"./Ajv.js\";\n\nimport {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\";\n\nimport {AjvValidationError} from \"../errors/AjvValidationError.js\";\nimport {AjvErrorObject, ErrorFormatter} from \"../interfaces/IAjvSettings.js\";\nimport {defaultErrorFormatter} from \"../utils/defaultErrorFormatter.js\";\nimport {getPath} from \"../utils/getPath.js\";\n\nexport interface AjvValidateOptions extends Record<string, any> {\n schema?: JsonSchema | Partial<JsonSchemaObject>;\n type?: Type<any> | any;\n collectionType?: Type<any> | any;\n returnsCoercedValues?: boolean;\n}\n\nexport class AjvService {\n readonly name = \"ajv\";\n protected errorFormatter = constant<ErrorFormatter>(\"ajv.errorFormatter\", defaultErrorFormatter);\n protected returnsCoercedValues = constant<boolean>(\"ajv.returnsCoercedValues\");\n protected ajv = inject(Ajv);\n\n async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {\n let {\n schema: defaultSchema,\n type,\n collectionType,\n returnsCoercedValues = this.returnsCoercedValues,\n ...additionalOptions\n } = this.mapOptions(options);\n\n const schema = defaultSchema || getJsonSchema(type, {...additionalOptions, customKeys: true});\n\n if (schema) {\n const localValue = this.returnsCoercedValues ? value : 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 if (returnsCoercedValues) {\n return localValue;\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\ninjectable(AjvService).type(\"validator:service\");\n"],"names":["AjvService","constant","defaultErrorFormatter","inject","Ajv","value","options","defaultSchema","type","collectionType","returnsCoercedValues","additionalOptions","schema","getJsonSchema","localValue","deepClone","validate","valid","errors","JsonSchema","message","error","nameOf","dataPath","getPath","getValue","AjvValidationError","targetType","propertyKey","store","JsonEntityStore","prototypeOf","setValue","_a","injectable"],"mappings":";;;;;;;;AAmBO,MAAMA,EAAW;AAAA,EAAjB,cAAA;AACL,SAAS,OAAO,OACN,KAAA,iBAAiBC,EAAyB,sBAAsBC,CAAqB,GACrF,KAAA,uBAAuBD,EAAkB,0BAA0B,GACnE,KAAA,MAAME,EAAOC,CAAG;AAAA,EAAA;AAAA,EAE1B,MAAM,SAASC,GAAYC,GAAwD;AAC7E,QAAA;AAAA,MACF,QAAQC;AAAA,MACR,MAAAC;AAAA,MACA,gBAAAC;AAAA,MACA,sBAAAC,IAAuB,KAAK;AAAA,MAC5B,GAAGC;AAAA,IAAA,IACD,KAAK,WAAWL,CAAO;AAErB,UAAAM,IAASL,KAAiBM,EAAcL,GAAM,EAAC,GAAGG,GAAmB,YAAY,IAAK;AAE5F,QAAIC,GAAQ;AACV,YAAME,IAAa,KAAK,uBAAuBT,IAAQU,EAAUV,CAAK,GAChEW,IAAW,KAAK,IAAI,QAAQJ,CAAM,GAElCK,IAAQ,MAAMD,EAASF,CAAU,GACjC,EAAC,QAAAI,MAAUF;AAEb,UAAA,CAACC,KAASC;AACN,cAAA,KAAK,UAAUA,GAAQ;AAAA,UAC3B,MAAAV;AAAA,UACA,gBAAAC;AAAA,UACA,OAAO;AAAA,UACP,OAAOK;AAAA,QAAA,CACR;AAGH,UAAIJ;AACK,eAAAI;AAAA,IACT;AAGK,WAAAT;AAAA,EAAA;AAAA,EAGC,WAAWC,GAA8D;AACjF,WAAIA,aAAmBa,IACd;AAAA,MACL,QAAQb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SON.stringify(a.data === void 0 ? "undefined" : a.data), n = (t) => t.filter(Boolean).join("").trim(), [, s, ...m] = c(a).split("."), i = m.length ? "." + m.join(".") : "";
5
- if (a.dataPath = d(a), a.collectionName) {
6
- const t = a.modelName || "";
7
- switch (a.collectionName) {
8
- case "Array":
9
- return n([`${t}[${s}]${i}`, ` ${a.message}. Given value: ${e}`]);
10
- case "Map":
11
- return n([`Map<${s}, ${t}>${i}`, ` ${a.message}. Given value: ${e}`]);
12
- case "Set":
13
- return n([`Set<${s}, ${t}>${i}`, ` ${a.message}. Given value: ${e}`]);
14
- }
15
- }
16
- return n([
17
- !a.modelName && "Value",
18
- `${a.modelName || ""}`,
19
- !((l = a == null ? void 0 : a.params) != null && l.missingProperty) && a.dataPath || (($ = a == null ? void 0 : a.params) == null ? void 0 : $.missingProperty) && (a.instancePath || "").replace(/\//g, "."),
20
- ` ${a.message}. Given value: ${e}`
21
- ]);
22
- }
23
- export {
24
- p as defaultErrorFormatter
25
- };
26
- //# sourceMappingURL=defaultErrorFormatter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defaultErrorFormatter.js","sources":["../../../src/utils/defaultErrorFormatter.ts"],"sourcesContent":["import {AjvErrorObject} from \"../interfaces/IAjvSettings.js\";\nimport {getInstancePath, getPath} from \"./getPath.js\";\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 const [, indexPath, ...paths] = getInstancePath(error).split(\".\");\n const deepPaths = paths.length ? \".\" + paths.join(\".\") : \"\";\n\n error.dataPath = getPath(error);\n\n if (error.collectionName) {\n const modelName = error.modelName || \"\";\n\n switch (error.collectionName) {\n case \"Array\":\n return join([`${modelName}[${indexPath}]${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n case \"Map\":\n return join([`Map<${indexPath}, ${modelName}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n case \"Set\":\n return join([`Set<${indexPath}, ${modelName}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);\n }\n }\n\n return join([\n !error.modelName && \"Value\",\n `${error.modelName || \"\"}`,\n (!error?.params?.missingProperty && error.dataPath) ||\n (error?.params?.missingProperty && (error.instancePath || \"\").replace(/\\//g, \".\")),\n ` ${error.message}. Given value: ${value}`\n ]);\n}\n"],"names":["defaultErrorFormatter","error","value","join","list","indexPath","paths","getInstancePath","deepPaths","getPath","modelName","_a","_b"],"mappings":";AAGO,SAASA,EAAsBC,GAA4C;;AAC1E,QAAAC,IAAQ,KAAK,UAAUD,EAAM,SAAS,SAAY,cAAcA,EAAM,IAAI,GAC1EE,IAAO,CAACC,MAAwBA,EAAK,OAAO,OAAO,EAAE,KAAK,EAAE,EAAE,KAAK,GAEnE,CAAG,EAAAC,GAAW,GAAGC,CAAK,IAAIC,EAAgBN,CAAK,EAAE,MAAM,GAAG,GAC1DO,IAAYF,EAAM,SAAS,MAAMA,EAAM,KAAK,GAAG,IAAI;AAIzD,MAFML,EAAA,WAAWQ,EAAQR,CAAK,GAE1BA,EAAM,gBAAgB;AAClB,UAAAS,IAAYT,EAAM,aAAa;AAErC,YAAQA,EAAM,gBAAgB;AAAA,MAC5B,KAAK;AACH,eAAOE,EAAK,CAAC,GAAGO,CAAS,IAAIL,CAAS,IAAIG,CAAS,IAAI,IAAIP,EAAM,OAAO,kBAAkBC,CAAK,EAAE,CAAC;AAAA,MACpG,KAAK;AACH,eAAOC,EAAK,CAAC,OAAOE,CAAS,KAAKK,CAAS,IAAIF,CAAS,IAAI,IAAIP,EAAM,OAAO,kBAAkBC,CAAK,EAAE,CAAC;AAAA,MACzG,KAAK;AACH,eAAOC,EAAK,CAAC,OAAOE,CAAS,KAAKK,CAAS,IAAIF,CAAS,IAAI,IAAIP,EAAM,OAAO,kBAAkBC,CAAK,EAAE,CAAC;AAAA,IAAA;AAAA,EAC3G;AAGF,SAAOC,EAAK;AAAA,IACV,CAACF,EAAM,aAAa;AAAA,IACpB,GAAGA,EAAM,aAAa,EAAE;AAAA,IACvB,GAACU,IAAAV,KAAA,gBAAAA,EAAO,WAAP,QAAAU,EAAe,oBAAmBV,EAAM,cACvCW,IAAAX,KAAA,gBAAAA,EAAO,WAAP,gBAAAW,EAAe,qBAAoBX,EAAM,gBAAgB,IAAI,QAAQ,OAAO,GAAG;AAAA,IAClF,IAAIA,EAAM,OAAO,kBAAkBC,CAAK;AAAA,EAAA,CACzC;AACH;"}
@@ -1,12 +0,0 @@
1
- import { getValue as a } from "@tsed/core";
2
- function i(t) {
3
- return t.params && t.params.missingProperty ? `.${t.params.missingProperty}` : n(t);
4
- }
5
- function n(t) {
6
- return a(t, "dataPath", a(t, "instancePath", "")).replace(/\//gi, ".");
7
- }
8
- export {
9
- n as getInstancePath,
10
- i as getPath
11
- };
12
- //# sourceMappingURL=getPath.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getPath.js","sources":["../../../src/utils/getPath.ts"],"sourcesContent":["import {getValue} from \"@tsed/core\";\n\nexport function getPath(error: any) {\n if (error.params && error.params.missingProperty) {\n return `.${error.params.missingProperty}`;\n }\n\n return getInstancePath(error);\n}\n\nexport function getInstancePath(error: any) {\n return getValue(error, \"dataPath\", getValue(error, \"instancePath\", \"\")).replace(/\\//gi, \".\");\n}\n"],"names":["getPath","error","getInstancePath","getValue"],"mappings":";AAEO,SAASA,EAAQC,GAAY;AAClC,SAAIA,EAAM,UAAUA,EAAM,OAAO,kBACxB,IAAIA,EAAM,OAAO,eAAe,KAGlCC,EAAgBD,CAAK;AAC9B;AAEO,SAASC,EAAgBD,GAAY;AACnC,SAAAE,EAASF,GAAO,YAAYE,EAASF,GAAO,gBAAgB,EAAE,CAAC,EAAE,QAAQ,QAAQ,GAAG;AAC7F;"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
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"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate.js","sourceRoot":"","sources":["../../../src/fn/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,UAAU,CAAC;AAGhC,OAAO,EAAC,UAAU,EAA0B,MAAM,2BAA2B,CAAC;AAE9E;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc,EAAE,OAAwC;IAC/E,OAAO,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACrD,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,kBAAkB,CAAC;AACjC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kCAAkC,CAAC;AACjD,cAAc,oBAAoB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"FormatsMethods.js","sourceRoot":"","sources":["../../../src/interfaces/FormatsMethods.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"IAjvSettings.js","sourceRoot":"","sources":["../../../src/interfaces/IAjvSettings.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"KeywordMethods.js","sourceRoot":"","sources":["../../../src/interfaces/KeywordMethods.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"Ajv.js","sourceRoot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,IAAI,KAAK,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;QACjD,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;IAC5C,CAAC;IAED,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAU;IACxC,OAAO,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,KAAK,EAAE,cAAc,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC/F,CAAC"}
@@ -1,24 +0,0 @@
1
- import {StoreSet, useDecorators} from "@tsed/core";
2
- import {Injectable} from "@tsed/di";
3
- import {AsyncFormatDefinition, FormatDefinition} from "ajv";
4
-
5
- export type FormatsOptions = Omit<FormatDefinition<any>, "validate" | "compare"> | Omit<AsyncFormatDefinition<any>, "validate" | "compare">;
6
-
7
- /**
8
- * Create a new custom formats validator
9
- * @param name
10
- * @param options
11
- * @decorator
12
- * @ajv
13
- */
14
- export function Formats(name: string, options: FormatsOptions = {}): ClassDecorator {
15
- return useDecorators(
16
- Injectable({
17
- type: "ajv:formats"
18
- }),
19
- StoreSet("ajv:formats", {
20
- name,
21
- options
22
- })
23
- );
24
- }
@@ -1,26 +0,0 @@
1
- import {StoreSet, useDecorators} from "@tsed/core";
2
- import {Injectable} from "@tsed/di";
3
- import {JsonSchema} from "@tsed/schema";
4
- import {AnySchemaObject, KeywordDefinition} from "ajv";
5
-
6
- export type KeywordOptions = Partial<Omit<KeywordDefinition, "metaSchema">> & {
7
- metaSchema?: AnySchemaObject | JsonSchema;
8
- };
9
-
10
- /**
11
- * Create new keyword custom validator
12
- * @param options
13
- * @decorator
14
- * @ajv
15
- */
16
- export function Keyword(options: KeywordOptions): ClassDecorator {
17
- return useDecorators(
18
- Injectable({
19
- type: "ajv:keyword"
20
- }),
21
- StoreSet("ajv:keyword", {
22
- ...options,
23
- metaSchema: options.metaSchema && options.metaSchema.toJSON ? options.metaSchema.toJSON() : options.metaSchema
24
- })
25
- );
26
- }
@@ -1,11 +0,0 @@
1
- import {BadRequest} from "@tsed/exceptions";
2
-
3
- export class AjvValidationError extends BadRequest {
4
- public name: string = "AJV_VALIDATION_ERROR";
5
- public errors: any[];
6
-
7
- constructor(message: string, errors: any[] = []) {
8
- super(message);
9
- this.errors = errors;
10
- }
11
- }
@@ -1,20 +0,0 @@
1
- import {DITest} from "@tsed/di";
2
- import {object, string} from "@tsed/schema";
3
-
4
- import {validate} from "./validate.js";
5
-
6
- describe("validate", () => {
7
- beforeEach(() => DITest.create());
8
- afterEach(() => DITest.reset());
9
-
10
- it("should validate a value against a JSON schema or a type", async () => {
11
- const value = {name: "John"};
12
- const options = object({
13
- name: string()
14
- });
15
-
16
- const result = await validate(value, options);
17
-
18
- expect(result).toEqual(value);
19
- });
20
- });
@@ -1,13 +0,0 @@
1
- import {inject} from "@tsed/di";
2
- import {JsonSchema} from "@tsed/schema";
3
-
4
- import {AjvService, type AjvValidateOptions} from "../services/AjvService.js";
5
-
6
- /**
7
- * Validate a value against a JSON schema or a type.
8
- * @param value
9
- * @param options
10
- */
11
- export function validate(value: unknown, options: AjvValidateOptions | JsonSchema) {
12
- return inject(AjvService).validate(value, options);
13
- }
package/src/index.ts DELETED
@@ -1,15 +0,0 @@
1
- /**
2
- * @file Automatically generated by @tsed/barrels.
3
- */
4
- export * from "./decorators/formats.js";
5
- export * from "./decorators/keyword.js";
6
- export * from "./errors/AjvValidationError.js";
7
- export * from "./fn/validate.js";
8
- export * from "./interfaces/FormatsMethods.js";
9
- export * from "./interfaces/IAjvSettings.js";
10
- export * from "./interfaces/interfaces.js";
11
- export * from "./interfaces/KeywordMethods.js";
12
- export * from "./services/Ajv.js";
13
- export * from "./services/AjvService.js";
14
- export * from "./utils/defaultErrorFormatter.js";
15
- export * from "./utils/getPath.js";
@@ -1,5 +0,0 @@
1
- export interface FormatsMethods<T extends string | number> {
2
- compare?: (data1: T, data2: T) => number | undefined;
3
-
4
- validate(data: T): boolean;
5
- }
@@ -1,23 +0,0 @@
1
- import {ErrorObject, Options} from "ajv";
2
-
3
- /**
4
- *
5
- */
6
- export interface AjvErrorObject extends ErrorObject {
7
- modelName: string;
8
- collectionName?: string;
9
- }
10
-
11
- /**
12
- *
13
- */
14
- export type ErrorFormatter = (error: AjvErrorObject) => string;
15
-
16
- /**
17
- *
18
- */
19
- export interface IAjvSettings extends Options {
20
- Ajv?: any;
21
- errorFormatter?: ErrorFormatter;
22
- returnsCoercedValues?: boolean;
23
- }
@@ -1,23 +0,0 @@
1
- import {AnySchemaObject, KeywordCxt, SchemaObjCxt, ValidateFunction} from "ajv";
2
-
3
- export interface AjvDataValidationCtx<T extends string | number = string | number> {
4
- dataPath: string;
5
- parentData: {[K in T]: any}; // object or array
6
- parentDataProperty: T; // string or number
7
- rootData: Record<string, any> | any[];
8
- dynamicAnchors: {[Ref in string]?: ValidateFunction};
9
- }
10
-
11
- export interface AjvDataValidateFunction {
12
- (...args: Parameters<ValidateFunction>): boolean | Promise<any>;
13
- }
14
-
15
- export interface AjvSchemaValidateFunction {
16
- (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: AjvDataValidationCtx): boolean | Promise<any>;
17
- }
18
-
19
- export interface KeywordMethods {
20
- code?: (cxt: KeywordCxt, ruleType?: string) => void;
21
- compile?: (schema: any, parentSchema: AnySchemaObject, it: SchemaObjCxt) => AjvDataValidateFunction;
22
- validate?: AjvSchemaValidateFunction | AjvDataValidateFunction;
23
- }
@@ -1,10 +0,0 @@
1
- import {IAjvSettings} from "./IAjvSettings.js";
2
-
3
- declare global {
4
- namespace TsED {
5
- interface Configuration {
6
- // @ts-ignore
7
- ajv?: IAjvSettings;
8
- }
9
- }
10
- }
@@ -1,39 +0,0 @@
1
- import {PlatformTest} from "@tsed/platform-http/testing";
2
- import {Ajv} from "ajv";
3
-
4
- describe("Ajv", () => {
5
- beforeEach(() => PlatformTest.create());
6
- afterEach(() => PlatformTest.reset());
7
- it("should create a new Ajv instance", async () => {
8
- const ajv = await PlatformTest.invoke<Ajv>(Ajv);
9
-
10
- expect(ajv).toBeInstanceOf(Ajv);
11
- expect(
12
- ajv.validate(
13
- {
14
- type: "object"
15
- },
16
- {}
17
- )
18
- ).toEqual(true);
19
-
20
- ajv.validate(
21
- {
22
- type: "object"
23
- },
24
- []
25
- );
26
-
27
- expect(ajv.errors).toEqual([
28
- {
29
- instancePath: "",
30
- keyword: "type",
31
- message: "must be object",
32
- params: {
33
- type: "object"
34
- },
35
- schemaPath: "#/type"
36
- }
37
- ]);
38
- });
39
- });
@@ -1,84 +0,0 @@
1
- import {cleanObject} from "@tsed/core";
2
- import {constant, inject, injectable, injector, ProviderScope} from "@tsed/di";
3
- import {Ajv, Format, KeywordDefinition, Options, Vocabulary} from "ajv";
4
- import AjvErrors from "ajv-errors";
5
- import AjvFormats from "ajv-formats";
6
-
7
- import {FormatsMethods} from "../interfaces/FormatsMethods.js";
8
- import {IAjvSettings} from "../interfaces/IAjvSettings.js";
9
-
10
- function getHandler(key: string, service: any) {
11
- if (service[key]) {
12
- return service[key].bind(service);
13
- }
14
- }
15
-
16
- function getKeywordProviders() {
17
- return injector().getProviders("ajv:keyword");
18
- }
19
-
20
- function bindKeywords(): Vocabulary {
21
- return getKeywordProviders().map((provider) => {
22
- const options = provider.store.get<Omit<KeywordDefinition, "compile">>("ajv:keyword", {})!;
23
- const service = inject(provider.token);
24
-
25
- return <KeywordDefinition>cleanObject({
26
- coerceTypes: "array",
27
- ...options,
28
- validate: getHandler("validate", service),
29
- compile: getHandler("compile", service),
30
- code: getHandler("code", service),
31
- macro: getHandler("macro", service)
32
- });
33
- });
34
- }
35
-
36
- function getFormatsProviders() {
37
- return injector().getProviders("ajv:formats");
38
- }
39
-
40
- function getFormats(): {name: string; options: Format}[] {
41
- return getFormatsProviders().map((provider) => {
42
- const {name, options} = provider.store.get<any>("ajv:formats", {})!;
43
- const service = inject<FormatsMethods<any>>(provider.token);
44
-
45
- return {
46
- name,
47
- options: {
48
- ...options,
49
- validate: service.validate.bind(service),
50
- compare: service.compare?.bind(service)
51
- }
52
- };
53
- });
54
- }
55
-
56
- injectable(Ajv)
57
- .scope(ProviderScope.SINGLETON)
58
- .factory(() => {
59
- const {errorFormatter, keywords = [], ...props} = constant<IAjvSettings>("ajv") || {};
60
- const options: Options = {
61
- verbose: false,
62
- coerceTypes: true,
63
- strict: false,
64
- keywords: [...keywords, ...bindKeywords()],
65
- discriminator: true,
66
- allErrors: true,
67
- ...props
68
- };
69
-
70
- // @ts-ignore
71
- const ajv = new Ajv(options);
72
-
73
- // add support for custom error messages
74
- // @ts-ignore
75
- AjvErrors(ajv);
76
- // @ts-ignore
77
- AjvFormats(ajv as any);
78
-
79
- getFormats().forEach(({name, options}) => {
80
- ajv.addFormat(name, options);
81
- });
82
-
83
- return ajv;
84
- });
@@ -1,47 +0,0 @@
1
- import {catchAsyncError} from "@tsed/core";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
- import {object, string} from "@tsed/schema";
4
-
5
- import {AjvService} from "./AjvService.js";
6
-
7
- describe("AjvService", () => {
8
- beforeEach(() => PlatformTest.create());
9
- afterEach(() => PlatformTest.reset());
10
-
11
- it("should use the function api as schema", async () => {
12
- const ajvService = PlatformTest.get<AjvService>(AjvService);
13
-
14
- const error: any = await catchAsyncError(() => ajvService.validate("test", string().minLength(5)));
15
- expect(error.errors).toEqual([
16
- {
17
- data: "test",
18
- dataPath: "",
19
- instancePath: "",
20
- keyword: "minLength",
21
- message: "must NOT have fewer than 5 characters",
22
- params: {
23
- limit: 5
24
- },
25
- schemaPath: "#/minLength"
26
- }
27
- ]);
28
- });
29
-
30
- it("should use the function api as schema (required)", async () => {
31
- const ajvService = PlatformTest.get<AjvService>(AjvService);
32
-
33
- const error: any = await catchAsyncError(() => ajvService.validate({}, object({test: string().required()})));
34
- expect(error.errors).toEqual([
35
- {
36
- dataPath: ".test",
37
- instancePath: "",
38
- keyword: "required",
39
- message: "must have required property 'test'",
40
- params: {
41
- missingProperty: "test"
42
- },
43
- schemaPath: "#/required"
44
- }
45
- ]);
46
- });
47
- });
@@ -1,123 +0,0 @@
1
- import "./Ajv.js";
2
-
3
- import {deepClone, getValue, nameOf, prototypeOf, setValue, Type} from "@tsed/core";
4
- import {constant, inject, injectable} from "@tsed/di";
5
- import {getJsonSchema, JsonEntityStore, JsonSchema, JsonSchemaObject} from "@tsed/schema";
6
- import {Ajv, ErrorObject} from "ajv";
7
-
8
- import {AjvValidationError} from "../errors/AjvValidationError.js";
9
- import {AjvErrorObject, ErrorFormatter} from "../interfaces/IAjvSettings.js";
10
- import {defaultErrorFormatter} from "../utils/defaultErrorFormatter.js";
11
- import {getPath} from "../utils/getPath.js";
12
-
13
- export interface AjvValidateOptions extends Record<string, any> {
14
- schema?: JsonSchema | Partial<JsonSchemaObject>;
15
- type?: Type<any> | any;
16
- collectionType?: Type<any> | any;
17
- returnsCoercedValues?: boolean;
18
- }
19
-
20
- export class AjvService {
21
- readonly name = "ajv";
22
- protected errorFormatter = constant<ErrorFormatter>("ajv.errorFormatter", defaultErrorFormatter);
23
- protected returnsCoercedValues = constant<boolean>("ajv.returnsCoercedValues");
24
- protected ajv = inject(Ajv);
25
-
26
- async validate(value: any, options: AjvValidateOptions | JsonSchema): Promise<any> {
27
- let {
28
- schema: defaultSchema,
29
- type,
30
- collectionType,
31
- returnsCoercedValues = this.returnsCoercedValues,
32
- ...additionalOptions
33
- } = this.mapOptions(options);
34
-
35
- const schema = defaultSchema || getJsonSchema(type, {...additionalOptions, customKeys: true});
36
-
37
- if (schema) {
38
- const localValue = this.returnsCoercedValues ? value : deepClone(value);
39
- const validate = this.ajv.compile(schema);
40
-
41
- const valid = await validate(localValue);
42
- const {errors} = validate;
43
-
44
- if (!valid && errors) {
45
- throw this.mapErrors(errors, {
46
- type,
47
- collectionType,
48
- async: true,
49
- value: localValue
50
- });
51
- }
52
-
53
- if (returnsCoercedValues) {
54
- return localValue;
55
- }
56
- }
57
-
58
- return value;
59
- }
60
-
61
- protected mapOptions(options: AjvValidateOptions | JsonSchema): AjvValidateOptions {
62
- if (options instanceof JsonSchema) {
63
- return {
64
- schema: options.toJSON({customKeys: true})
65
- };
66
- }
67
-
68
- return options;
69
- }
70
-
71
- protected mapErrors(errors: ErrorObject[], options: any) {
72
- const {type, collectionType, value} = options;
73
-
74
- const message = errors
75
- .map((error: AjvErrorObject) => {
76
- if (collectionType) {
77
- error.collectionName = nameOf(collectionType);
78
- }
79
-
80
- const dataPath = getPath(error);
81
-
82
- if (!error.data) {
83
- if (dataPath) {
84
- error.data = getValue(value, dataPath.replace(/^\./, ""));
85
- } else if (error.schemaPath !== "#/required") {
86
- error.data = value;
87
- }
88
- }
89
-
90
- if (dataPath && dataPath.match(/pwd|password|mdp|secret/)) {
91
- error.data = "[REDACTED]";
92
- }
93
-
94
- if (type) {
95
- error.modelName = nameOf(type);
96
- error.message = this.mapClassError(error, type);
97
- }
98
-
99
- return this.errorFormatter.call(this, error, {});
100
- })
101
- .join("\n");
102
-
103
- return new AjvValidationError(message, errors);
104
- }
105
-
106
- protected mapClassError(error: AjvErrorObject, targetType: Type<any>) {
107
- const propertyKey = getValue(error, "params.missingProperty");
108
-
109
- if (propertyKey) {
110
- const store = JsonEntityStore.from<JsonEntityStore>(prototypeOf(targetType), propertyKey);
111
-
112
- if (store) {
113
- setValue(error, "params.missingProperty", store.name || propertyKey);
114
-
115
- return error.message?.replace(`'${propertyKey}'`, `'${store.name || propertyKey}'`);
116
- }
117
- }
118
-
119
- return error.message;
120
- }
121
- }
122
-
123
- injectable(AjvService).type("validator:service");
@@ -1,73 +0,0 @@
1
- import {defaultErrorFormatter} from "./defaultErrorFormatter.js";
2
-
3
- describe("defaultErrorFormatter", () => {
4
- it("should extract data and format error (data = undefined)", () => {
5
- expect(
6
- defaultErrorFormatter({
7
- keyword: "maximum",
8
- dataPath: "/length",
9
- schemaPath: "#/properties/length/maximum",
10
- params: {comparison: "<=", limit: 250},
11
- message: "should be <= 250",
12
- data: undefined,
13
- modelName: "RandomStringModel"
14
- } as any)
15
- ).toEqual('RandomStringModel.length should be <= 250. Given value: "undefined"');
16
- });
17
-
18
- it("should extract data and format error (modelName = '' & collectionName)", () => {
19
- expect(
20
- defaultErrorFormatter({
21
- keyword: "maximum",
22
- dataPath: "/0",
23
- schemaPath: "#/properties/length/maximum",
24
- params: {comparison: "<=", limit: 250},
25
- message: "should be <= 250",
26
- data: undefined,
27
- modelName: undefined,
28
- collectionName: "Array"
29
- } as any)
30
- ).toEqual('[0] should be <= 250. Given value: "undefined"');
31
- });
32
-
33
- it("should extract data and format error (modelName = '')", () => {
34
- expect(
35
- defaultErrorFormatter({
36
- keyword: "maximum",
37
- dataPath: "/length",
38
- schemaPath: "#/properties/length/maximum",
39
- params: {comparison: "<=", limit: 250},
40
- message: "should be <= 250",
41
- data: undefined,
42
- modelName: undefined
43
- } as any)
44
- ).toEqual('Value.length should be <= 250. Given value: "undefined"');
45
- });
46
-
47
- it("should extract data and format error", () => {
48
- expect(
49
- defaultErrorFormatter({
50
- keyword: "maximum",
51
- dataPath: "/length",
52
- schemaPath: "#/properties/length/maximum",
53
- params: {comparison: "<=", limit: 250},
54
- message: "should be <= 250",
55
- data: 251,
56
- modelName: "RandomStringModel"
57
- } as any)
58
- ).toEqual("RandomStringModel.length should be <= 250. Given value: 251");
59
- });
60
- it("should extract data and format error(2)", () => {
61
- expect(
62
- defaultErrorFormatter({
63
- instancePath: "/length",
64
- schemaPath: "#/properties/length/maximum",
65
- keyword: "maximum",
66
- params: {comparison: "<=", limit: 250},
67
- message: "must be <= 250",
68
- data: {length: 251},
69
- modelName: "RandomStringModel"
70
- } as any)
71
- ).toEqual('RandomStringModel.length must be <= 250. Given value: {"length":251}');
72
- });
73
- });
@@ -1,33 +0,0 @@
1
- import {AjvErrorObject} from "../interfaces/IAjvSettings.js";
2
- import {getInstancePath, getPath} from "./getPath.js";
3
-
4
- export function defaultErrorFormatter(error: AjvErrorObject & {dataPath: string}) {
5
- const value = JSON.stringify(error.data === undefined ? "undefined" : error.data);
6
- const join = (list: any[]): string => list.filter(Boolean).join("").trim();
7
-
8
- const [, indexPath, ...paths] = getInstancePath(error).split(".");
9
- const deepPaths = paths.length ? "." + paths.join(".") : "";
10
-
11
- error.dataPath = getPath(error);
12
-
13
- if (error.collectionName) {
14
- const modelName = error.modelName || "";
15
-
16
- switch (error.collectionName) {
17
- case "Array":
18
- return join([`${modelName}[${indexPath}]${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
19
- case "Map":
20
- return join([`Map<${indexPath}, ${modelName}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
21
- case "Set":
22
- return join([`Set<${indexPath}, ${modelName}>${deepPaths}`, ` ${error.message}. Given value: ${value}`]);
23
- }
24
- }
25
-
26
- return join([
27
- !error.modelName && "Value",
28
- `${error.modelName || ""}`,
29
- (!error?.params?.missingProperty && error.dataPath) ||
30
- (error?.params?.missingProperty && (error.instancePath || "").replace(/\//g, ".")),
31
- ` ${error.message}. Given value: ${value}`
32
- ]);
33
- }
@@ -1,13 +0,0 @@
1
- import {getValue} from "@tsed/core";
2
-
3
- export function getPath(error: any) {
4
- if (error.params && error.params.missingProperty) {
5
- return `.${error.params.missingProperty}`;
6
- }
7
-
8
- return getInstancePath(error);
9
- }
10
-
11
- export function getInstancePath(error: any) {
12
- return getValue(error, "dataPath", getValue(error, "instancePath", "")).replace(/\//gi, ".");
13
- }
package/vite.config.mts DELETED
@@ -1,45 +0,0 @@
1
- import {defineConfig} from "vite";
2
- import {join} from "node:path";
3
- import {extname, relative} from "path";
4
- import {fileURLToPath} from "node:url";
5
- import {globbySync} from "globby";
6
-
7
- // https://vitejs.dev/config/
8
- export default defineConfig({
9
- plugins: [],
10
- build: {
11
- assetsInlineLimit: 0,
12
- sourcemap: true,
13
- outDir: "lib/browser",
14
- lib: {
15
- entry: join(import.meta.dirname, "src/index.ts"),
16
- formats: ["es"]
17
- },
18
- rollupOptions: {
19
- external: [/node_modules\/(.*)/, /^@tsed\//],
20
- input: Object.fromEntries(
21
- globbySync("src/**/*.ts", {
22
- ignore: [
23
- "**/*.spec.{ts,tsx}",
24
- "**/*.stories.{ts,tsx}",
25
- "**/__mocks__/**",
26
- "**/__mock__/**",
27
- "**/interfaces/**"
28
- ]
29
- }).map((file) => [
30
- // The name of the entry point
31
- // lib/nested/foo.ts becomes nested/foo
32
- relative("src", file.slice(0, file.length - extname(file).length)),
33
- // The absolute path to the entry file
34
- // lib/nested/foo.ts becomes /project/lib/nested/foo.ts
35
- fileURLToPath(new URL(file, import.meta.url))
36
- ])
37
- ),
38
- output: {
39
- assetFileNames: "assets/[name][extname]",
40
- chunkFileNames: "chunks/[name].js",
41
- entryFileNames: "[name].js"
42
- }
43
- }
44
- }
45
- });
package/vitest.config.mts DELETED
@@ -1,21 +0,0 @@
1
- // @ts-ignore
2
- import {presets} from "@tsed/vitest/presets";
3
- import {defineConfig} from "vitest/config";
4
-
5
- export default defineConfig(
6
- {
7
- ...presets,
8
- test: {
9
- ...presets.test,
10
- coverage: {
11
- ...presets.test.coverage,
12
- thresholds: {
13
- statements: 0,
14
- branches: 0,
15
- functions: 0,
16
- lines: 0
17
- }
18
- }
19
- }
20
- }
21
- );