vovk-ajv 0.0.0-beta.6 → 0.0.0-beta.7
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 +2 -3
- package/index.js +20 -20
- package/package.json +9 -3
package/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Options } from 'ajv';
|
|
2
|
-
import ajvLocalize from 'ajv-i18n';
|
|
1
|
+
import { type Options } from 'ajv';
|
|
3
2
|
import { type VovkValidateOnClient } from 'vovk/createValidateOnClient';
|
|
4
|
-
type Lang =
|
|
3
|
+
type Lang = 'en' | 'ar' | 'ca' | 'cs' | 'de' | 'es' | 'fi' | 'fr' | 'hu' | 'id' | 'it' | 'ja' | 'ko' | 'nb' | 'nl' | 'pl' | 'pt-BR' | 'ru' | 'sk' | 'sv' | 'th' | 'zh' | 'zh-TW';
|
|
5
4
|
export type VovkAjvConfig = {
|
|
6
5
|
options?: Options;
|
|
7
6
|
localize?: Lang;
|
package/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import _Ajv2020 from 'ajv/dist/2020.js';
|
|
3
|
+
import _ajvFormats from 'ajv-formats';
|
|
4
|
+
import _ajvLocalize from 'ajv-i18n';
|
|
5
|
+
import _ajvErrors from 'ajv-errors';
|
|
6
|
+
import { createValidateOnClient, HttpException, HttpStatus, } from 'vovk/createValidateOnClient';
|
|
7
|
+
// Handle ESM/CJS interop - these packages export CJS and may have .default wrapper
|
|
8
|
+
const Ajv2020 = (_Ajv2020.default ?? _Ajv2020);
|
|
9
|
+
const ajvFormats = (_ajvFormats.default ?? _ajvFormats);
|
|
10
|
+
const ajvErrors = (_ajvErrors.default ?? _ajvErrors);
|
|
11
|
+
const ajvLocalize = (_ajvLocalize.default ??
|
|
12
|
+
_ajvLocalize);
|
|
13
13
|
const createAjv = (options, target) => {
|
|
14
|
-
const AjvClass = target === 'draft-2020-12' ?
|
|
14
|
+
const AjvClass = target === 'draft-2020-12' ? Ajv2020 : Ajv;
|
|
15
15
|
const ajv = new AjvClass({ allErrors: true, ...options });
|
|
16
|
-
(
|
|
17
|
-
(
|
|
16
|
+
ajvFormats(ajv);
|
|
17
|
+
ajvErrors(ajv);
|
|
18
18
|
ajv.addKeyword('x-isForm');
|
|
19
19
|
ajv.addKeyword('x-tsType');
|
|
20
20
|
return ajv;
|
|
@@ -60,8 +60,8 @@ const validate = ({ input, schema, localize = 'en', type, endpoint, options, tar
|
|
|
60
60
|
}
|
|
61
61
|
const isValid = ajv.validate(schema, input);
|
|
62
62
|
if (!isValid) {
|
|
63
|
-
|
|
64
|
-
throw new
|
|
63
|
+
ajvLocalize[localize](ajv.errors);
|
|
64
|
+
throw new HttpException(HttpStatus.NULL, `Client-side validation failed. Invalid ${isFormData ? 'form' : type} on client: ${ajv.errorsText()}`, { input, errors: ajv.errors, endpoint });
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
};
|
|
@@ -72,7 +72,7 @@ const getConfig = (schema) => {
|
|
|
72
72
|
const target = config?.target;
|
|
73
73
|
return { options, localize, target };
|
|
74
74
|
};
|
|
75
|
-
const validateOnClientAjv =
|
|
75
|
+
const validateOnClientAjv = createValidateOnClient({
|
|
76
76
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
77
77
|
const { options, localize, target } = getConfig(fullSchema);
|
|
78
78
|
validate({
|
|
@@ -86,7 +86,7 @@ const validateOnClientAjv = (0, createValidateOnClient_1.createValidateOnClient)
|
|
|
86
86
|
});
|
|
87
87
|
},
|
|
88
88
|
});
|
|
89
|
-
const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) =>
|
|
89
|
+
const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) => createValidateOnClient({
|
|
90
90
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
91
91
|
const { options, localize, target } = getConfig(fullSchema);
|
|
92
92
|
validate({
|
|
@@ -100,6 +100,6 @@ const configure = ({ options: givenOptions, localize: givenLocalize, target: giv
|
|
|
100
100
|
});
|
|
101
101
|
},
|
|
102
102
|
});
|
|
103
|
-
|
|
103
|
+
export const validateOnClient = Object.assign(validateOnClientAjv, {
|
|
104
104
|
configure,
|
|
105
105
|
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vovk-ajv",
|
|
3
|
-
"version": "0.0.0-beta.
|
|
3
|
+
"version": "0.0.0-beta.7",
|
|
4
4
|
"description": "Client-side JSON Schema validation library for Vovk.ts",
|
|
5
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"default": "./index.mjs",
|
|
9
|
+
"types": "./index.d.mts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
6
12
|
"scripts": {
|
|
7
13
|
"build": "tsc",
|
|
8
14
|
"tsc": "tsc --noEmit",
|
|
@@ -26,7 +32,7 @@
|
|
|
26
32
|
},
|
|
27
33
|
"homepage": "https://vovk.dev/imports",
|
|
28
34
|
"peerDependencies": {
|
|
29
|
-
"vovk": "^3.0.0-beta.
|
|
35
|
+
"vovk": "^3.0.0-beta.108"
|
|
30
36
|
},
|
|
31
37
|
"dependencies": {
|
|
32
38
|
"ajv": "^8.17.1",
|