vovk-ajv 0.0.0-draft.39 → 0.0.0-draft.41
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.d.ts +3 -3
- package/index.js +50 -42
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ import { type VovkValidateOnClient } from 'vovk';
|
|
|
3
3
|
import ajvLocalize from 'ajv-i18n';
|
|
4
4
|
type Lang = keyof typeof ajvLocalize;
|
|
5
5
|
export type VovkAjvConfig = {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
options?: Options;
|
|
7
|
+
localize?: Lang;
|
|
8
8
|
};
|
|
9
9
|
declare const validateOnClientAjv: VovkValidateOnClient;
|
|
10
10
|
declare const configure: ({ options: givenOptions, localize: givenLocalize }: VovkAjvConfig) => VovkValidateOnClient;
|
|
11
11
|
export declare const validateOnClient: typeof validateOnClientAjv & {
|
|
12
|
-
|
|
12
|
+
configure: typeof configure;
|
|
13
13
|
};
|
|
14
14
|
export {};
|
package/index.js
CHANGED
|
@@ -1,59 +1,67 @@
|
|
|
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 = __importDefault(require(
|
|
8
|
-
const vovk_1 = require(
|
|
9
|
-
const ajv_formats_1 = __importDefault(require(
|
|
10
|
-
const ajv_i18n_1 = __importDefault(require(
|
|
11
|
-
const ajv_errors_1 = __importDefault(require(
|
|
9
|
+
const ajv_1 = __importDefault(require('ajv'));
|
|
10
|
+
const vovk_1 = require('vovk');
|
|
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'));
|
|
12
14
|
const createAjv = (options) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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;
|
|
18
20
|
};
|
|
19
|
-
const validate = ({ data, schema, ajv, localize = 'en', type, endpoint
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
const validate = ({ data, schema, ajv, localize = 'en', type, endpoint }) => {
|
|
22
|
+
if (data && schema) {
|
|
23
|
+
const isValid = ajv.validate(schema, data);
|
|
24
|
+
if (!isValid) {
|
|
25
|
+
ajv_i18n_1.default[localize](ajv.errors);
|
|
26
|
+
throw new vovk_1.HttpException(
|
|
27
|
+
vovk_1.HttpStatus.NULL,
|
|
28
|
+
`Ajv validation failed. Invalid ${type} on client for ${endpoint}. ${ajv.errorsText()}`,
|
|
29
|
+
{ data, errors: ajv.errors }
|
|
30
|
+
);
|
|
26
31
|
}
|
|
32
|
+
}
|
|
27
33
|
};
|
|
28
|
-
const validateAll = ({ input, validation, ajv, localize = 'en'
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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' });
|
|
32
38
|
};
|
|
33
39
|
const getConfig = (fullSchema) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
const config = fullSchema.config.libs?.ajv;
|
|
41
|
+
const options = config?.options || {};
|
|
42
|
+
const localize = config?.localize || 'en';
|
|
43
|
+
return { options, localize };
|
|
38
44
|
};
|
|
39
45
|
let ajvScope = null;
|
|
40
46
|
const validateOnClientAjv = (input, validation, fullSchema) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
const { options, localize } = getConfig(fullSchema);
|
|
48
|
+
if (!ajvScope) {
|
|
49
|
+
ajvScope = createAjv(options);
|
|
50
|
+
}
|
|
51
|
+
return validateAll({ input, validation, ajv: ajvScope, localize });
|
|
46
52
|
};
|
|
47
|
-
const configure =
|
|
53
|
+
const configure =
|
|
54
|
+
({ options: givenOptions, localize: givenLocalize }) =>
|
|
55
|
+
(input, validation, fullSchema) => {
|
|
48
56
|
const { options, localize } = getConfig(fullSchema);
|
|
49
57
|
const ajv = createAjv({ ...options, ...givenOptions });
|
|
50
58
|
validateAll({
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
input,
|
|
60
|
+
validation,
|
|
61
|
+
ajv,
|
|
62
|
+
localize: givenLocalize || localize,
|
|
55
63
|
});
|
|
56
|
-
};
|
|
64
|
+
};
|
|
57
65
|
exports.validateOnClient = Object.assign(validateOnClientAjv, {
|
|
58
|
-
|
|
66
|
+
configure,
|
|
59
67
|
});
|
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.41",
|
|
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.116"
|
|
37
37
|
}
|
|
38
38
|
}
|