vovk-ajv 0.0.0-draft.89 → 0.0.0-draft.90

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 +93 -72
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,85 +1,106 @@
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
+ return ajv;
28
22
  };
29
- const validate = ({ input, schema, localize = 'en', type, endpoint, options, target }) => {
30
- if (input && 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 (input instanceof FormData) {
34
- input = Object.fromEntries(input.entries());
35
- }
36
- const isValid = ajv.validate(schema, input);
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 ${input instanceof FormData ? 'form' : type} on client: ${ajv.errorsText()}`,
42
- { input, errors: ajv.errors, endpoint }
43
- );
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;
61
+ }
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 });
66
+ }
44
67
  }
45
- }
46
68
  };
47
69
  const getConfig = (schema) => {
48
- const config = schema.meta?.config?.libs?.ajv;
49
- const options = config?.options ?? {};
50
- const localize = config?.localize ?? 'en';
51
- const target = config?.target;
52
- return { options, localize, target };
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 };
53
75
  };
54
76
  const validateOnClientAjv = (0, vovk_1.createValidateOnClient)({
55
- validate: (input, schema, { endpoint, type, fullSchema }) => {
56
- const { options, localize, target } = getConfig(fullSchema);
57
- validate({
58
- input,
59
- schema,
60
- target,
61
- localize,
62
- endpoint,
63
- options,
64
- type,
65
- });
66
- },
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
+ },
67
89
  });
68
- const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) =>
69
- (0, vovk_1.createValidateOnClient)({
90
+ const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) => (0, vovk_1.createValidateOnClient)({
70
91
  validate: (input, schema, { endpoint, type, fullSchema }) => {
71
- const { options, localize, target } = getConfig(fullSchema);
72
- validate({
73
- input,
74
- schema,
75
- target: givenTarget ?? target,
76
- localize: givenLocalize ?? localize,
77
- endpoint,
78
- options: givenOptions ?? options,
79
- type,
80
- });
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
+ });
81
102
  },
82
- });
103
+ });
83
104
  exports.validateOnClient = Object.assign(validateOnClientAjv, {
84
- configure,
105
+ configure,
85
106
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.89",
3
+ "version": "0.0.0-draft.90",
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.362"
36
+ "vovk": "^3.0.0-draft.366"
37
37
  }
38
38
  }