vovk-ajv 0.0.0-draft.55 → 0.0.0-draft.56

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 (3) hide show
  1. package/index.d.ts +2 -1
  2. package/index.js +14 -11
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -5,9 +5,10 @@ type Lang = keyof typeof ajvLocalize;
5
5
  export type VovkAjvConfig = {
6
6
  options?: Options;
7
7
  localize?: Lang;
8
+ target: 'draft-2020-12' | 'draft-07';
8
9
  };
9
10
  declare const validateOnClientAjv: VovkValidateOnClient;
10
- declare const configure: ({ options: givenOptions, localize: givenLocalize }: VovkAjvConfig) => VovkValidateOnClient;
11
+ declare const configure: ({ options: givenOptions, localize: givenLocalize, target: givenTarget }: VovkAjvConfig) => VovkValidateOnClient;
11
12
  export declare const validateOnClient: typeof validateOnClientAjv & {
12
13
  configure: typeof configure;
13
14
  };
package/index.js CHANGED
@@ -5,12 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.validateOnClient = void 0;
7
7
  const ajv_1 = require("ajv");
8
+ const _2020_1 = __importDefault(require("ajv/dist/2020"));
8
9
  const vovk_1 = require("vovk");
9
10
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
10
11
  const ajv_i18n_1 = __importDefault(require("ajv-i18n"));
11
12
  const ajv_errors_1 = __importDefault(require("ajv-errors"));
12
- const createAjv = (options) => {
13
- const ajv = new ajv_1.Ajv({ allErrors: true, ...options });
13
+ const createAjv = (options, target) => {
14
+ const AjvClass = target === 'draft-2020-12' ? _2020_1.default : ajv_1.Ajv;
15
+ const ajv = new AjvClass({ allErrors: true, ...options });
14
16
  (0, ajv_formats_1.default)(ajv);
15
17
  (0, ajv_errors_1.default)(ajv);
16
18
  ajv.addKeyword('x-isDto');
@@ -32,26 +34,27 @@ const validateAll = ({ input, validation, ajv, localize = 'en', }) => {
32
34
  };
33
35
  const getConfig = (schema) => {
34
36
  const config = schema.config.libs?.ajv;
35
- const options = config?.options || {};
36
- const localize = config?.localize || 'en';
37
- return { options, localize };
37
+ const options = config?.options ?? {};
38
+ const localize = config?.localize ?? 'en';
39
+ const target = config?.target ?? 'draft-2020-12';
40
+ return { options, localize, target };
38
41
  };
39
42
  let ajvScope = null;
40
43
  const validateOnClientAjv = (input, validation, schema) => {
41
- const { options, localize } = getConfig(schema);
44
+ const { options, localize, target } = getConfig(schema);
42
45
  if (!ajvScope) {
43
- ajvScope = createAjv(options);
46
+ ajvScope = createAjv(options, target);
44
47
  }
45
48
  return validateAll({ input, validation, ajv: ajvScope, localize });
46
49
  };
47
- const configure = ({ options: givenOptions, localize: givenLocalize }) => (input, validation, schema) => {
48
- const { options, localize } = getConfig(schema);
49
- const ajv = createAjv({ ...options, ...givenOptions });
50
+ const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) => (input, validation, schema) => {
51
+ const { options, localize, target } = getConfig(schema);
52
+ const ajv = createAjv({ ...options, ...givenOptions }, givenTarget ?? target);
50
53
  validateAll({
51
54
  input,
52
55
  validation,
53
56
  ajv,
54
- localize: givenLocalize || localize,
57
+ localize: givenLocalize ?? localize,
55
58
  });
56
59
  };
57
60
  exports.validateOnClient = Object.assign(validateOnClientAjv, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.55",
3
+ "version": "0.0.0-draft.56",
4
4
  "description": "Local validation for JSON schemas for vovk-zod and other validation libraries for Vovk.ts",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -33,6 +33,6 @@
33
33
  "ajv-i18n": "^4.2.0"
34
34
  },
35
35
  "peerDependencies": {
36
- "vovk": "^3.0.0-draft.138"
36
+ "vovk": "^3.0.0-draft.140"
37
37
  }
38
38
  }