vovk-ajv 0.0.0-draft.99 → 0.0.2
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/README.md +40 -13
- package/index.d.ts +3 -7
- package/index.js +30 -38
- package/package.json +19 -10
package/README.md
CHANGED
|
@@ -1,22 +1,49 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<picture>
|
|
3
|
-
<source width="300" media="(prefers-color-scheme: dark)" srcset="https://vovk.dev/vovk-logo-white.svg">
|
|
4
|
-
<source width="300" media="(prefers-color-scheme: light)" srcset="https://vovk.dev/vovk-logo.svg">
|
|
5
|
-
<img width="300" alt="vovk" src="https://vovk.dev/vovk-logo.svg">
|
|
6
|
-
</picture><br>
|
|
7
|
-
<strong>RESTful + RPC = ♥️</strong>
|
|
8
|
-
</p>
|
|
9
|
-
|
|
10
1
|
<p align="center">
|
|
11
|
-
|
|
2
|
+
<a href="https://vovk.dev">
|
|
3
|
+
<picture>
|
|
4
|
+
<source width="300" media="(prefers-color-scheme: dark)" srcset="https://vovk.dev/vovk-logo-white.svg">
|
|
5
|
+
<source width="300" media="(prefers-color-scheme: light)" srcset="https://vovk.dev/vovk-logo.svg">
|
|
6
|
+
<img width="300" alt="vovk" src="https://vovk.dev/vovk-logo.svg">
|
|
7
|
+
</picture>
|
|
8
|
+
</a>
|
|
9
|
+
<br>
|
|
10
|
+
<strong>Back-end Framework for Next.js App Router</strong>
|
|
11
|
+
<br />
|
|
12
|
+
<a href="https://vovk.dev/">Documentation</a>
|
|
13
|
+
|
|
14
|
+
<a href="https://vovk.dev/quick-install">Quick Start</a>
|
|
15
|
+
|
|
16
|
+
<a href="https://vovk.dev/performance">Performance</a>
|
|
12
17
|
</p>
|
|
13
18
|
|
|
14
19
|
---
|
|
15
20
|
|
|
16
|
-
## vovk-
|
|
21
|
+
## vovk-ajv [](https://www.npmjs.com/package/vovk-ajv)
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
[Ajv](https://ajv.js.org/) [client-side validation](https://vovk.dev/validation/client) for Vovk.ts. Exports `validateOnClient` function that can be injected into `createRPC` function at the generated RPC client by modifying [imports config](https://vovk.dev/imports#validateonclient).
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm install vovk-ajv
|
|
27
|
+
```
|
|
19
28
|
|
|
20
29
|
```ts
|
|
21
|
-
|
|
30
|
+
/** @type {import('vovk').VovkConfig} */
|
|
31
|
+
const config = {
|
|
32
|
+
outputConfig: {
|
|
33
|
+
imports: {
|
|
34
|
+
validateOnClient: 'vovk-ajv',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
libs: {
|
|
38
|
+
/** @type {import('vovk-ajv').VovkAjvConfig} */
|
|
39
|
+
ajv: {
|
|
40
|
+
options: {
|
|
41
|
+
strict: false,
|
|
42
|
+
},
|
|
43
|
+
localize: 'de',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default config;
|
|
22
49
|
```
|
package/index.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import { Options } from 'ajv';
|
|
2
|
-
import
|
|
3
|
-
import { type VovkValidateOnClient } from 'vovk';
|
|
4
|
-
type Lang = keyof typeof ajvLocalize;
|
|
1
|
+
import { type Options } from 'ajv';
|
|
2
|
+
import { type VovkValidateOnClient } from 'vovk/createValidateOnClient';
|
|
5
3
|
export type VovkAjvConfig = {
|
|
6
4
|
options?: Options;
|
|
7
|
-
localize?: Lang;
|
|
8
5
|
target?: 'draft-2020-12' | 'draft-07';
|
|
9
6
|
};
|
|
10
7
|
export declare const validateOnClient: VovkValidateOnClient<unknown> & {
|
|
11
|
-
configure: ({ options: givenOptions,
|
|
8
|
+
configure: ({ options: givenOptions, target: givenTarget }: VovkAjvConfig) => VovkValidateOnClient<unknown>;
|
|
12
9
|
};
|
|
13
|
-
export {};
|
package/index.js
CHANGED
|
@@ -1,33 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const _2020_1 = __importDefault(require('ajv/dist/2020'));
|
|
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'));
|
|
14
|
-
const vovk_1 = require('vovk');
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import _Ajv2020 from 'ajv/dist/2020.js';
|
|
3
|
+
import _ajvFormats from 'ajv-formats';
|
|
4
|
+
import _ajvErrors from 'ajv-errors';
|
|
5
|
+
import { createValidateOnClient, HttpException, HttpStatus } from 'vovk/createValidateOnClient';
|
|
6
|
+
// Handle ESM/CJS interop - these packages export CJS and may have .default wrapper
|
|
7
|
+
const Ajv2020 = _Ajv2020.default ?? _Ajv2020;
|
|
8
|
+
const ajvFormats = _ajvFormats.default ?? _ajvFormats;
|
|
9
|
+
const ajvErrors = _ajvErrors.default ?? _ajvErrors;
|
|
15
10
|
const createAjv = (options, target) => {
|
|
16
|
-
const AjvClass = target === 'draft-2020-12' ?
|
|
11
|
+
const AjvClass = target === 'draft-2020-12' ? Ajv2020 : Ajv;
|
|
17
12
|
const ajv = new AjvClass({ allErrors: true, ...options });
|
|
18
|
-
(
|
|
19
|
-
(
|
|
20
|
-
ajv.addKeyword('x-
|
|
21
|
-
ajv.addKeyword('x-isForm');
|
|
13
|
+
ajvFormats(ajv);
|
|
14
|
+
ajvErrors(ajv);
|
|
15
|
+
ajv.addKeyword('x-contentType');
|
|
22
16
|
ajv.addKeyword('x-tsType');
|
|
23
17
|
return ajv;
|
|
24
18
|
};
|
|
25
|
-
const validate = ({ input, schema,
|
|
19
|
+
const validate = ({ input, schema, type, endpoint, options, target }) => {
|
|
26
20
|
if (input && schema) {
|
|
21
|
+
if (input instanceof Blob) {
|
|
22
|
+
return; // skip validation for binary data
|
|
23
|
+
}
|
|
27
24
|
const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
|
|
28
25
|
const ajv = createAjv(options ?? {}, target ?? schemaTarget);
|
|
29
|
-
|
|
30
|
-
if (input instanceof FormData) {
|
|
26
|
+
if (input instanceof FormData || input instanceof URLSearchParams) {
|
|
31
27
|
const formDataEntries = Array.from(input.entries());
|
|
32
28
|
const result = {};
|
|
33
29
|
formDataEntries.forEach(([key, value]) => {
|
|
@@ -59,51 +55,47 @@ const validate = ({ input, schema, localize = 'en', type, endpoint, options, tar
|
|
|
59
55
|
}
|
|
60
56
|
const isValid = ajv.validate(schema, input);
|
|
61
57
|
if (!isValid) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
);
|
|
58
|
+
throw new HttpException(HttpStatus.NULL, `Client-side validation failed. Invalid ${type}: ${ajv.errorsText()}`, {
|
|
59
|
+
input,
|
|
60
|
+
errors: ajv.errors,
|
|
61
|
+
endpoint,
|
|
62
|
+
});
|
|
68
63
|
}
|
|
69
64
|
}
|
|
70
65
|
};
|
|
71
66
|
const getConfig = (schema) => {
|
|
72
67
|
const config = schema.meta?.config?.libs?.ajv;
|
|
73
68
|
const options = config?.options ?? {};
|
|
74
|
-
const localize = config?.localize ?? 'en';
|
|
75
69
|
const target = config?.target;
|
|
76
|
-
return { options,
|
|
70
|
+
return { options, target };
|
|
77
71
|
};
|
|
78
|
-
const validateOnClientAjv =
|
|
72
|
+
const validateOnClientAjv = createValidateOnClient({
|
|
79
73
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
80
|
-
const { options,
|
|
74
|
+
const { options, target } = getConfig(fullSchema);
|
|
81
75
|
validate({
|
|
82
76
|
input,
|
|
83
77
|
schema,
|
|
84
78
|
target,
|
|
85
|
-
localize,
|
|
86
79
|
endpoint,
|
|
87
80
|
options,
|
|
88
81
|
type,
|
|
89
82
|
});
|
|
90
83
|
},
|
|
91
84
|
});
|
|
92
|
-
const configure = ({ options: givenOptions,
|
|
93
|
-
|
|
85
|
+
const configure = ({ options: givenOptions, target: givenTarget }) =>
|
|
86
|
+
createValidateOnClient({
|
|
94
87
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
95
|
-
const { options,
|
|
88
|
+
const { options, target } = getConfig(fullSchema);
|
|
96
89
|
validate({
|
|
97
90
|
input,
|
|
98
91
|
schema,
|
|
99
92
|
target: givenTarget ?? target,
|
|
100
|
-
localize: givenLocalize ?? localize,
|
|
101
93
|
endpoint,
|
|
102
94
|
options: givenOptions ?? options,
|
|
103
95
|
type,
|
|
104
96
|
});
|
|
105
97
|
},
|
|
106
98
|
});
|
|
107
|
-
|
|
99
|
+
export const validateOnClient = Object.assign(validateOnClientAjv, {
|
|
108
100
|
configure,
|
|
109
101
|
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vovk-ajv",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Client-side JSON Schema validation library for Vovk.ts",
|
|
5
|
+
"files": [
|
|
6
|
+
"index.js",
|
|
7
|
+
"index.d.ts"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./index.js",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"default": "./index.js",
|
|
14
|
+
"types": "./index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
6
17
|
"scripts": {
|
|
7
18
|
"build": "tsc",
|
|
8
19
|
"tsc": "tsc --noEmit",
|
|
@@ -17,22 +28,20 @@
|
|
|
17
28
|
"vovk",
|
|
18
29
|
"json",
|
|
19
30
|
"ajv",
|
|
20
|
-
"validation"
|
|
21
|
-
"zod"
|
|
31
|
+
"validation"
|
|
22
32
|
],
|
|
23
33
|
"author": "Andrey Gubanov",
|
|
24
34
|
"license": "MIT",
|
|
25
35
|
"bugs": {
|
|
26
36
|
"url": "https://github.com/finom/vovk/issues"
|
|
27
37
|
},
|
|
28
|
-
"homepage": "https://
|
|
38
|
+
"homepage": "https://vovk.dev/imports",
|
|
29
39
|
"peerDependencies": {
|
|
30
|
-
"vovk": "
|
|
31
|
-
"ajv": "^8.17.1"
|
|
40
|
+
"vovk": ">=3.0.0"
|
|
32
41
|
},
|
|
33
42
|
"dependencies": {
|
|
43
|
+
"ajv": "^8.18.0",
|
|
34
44
|
"ajv-errors": "^3.0.0",
|
|
35
|
-
"ajv-formats": "^3.0.1"
|
|
36
|
-
"ajv-i18n": "^4.2.0"
|
|
45
|
+
"ajv-formats": "^3.0.1"
|
|
37
46
|
}
|
|
38
47
|
}
|