vovk-ajv 0.0.0-draft.83 → 0.0.0-draft.85

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 -4
  2. package/index.js +69 -92
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -7,9 +7,7 @@ export type VovkAjvConfig = {
7
7
  localize?: Lang;
8
8
  target?: 'draft-2020-12' | 'draft-07';
9
9
  };
10
- declare const validateOnClientAjv: VovkValidateOnClient;
11
- declare const configure: ({ options: givenOptions, localize: givenLocalize, target: givenTarget }: VovkAjvConfig) => VovkValidateOnClient;
12
- export declare const validateOnClient: typeof validateOnClientAjv & {
13
- configure: typeof configure;
10
+ export declare const validateOnClient: VovkValidateOnClient<unknown> & {
11
+ configure: ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }: VovkAjvConfig) => VovkValidateOnClient<unknown>;
14
12
  };
15
13
  export {};
package/index.js CHANGED
@@ -1,101 +1,78 @@
1
- 'use strict';
2
- var __importDefault =
3
- (this && this.__importDefault) ||
4
- function (mod) {
5
- return mod && mod.__esModule ? mod : { default: mod };
6
- };
7
- Object.defineProperty(exports, '__esModule', { value: true });
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
8
6
  exports.validateOnClient = void 0;
9
- const ajv_1 = require('ajv');
10
- const _2020_1 = __importDefault(require('ajv/dist/2020'));
11
- const ajv_formats_1 = __importDefault(require('ajv-formats'));
12
- const ajv_i18n_1 = __importDefault(require('ajv-i18n'));
13
- const ajv_errors_1 = __importDefault(require('ajv-errors'));
14
- const vovk_1 = require('vovk');
7
+ const ajv_1 = require("ajv");
8
+ const _2020_1 = __importDefault(require("ajv/dist/2020"));
9
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
10
+ const ajv_i18n_1 = __importDefault(require("ajv-i18n"));
11
+ const ajv_errors_1 = __importDefault(require("ajv-errors"));
12
+ const vovk_1 = require("vovk");
15
13
  const createAjv = (options, target) => {
16
- const AjvClass = target === 'draft-2020-12' ? _2020_1.default : ajv_1.Ajv;
17
- const ajv = new AjvClass({ allErrors: true, ...options });
18
- (0, ajv_formats_1.default)(ajv);
19
- (0, ajv_errors_1.default)(ajv);
20
- ajv.addKeyword('x-isDto');
21
- ajv.addKeyword('x-formData');
22
- ajv.addKeyword('x-tsType');
23
- ajv.addFormat('binary', {
24
- type: 'string',
25
- validate: (data) => data instanceof File || data instanceof Blob,
26
- });
27
- return ajv;
14
+ const AjvClass = target === 'draft-2020-12' ? _2020_1.default : ajv_1.Ajv;
15
+ const ajv = new AjvClass({ allErrors: true, ...options });
16
+ (0, ajv_formats_1.default)(ajv);
17
+ (0, ajv_errors_1.default)(ajv);
18
+ ajv.addKeyword('x-isDto');
19
+ ajv.addKeyword('x-formData');
20
+ ajv.addKeyword('x-tsType');
21
+ ajv.addFormat('binary', {
22
+ type: 'string',
23
+ validate: (data) => data instanceof File || data instanceof Blob,
24
+ });
25
+ return ajv;
28
26
  };
29
- const validate = ({ data, schema, localize = 'en', type, endpoint, options, target }) => {
30
- if (data && schema) {
31
- const schemaTarget = schema.$schema.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
32
- const ajv = createAjv(options ?? {}, target ?? schemaTarget);
33
- if (data instanceof FormData) {
34
- data = Object.fromEntries(data.entries());
27
+ const validate = ({ input, schema, localize = 'en', type, endpoint, options, target, }) => {
28
+ if (input && schema) {
29
+ const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
30
+ const ajv = createAjv(options ?? {}, target ?? schemaTarget);
31
+ if (input instanceof FormData) {
32
+ input = Object.fromEntries(input.entries());
33
+ }
34
+ const isValid = ajv.validate(schema, input);
35
+ if (!isValid) {
36
+ ajv_i18n_1.default[localize](ajv.errors);
37
+ throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${input instanceof FormData ? 'form' : type} on client: ${ajv.errorsText()}`, { input, errors: ajv.errors, endpoint });
38
+ }
35
39
  }
36
- const isValid = ajv.validate(schema, data);
37
- if (!isValid) {
38
- ajv_i18n_1.default[localize](ajv.errors);
39
- throw new vovk_1.HttpException(
40
- vovk_1.HttpStatus.NULL,
41
- `Ajv validation failed. Invalid ${data instanceof FormData ? 'form' : type} on client: ${ajv.errorsText()}`,
42
- { data, errors: ajv.errors, endpoint }
43
- );
44
- }
45
- }
46
- };
47
- const validateAll = ({ input, validation, localize = 'en', target, options }) => {
48
- validate({
49
- data: input.body,
50
- schema: validation.body,
51
- target,
52
- localize,
53
- endpoint: input.endpoint,
54
- options,
55
- type: 'body',
56
- });
57
- validate({
58
- data: input.query,
59
- schema: validation.query,
60
- target,
61
- localize,
62
- endpoint: input.endpoint,
63
- options,
64
- type: 'query',
65
- });
66
- validate({
67
- data: input.params,
68
- schema: validation.params,
69
- target,
70
- localize,
71
- endpoint: input.endpoint,
72
- options,
73
- type: 'params',
74
- });
75
40
  };
76
41
  const getConfig = (schema) => {
77
- const config = schema.meta?.config?.libs?.ajv;
78
- const options = config?.options ?? {};
79
- const localize = config?.localize ?? 'en';
80
- const target = config?.target;
81
- return { options, localize, target };
42
+ const config = schema.meta?.config?.libs?.ajv;
43
+ const options = config?.options ?? {};
44
+ const localize = config?.localize ?? 'en';
45
+ const target = config?.target;
46
+ return { options, localize, target };
82
47
  };
83
- const validateOnClientAjv = (input, validation, schema) => {
84
- const { options, localize, target } = getConfig(schema);
85
- return validateAll({ input, validation, target, localize, options });
86
- };
87
- const configure =
88
- ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) =>
89
- (input, validation, schema) => {
90
- const { options, localize, target } = getConfig(schema);
91
- validateAll({
92
- input,
93
- validation,
94
- target: givenTarget ?? target,
95
- localize: givenLocalize ?? localize,
96
- options: givenOptions ?? options,
97
- });
98
- };
48
+ const validateOnClientAjv = (0, vovk_1.createValidateOnClient)({
49
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
50
+ const { options, localize, target } = getConfig(fullSchema);
51
+ validate({
52
+ input,
53
+ schema,
54
+ target,
55
+ localize,
56
+ endpoint,
57
+ options,
58
+ type,
59
+ });
60
+ },
61
+ });
62
+ const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) => (0, vovk_1.createValidateOnClient)({
63
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
64
+ const { options, localize, target } = getConfig(fullSchema);
65
+ validate({
66
+ input,
67
+ schema,
68
+ target: givenTarget ?? target,
69
+ localize: givenLocalize ?? localize,
70
+ endpoint,
71
+ options: givenOptions ?? options,
72
+ type,
73
+ });
74
+ },
75
+ });
99
76
  exports.validateOnClient = Object.assign(validateOnClientAjv, {
100
- configure,
77
+ configure,
101
78
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.83",
3
+ "version": "0.0.0-draft.85",
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.324"
36
+ "vovk": "^3.0.0-draft.348"
37
37
  }
38
38
  }