vovk-ajv 0.0.0-draft.72 → 0.0.0-draft.75
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 +41 -17
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -11,7 +11,6 @@ const ajv_i18n_1 = __importDefault(require("ajv-i18n"));
|
|
|
11
11
|
const ajv_errors_1 = __importDefault(require("ajv-errors"));
|
|
12
12
|
const vovk_1 = require("vovk");
|
|
13
13
|
const createAjv = (options, target) => {
|
|
14
|
-
// TODO auto-detect by https://json-schema.org/draft-07/schema, https://json-schema.org/draft/2020-12/schema
|
|
15
14
|
const AjvClass = target === 'draft-2020-12' ? _2020_1.default : ajv_1.Ajv;
|
|
16
15
|
const ajv = new AjvClass({ allErrors: true, ...options });
|
|
17
16
|
(0, ajv_formats_1.default)(ajv);
|
|
@@ -19,49 +18,74 @@ const createAjv = (options, target) => {
|
|
|
19
18
|
ajv.addKeyword('x-isDto');
|
|
20
19
|
ajv.addKeyword('x-formData');
|
|
21
20
|
ajv.addKeyword('x-tsType');
|
|
21
|
+
ajv.addFormat('binary', {
|
|
22
|
+
type: 'string',
|
|
23
|
+
validate: (data) => data instanceof File || data instanceof Blob,
|
|
24
|
+
});
|
|
22
25
|
return ajv;
|
|
23
26
|
};
|
|
24
|
-
const validate = ({ data, schema,
|
|
27
|
+
const validate = ({ data, schema, localize = 'en', type, endpoint, options, target, }) => {
|
|
25
28
|
if (data && schema) {
|
|
26
|
-
const
|
|
27
|
-
|
|
29
|
+
const schemaTarget = schema.$schema === 'https://json-schema.org/draft-07/schema#' ? 'draft-07' : 'draft-2020-12';
|
|
30
|
+
const ajv = createAjv(options ?? {}, target ?? schemaTarget);
|
|
31
|
+
if (data instanceof FormData) {
|
|
28
32
|
data = Object.fromEntries(data.entries());
|
|
29
33
|
}
|
|
30
34
|
const isValid = ajv.validate(schema, data);
|
|
31
35
|
if (!isValid) {
|
|
32
36
|
ajv_i18n_1.default[localize](ajv.errors);
|
|
33
|
-
throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${
|
|
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 });
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
};
|
|
37
|
-
const validateAll = ({ input, validation,
|
|
38
|
-
validate({
|
|
39
|
-
|
|
40
|
-
|
|
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
|
+
});
|
|
41
69
|
};
|
|
42
70
|
const getConfig = (schema) => {
|
|
43
71
|
const config = schema.meta?.config?.libs?.ajv;
|
|
44
72
|
const options = config?.options ?? {};
|
|
45
73
|
const localize = config?.localize ?? 'en';
|
|
46
|
-
const target = config?.target
|
|
74
|
+
const target = config?.target;
|
|
47
75
|
return { options, localize, target };
|
|
48
76
|
};
|
|
49
|
-
let ajvScope = null;
|
|
50
77
|
const validateOnClientAjv = (input, validation, schema) => {
|
|
51
78
|
const { options, localize, target } = getConfig(schema);
|
|
52
|
-
|
|
53
|
-
ajvScope = createAjv(options, target);
|
|
54
|
-
}
|
|
55
|
-
return validateAll({ input, validation, ajv: ajvScope, localize });
|
|
79
|
+
return validateAll({ input, validation, target, localize, options });
|
|
56
80
|
};
|
|
57
81
|
const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) => (input, validation, schema) => {
|
|
58
82
|
const { options, localize, target } = getConfig(schema);
|
|
59
|
-
const ajv = createAjv({ ...options, ...givenOptions }, givenTarget ?? target);
|
|
60
83
|
validateAll({
|
|
61
84
|
input,
|
|
62
85
|
validation,
|
|
63
|
-
|
|
86
|
+
target: givenTarget ?? target,
|
|
64
87
|
localize: givenLocalize ?? localize,
|
|
88
|
+
options: givenOptions ?? options,
|
|
65
89
|
});
|
|
66
90
|
};
|
|
67
91
|
exports.validateOnClient = Object.assign(validateOnClientAjv, {
|
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.75",
|
|
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.
|
|
36
|
+
"vovk": "^3.0.0-draft.303"
|
|
37
37
|
}
|
|
38
38
|
}
|