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