vovk-ajv 0.0.0-draft.93 → 0.0.0-draft.95

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 +94 -91
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,106 +1,109 @@
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-isForm');
20
- ajv.addKeyword('x-tsType');
21
- 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-isForm');
22
+ ajv.addKeyword('x-tsType');
23
+ return ajv;
22
24
  };
23
- const validate = ({ input, schema, localize = 'en', type, endpoint, options, target, }) => {
24
- if (input && schema) {
25
- const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
26
- const ajv = createAjv(options ?? {}, target ?? schemaTarget);
27
- const isFormData = input instanceof FormData;
28
- if (input instanceof FormData) {
29
- const formDataEntries = Array.from(input.entries());
30
- const result = {};
31
- formDataEntries.forEach(([key, value]) => {
32
- // Process the value (handle Blobs/Files)
33
- let processedValue;
34
- if (value instanceof Blob) {
35
- processedValue = '<binary>';
36
- }
37
- else if (Array.isArray(value)) {
38
- processedValue = value.map((item) => (item instanceof Blob ? '<binary>' : item));
39
- }
40
- else {
41
- processedValue = value;
42
- }
43
- // Handle duplicate keys
44
- if (key in result) {
45
- // If the key already exists
46
- if (Array.isArray(result[key])) {
47
- // If it's already an array, push to it
48
- result[key].push(processedValue);
49
- }
50
- else {
51
- // If it's not an array yet, convert it to an array with both values
52
- result[key] = [result[key], processedValue];
53
- }
54
- }
55
- else {
56
- // First occurrence of this key
57
- result[key] = processedValue;
58
- }
59
- });
60
- input = result;
25
+ const validate = ({ input, schema, localize = 'en', type, endpoint, options, target }) => {
26
+ if (input && schema) {
27
+ const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
28
+ const ajv = createAjv(options ?? {}, target ?? schemaTarget);
29
+ const isFormData = input instanceof FormData;
30
+ if (input instanceof FormData) {
31
+ const formDataEntries = Array.from(input.entries());
32
+ const result = {};
33
+ formDataEntries.forEach(([key, value]) => {
34
+ // Process the value (handle Blobs/Files)
35
+ let processedValue;
36
+ if (value instanceof Blob) {
37
+ processedValue = '<binary>';
38
+ } else if (Array.isArray(value)) {
39
+ processedValue = value.map((item) => (item instanceof Blob ? '<binary>' : item));
40
+ } else {
41
+ processedValue = value;
61
42
  }
62
- const isValid = ajv.validate(schema, input);
63
- if (!isValid) {
64
- ajv_i18n_1.default[localize](ajv.errors);
65
- throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${isFormData ? 'form' : type} on client: ${ajv.errorsText()}`, { input, errors: ajv.errors, endpoint });
43
+ // Handle duplicate keys
44
+ if (key in result) {
45
+ // If the key already exists
46
+ if (Array.isArray(result[key])) {
47
+ // If it's already an array, push to it
48
+ result[key].push(processedValue);
49
+ } else {
50
+ // If it's not an array yet, convert it to an array with both values
51
+ result[key] = [result[key], processedValue];
52
+ }
53
+ } else {
54
+ // First occurrence of this key
55
+ result[key] = processedValue;
66
56
  }
57
+ });
58
+ input = result;
59
+ }
60
+ const isValid = ajv.validate(schema, input);
61
+ if (!isValid) {
62
+ ajv_i18n_1.default[localize](ajv.errors);
63
+ throw new vovk_1.HttpException(
64
+ vovk_1.HttpStatus.NULL,
65
+ `Ajv validation failed. Invalid ${isFormData ? 'form' : type} on client: ${ajv.errorsText()}`,
66
+ { input, errors: ajv.errors, endpoint }
67
+ );
67
68
  }
69
+ }
68
70
  };
69
71
  const getConfig = (schema) => {
70
- const config = schema.meta?.config?.libs?.ajv;
71
- const options = config?.options ?? {};
72
- const localize = config?.localize ?? 'en';
73
- const target = config?.target;
74
- return { options, localize, target };
72
+ const config = schema.meta?.config?.libs?.ajv;
73
+ const options = config?.options ?? {};
74
+ const localize = config?.localize ?? 'en';
75
+ const target = config?.target;
76
+ return { options, localize, target };
75
77
  };
76
78
  const validateOnClientAjv = (0, vovk_1.createValidateOnClient)({
77
- validate: (input, schema, { endpoint, type, fullSchema }) => {
78
- const { options, localize, target } = getConfig(fullSchema);
79
- validate({
80
- input,
81
- schema,
82
- target,
83
- localize,
84
- endpoint,
85
- options,
86
- type,
87
- });
88
- },
79
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
80
+ const { options, localize, target } = getConfig(fullSchema);
81
+ validate({
82
+ input,
83
+ schema,
84
+ target,
85
+ localize,
86
+ endpoint,
87
+ options,
88
+ type,
89
+ });
90
+ },
89
91
  });
90
- const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) => (0, vovk_1.createValidateOnClient)({
92
+ const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) =>
93
+ (0, vovk_1.createValidateOnClient)({
91
94
  validate: (input, schema, { endpoint, type, fullSchema }) => {
92
- const { options, localize, target } = getConfig(fullSchema);
93
- validate({
94
- input,
95
- schema,
96
- target: givenTarget ?? target,
97
- localize: givenLocalize ?? localize,
98
- endpoint,
99
- options: givenOptions ?? options,
100
- type,
101
- });
95
+ const { options, localize, target } = getConfig(fullSchema);
96
+ validate({
97
+ input,
98
+ schema,
99
+ target: givenTarget ?? target,
100
+ localize: givenLocalize ?? localize,
101
+ endpoint,
102
+ options: givenOptions ?? options,
103
+ type,
104
+ });
102
105
  },
103
- });
106
+ });
104
107
  exports.validateOnClient = Object.assign(validateOnClientAjv, {
105
- configure,
108
+ configure,
106
109
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.93",
3
+ "version": "0.0.0-draft.95",
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": {
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/finom/vovk#readme",
29
29
  "peerDependencies": {
30
- "vovk": "^3.0.0-draft.396",
30
+ "vovk": "^3.0.0-draft.398",
31
31
  "ajv": "^8.17.1"
32
32
  },
33
33
  "dependencies": {