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