vovk-ajv 0.0.0-draft.99 → 0.1.0
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 +42 -13
- package/index.d.ts +3 -7
- package/index.js +92 -97
- package/package.json +19 -10
package/README.md
CHANGED
|
@@ -1,22 +1,51 @@
|
|
|
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
|
+
<em>One codebase → type-safe clients, OpenAPI, and AI tools</em>
|
|
13
|
+
<br />
|
|
14
|
+
<a href="https://vovk.dev/">Documentation</a>
|
|
15
|
+
|
|
16
|
+
<a href="https://vovk.dev/quick-install">Quick Start</a>
|
|
17
|
+
|
|
18
|
+
<a href="https://vovk.dev/performance">Performance</a>
|
|
12
19
|
</p>
|
|
13
20
|
|
|
14
21
|
---
|
|
15
22
|
|
|
16
|
-
## vovk-
|
|
23
|
+
## vovk-ajv [](https://www.npmjs.com/package/vovk-ajv)
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
[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).
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install vovk-ajv
|
|
29
|
+
```
|
|
19
30
|
|
|
20
31
|
```ts
|
|
21
|
-
|
|
32
|
+
/** @type {import('vovk').VovkConfig} */
|
|
33
|
+
const config = {
|
|
34
|
+
outputConfig: {
|
|
35
|
+
imports: {
|
|
36
|
+
validateOnClient: 'vovk-ajv',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
libs: {
|
|
40
|
+
/** @type {import('vovk-ajv').VovkAjvConfig} */
|
|
41
|
+
ajv: {
|
|
42
|
+
options: {
|
|
43
|
+
strict: false,
|
|
44
|
+
},
|
|
45
|
+
localize: 'de',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export default config;
|
|
22
51
|
```
|
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/create-validate-on-client';
|
|
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,109 +1,104 @@
|
|
|
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/create-validate-on-client';
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return ajv;
|
|
11
|
+
const AjvClass = target === 'draft-2020-12' ? Ajv2020 : Ajv;
|
|
12
|
+
const ajv = new AjvClass({ allErrors: true, ...options });
|
|
13
|
+
ajvFormats(ajv);
|
|
14
|
+
ajvErrors(ajv);
|
|
15
|
+
ajv.addKeyword('x-contentType');
|
|
16
|
+
ajv.addKeyword('x-tsType');
|
|
17
|
+
return ajv;
|
|
24
18
|
};
|
|
25
|
-
const validate = ({ input, schema,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const isFormData = input instanceof FormData;
|
|
30
|
-
if (input instanceof FormData) {
|
|
31
|
-
const formDataEntries = Array.from(input.entries());
|
|
32
|
-
const result = {};
|
|
33
|
-
formDataEntries.forEach(([key, value]) => {
|
|
34
|
-
// Process the value (handle Blobs/Files)
|
|
35
|
-
let processedValue;
|
|
36
|
-
if (value instanceof Blob) {
|
|
37
|
-
processedValue = '<binary>';
|
|
38
|
-
} else if (Array.isArray(value)) {
|
|
39
|
-
processedValue = value.map((item) => (item instanceof Blob ? '<binary>' : item));
|
|
40
|
-
} else {
|
|
41
|
-
processedValue = value;
|
|
19
|
+
const validate = ({ input, schema, type, endpoint, options, target, }) => {
|
|
20
|
+
if (input && schema) {
|
|
21
|
+
if (input instanceof Blob) {
|
|
22
|
+
return; // skip validation for binary data
|
|
42
23
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
24
|
+
const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
|
|
25
|
+
const ajv = createAjv(options ?? {}, target ?? schemaTarget);
|
|
26
|
+
if (input instanceof FormData || input instanceof URLSearchParams) {
|
|
27
|
+
const formDataEntries = Array.from(input.entries());
|
|
28
|
+
const result = {};
|
|
29
|
+
formDataEntries.forEach(([key, value]) => {
|
|
30
|
+
// Process the value (handle Blobs/Files)
|
|
31
|
+
let processedValue;
|
|
32
|
+
if (value instanceof Blob) {
|
|
33
|
+
processedValue = '<binary>';
|
|
34
|
+
}
|
|
35
|
+
else if (Array.isArray(value)) {
|
|
36
|
+
processedValue = value.map((item) => (item instanceof Blob ? '<binary>' : item));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
processedValue = value;
|
|
40
|
+
}
|
|
41
|
+
// Handle duplicate keys
|
|
42
|
+
if (key in result) {
|
|
43
|
+
// If the key already exists
|
|
44
|
+
if (Array.isArray(result[key])) {
|
|
45
|
+
// If it's already an array, push to it
|
|
46
|
+
result[key].push(processedValue);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// If it's not an array yet, convert it to an array with both values
|
|
50
|
+
result[key] = [result[key], processedValue];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// First occurrence of this key
|
|
55
|
+
result[key] = processedValue;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
input = result;
|
|
59
|
+
}
|
|
60
|
+
const isValid = ajv.validate(schema, input);
|
|
61
|
+
if (!isValid) {
|
|
62
|
+
throw new HttpException(HttpStatus.NULL, `Client-side validation failed. Invalid ${type}: ${ajv.errorsText()}`, {
|
|
63
|
+
input,
|
|
64
|
+
errors: ajv.errors,
|
|
65
|
+
endpoint,
|
|
66
|
+
});
|
|
56
67
|
}
|
|
57
|
-
});
|
|
58
|
-
input = result;
|
|
59
|
-
}
|
|
60
|
-
const isValid = ajv.validate(schema, input);
|
|
61
|
-
if (!isValid) {
|
|
62
|
-
ajv_i18n_1.default[localize](ajv.errors);
|
|
63
|
-
throw new vovk_1.HttpException(
|
|
64
|
-
vovk_1.HttpStatus.NULL,
|
|
65
|
-
`Ajv validation failed. Invalid ${isFormData ? 'form' : type} on client: ${ajv.errorsText()}`,
|
|
66
|
-
{ input, errors: ajv.errors, endpoint }
|
|
67
|
-
);
|
|
68
68
|
}
|
|
69
|
-
}
|
|
70
69
|
};
|
|
71
70
|
const getConfig = (schema) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return { options, localize, target };
|
|
71
|
+
const config = schema.meta?.config?.libs?.ajv;
|
|
72
|
+
const options = config?.options ?? {};
|
|
73
|
+
const target = config?.target;
|
|
74
|
+
return { options, target };
|
|
77
75
|
};
|
|
78
|
-
const validateOnClientAjv =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
},
|
|
76
|
+
const validateOnClientAjv = createValidateOnClient({
|
|
77
|
+
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
78
|
+
const { options, target } = getConfig(fullSchema);
|
|
79
|
+
validate({
|
|
80
|
+
input,
|
|
81
|
+
schema,
|
|
82
|
+
target,
|
|
83
|
+
endpoint,
|
|
84
|
+
options,
|
|
85
|
+
type,
|
|
86
|
+
});
|
|
87
|
+
},
|
|
91
88
|
});
|
|
92
|
-
const configure = ({ options: givenOptions,
|
|
93
|
-
(0, vovk_1.createValidateOnClient)({
|
|
89
|
+
const configure = ({ options: givenOptions, target: givenTarget }) => createValidateOnClient({
|
|
94
90
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
91
|
+
const { options, target } = getConfig(fullSchema);
|
|
92
|
+
validate({
|
|
93
|
+
input,
|
|
94
|
+
schema,
|
|
95
|
+
target: givenTarget ?? target,
|
|
96
|
+
endpoint,
|
|
97
|
+
options: givenOptions ?? options,
|
|
98
|
+
type,
|
|
99
|
+
});
|
|
105
100
|
},
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
});
|
|
102
|
+
export const validateOnClient = Object.assign(validateOnClientAjv, {
|
|
103
|
+
configure,
|
|
109
104
|
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vovk-ajv",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
}
|