vovk-ajv 0.0.0-draft.29 → 0.0.0-draft.31
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 +27 -35
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -10,41 +10,32 @@ const ajv_1 = __importDefault(require('ajv'));
|
|
|
10
10
|
const vovk_1 = require('vovk');
|
|
11
11
|
const ajv_formats_1 = __importDefault(require('ajv-formats'));
|
|
12
12
|
const ajv_i18n_1 = __importDefault(require('ajv-i18n'));
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
if (validation.query) {
|
|
26
|
-
const isValid = ajv.validate(validation.query, input.query);
|
|
27
|
-
if (!isValid) {
|
|
28
|
-
ajv_i18n_1.default[localize](ajv.errors);
|
|
29
|
-
throw new vovk_1.HttpException(
|
|
30
|
-
vovk_1.HttpStatus.NULL,
|
|
31
|
-
`Ajv validation failed. Invalid request query on client for ${input.endpoint}. ${ajv.errorsText()}`,
|
|
32
|
-
{ query: input.query, endpoint: input.endpoint, errors: ajv.errors }
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (validation.params) {
|
|
37
|
-
const isValid = ajv.validate(validation.params, input.params);
|
|
13
|
+
const ajv_errors_1 = __importDefault(require('ajv-errors'));
|
|
14
|
+
const createAjv = (options) => {
|
|
15
|
+
const ajv = new ajv_1.default({ allErrors: true, ...options });
|
|
16
|
+
(0, ajv_formats_1.default)(ajv);
|
|
17
|
+
(0, ajv_errors_1.default)(ajv);
|
|
18
|
+
ajv.addKeyword('x-isDto');
|
|
19
|
+
return ajv;
|
|
20
|
+
};
|
|
21
|
+
const validate = ({ data, schema, ajv, localize = 'en', type, endpoint }) => {
|
|
22
|
+
if (data && schema) {
|
|
23
|
+
const isValid = ajv.validate(schema, data);
|
|
38
24
|
if (!isValid) {
|
|
39
25
|
ajv_i18n_1.default[localize](ajv.errors);
|
|
40
26
|
throw new vovk_1.HttpException(
|
|
41
27
|
vovk_1.HttpStatus.NULL,
|
|
42
|
-
`Ajv validation failed. Invalid
|
|
43
|
-
{
|
|
28
|
+
`Ajv validation failed. Invalid ${type} on client for ${endpoint}. ${ajv.errorsText()}`,
|
|
29
|
+
{ data, errors: ajv.errors }
|
|
44
30
|
);
|
|
45
31
|
}
|
|
46
32
|
}
|
|
47
33
|
};
|
|
34
|
+
const validateAll = ({ input, validation, ajv, localize = 'en' }) => {
|
|
35
|
+
validate({ data: input.body, schema: validation.body, ajv, localize, endpoint: input.endpoint, type: 'body' });
|
|
36
|
+
validate({ data: input.query, schema: validation.query, ajv, localize, endpoint: input.endpoint, type: 'query' });
|
|
37
|
+
validate({ data: input.params, schema: validation.params, ajv, localize, endpoint: input.endpoint, type: 'params' });
|
|
38
|
+
};
|
|
48
39
|
const getConfig = (fullSchema) => {
|
|
49
40
|
const config = fullSchema.config.custom?.ajv;
|
|
50
41
|
const options = config?.options || {};
|
|
@@ -55,20 +46,21 @@ let ajvScope = null;
|
|
|
55
46
|
const validateOnClientAjv = (input, validation, fullSchema) => {
|
|
56
47
|
const { options, localize } = getConfig(fullSchema);
|
|
57
48
|
if (!ajvScope) {
|
|
58
|
-
ajvScope =
|
|
59
|
-
(0, ajv_formats_1.default)(ajvScope);
|
|
60
|
-
ajvScope.addKeyword('x-isDto');
|
|
49
|
+
ajvScope = createAjv(options);
|
|
61
50
|
}
|
|
62
|
-
return
|
|
51
|
+
return validateAll({ input, validation, ajv: ajvScope, localize });
|
|
63
52
|
};
|
|
64
53
|
const configure =
|
|
65
54
|
({ options: givenOptions, localize: givenLocalize }) =>
|
|
66
55
|
(input, validation, fullSchema) => {
|
|
67
56
|
const { options, localize } = getConfig(fullSchema);
|
|
68
|
-
const ajv =
|
|
69
|
-
(
|
|
70
|
-
|
|
71
|
-
|
|
57
|
+
const ajv = createAjv({ ...options, ...givenOptions });
|
|
58
|
+
validateAll({
|
|
59
|
+
input,
|
|
60
|
+
validation,
|
|
61
|
+
ajv,
|
|
62
|
+
localize: givenLocalize || localize,
|
|
63
|
+
});
|
|
72
64
|
};
|
|
73
65
|
exports.validateOnClient = Object.assign(validateOnClientAjv, {
|
|
74
66
|
configure,
|
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.31",
|
|
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": {
|
|
@@ -28,10 +28,11 @@
|
|
|
28
28
|
"homepage": "https://github.com/finom/vovk#readme",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"ajv": "^8.17.1",
|
|
31
|
+
"ajv-errors": "^3.0.0",
|
|
31
32
|
"ajv-formats": "^3.0.1",
|
|
32
33
|
"ajv-i18n": "^4.2.0"
|
|
33
34
|
},
|
|
34
35
|
"peerDependencies": {
|
|
35
|
-
"vovk": "^3.0.0-draft.
|
|
36
|
+
"vovk": "^3.0.0-draft.96"
|
|
36
37
|
}
|
|
37
38
|
}
|