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

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 (2) hide show
  1. package/index.js +86 -78
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,93 +1,101 @@
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 });
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 });
6
8
  exports.validateOnClient = void 0;
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");
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');
13
15
  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 });
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;
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;
26
28
  };
27
- const validate = ({ data, schema, localize = 'en', type, endpoint, options, target, }) => {
28
- if (data && 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 (data instanceof FormData) {
32
- data = Object.fromEntries(data.entries());
33
- }
34
- const isValid = ajv.validate(schema, data);
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 ${data instanceof FormData ? 'form' : type} on client: ${ajv.errorsText()}`, { data, errors: ajv.errors, endpoint });
38
- }
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());
39
35
  }
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
+ }
40
46
  };
41
- const validateAll = ({ input, validation, localize = 'en', target, options, }) => {
42
- validate({
43
- data: input.body,
44
- schema: validation.body,
45
- target,
46
- localize,
47
- endpoint: input.endpoint,
48
- options,
49
- type: 'body',
50
- });
51
- validate({
52
- data: input.query,
53
- schema: validation.query,
54
- target,
55
- localize,
56
- endpoint: input.endpoint,
57
- options,
58
- type: 'query',
59
- });
60
- validate({
61
- data: input.params,
62
- schema: validation.params,
63
- target,
64
- localize,
65
- endpoint: input.endpoint,
66
- options,
67
- type: 'params',
68
- });
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
+ });
69
75
  };
70
76
  const getConfig = (schema) => {
71
- const config = schema.meta?.config?.libs?.ajv;
72
- const options = config?.options ?? {};
73
- const localize = config?.localize ?? 'en';
74
- const target = config?.target;
75
- return { options, localize, target };
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 };
76
82
  };
77
83
  const validateOnClientAjv = (input, validation, schema) => {
78
- const { options, localize, target } = getConfig(schema);
79
- return validateAll({ input, validation, target, localize, options });
84
+ const { options, localize, target } = getConfig(schema);
85
+ return validateAll({ input, validation, target, localize, options });
80
86
  };
81
- const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) => (input, validation, schema) => {
87
+ const configure =
88
+ ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) =>
89
+ (input, validation, schema) => {
82
90
  const { options, localize, target } = getConfig(schema);
83
91
  validateAll({
84
- input,
85
- validation,
86
- target: givenTarget ?? target,
87
- localize: givenLocalize ?? localize,
88
- options: givenOptions ?? options,
92
+ input,
93
+ validation,
94
+ target: givenTarget ?? target,
95
+ localize: givenLocalize ?? localize,
96
+ options: givenOptions ?? options,
89
97
  });
90
- };
98
+ };
91
99
  exports.validateOnClient = Object.assign(validateOnClientAjv, {
92
- configure,
100
+ configure,
93
101
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.82",
3
+ "version": "0.0.0-draft.83",
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.323"
36
+ "vovk": "^3.0.0-draft.324"
37
37
  }
38
38
  }