vovk-ajv 0.0.0-draft.92 → 0.0.0-draft.94
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.
- package/index.js +94 -91
- package/package.json +5 -3
package/index.js
CHANGED
|
@@ -1,106 +1,109 @@
|
|
|
1
|
-
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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(
|
|
8
|
-
const _2020_1 = __importDefault(require(
|
|
9
|
-
const ajv_formats_1 = __importDefault(require(
|
|
10
|
-
const ajv_i18n_1 = __importDefault(require(
|
|
11
|
-
const ajv_errors_1 = __importDefault(require(
|
|
12
|
-
const vovk_1 = require(
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
|
92
|
+
const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) =>
|
|
93
|
+
(0, vovk_1.createValidateOnClient)({
|
|
91
94
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.0.0-draft.94",
|
|
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,8 +27,10 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/finom/vovk#readme",
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"vovk": "^3.0.0-draft.
|
|
31
|
-
"ajv": "^8.17.1"
|
|
30
|
+
"vovk": "^3.0.0-draft.397",
|
|
31
|
+
"ajv": "^8.17.1"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
32
34
|
"ajv-errors": "^3.0.0",
|
|
33
35
|
"ajv-formats": "^3.0.1",
|
|
34
36
|
"ajv-i18n": "^4.2.0"
|