vovk-ajv 0.0.2 → 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 +2 -0
- package/index.d.ts +1 -1
- package/index.js +82 -79
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
<br>
|
|
10
10
|
<strong>Back-end Framework for Next.js App Router</strong>
|
|
11
11
|
<br />
|
|
12
|
+
<em>One codebase → type-safe clients, OpenAPI, and AI tools</em>
|
|
13
|
+
<br />
|
|
12
14
|
<a href="https://vovk.dev/">Documentation</a>
|
|
13
15
|
|
|
14
16
|
<a href="https://vovk.dev/quick-install">Quick Start</a>
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Options } from 'ajv';
|
|
2
|
-
import { type VovkValidateOnClient } from 'vovk/
|
|
2
|
+
import { type VovkValidateOnClient } from 'vovk/create-validate-on-client';
|
|
3
3
|
export type VovkAjvConfig = {
|
|
4
4
|
options?: Options;
|
|
5
5
|
target?: 'draft-2020-12' | 'draft-07';
|
package/index.js
CHANGED
|
@@ -2,100 +2,103 @@ import { Ajv } from 'ajv';
|
|
|
2
2
|
import _Ajv2020 from 'ajv/dist/2020.js';
|
|
3
3
|
import _ajvFormats from 'ajv-formats';
|
|
4
4
|
import _ajvErrors from 'ajv-errors';
|
|
5
|
-
import { createValidateOnClient, HttpException, HttpStatus } from 'vovk/
|
|
5
|
+
import { createValidateOnClient, HttpException, HttpStatus, } from 'vovk/create-validate-on-client';
|
|
6
6
|
// Handle ESM/CJS interop - these packages export CJS and may have .default wrapper
|
|
7
7
|
const Ajv2020 = _Ajv2020.default ?? _Ajv2020;
|
|
8
8
|
const ajvFormats = _ajvFormats.default ?? _ajvFormats;
|
|
9
9
|
const ajvErrors = _ajvErrors.default ?? _ajvErrors;
|
|
10
10
|
const createAjv = (options, target) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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;
|
|
18
18
|
};
|
|
19
|
-
const validate = ({ input, schema, type, endpoint, options, target }) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
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
|
-
} else if (Array.isArray(value)) {
|
|
35
|
-
processedValue = value.map((item) => (item instanceof Blob ? '<binary>' : item));
|
|
36
|
-
} else {
|
|
37
|
-
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
|
|
38
23
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
+
});
|
|
52
67
|
}
|
|
53
|
-
});
|
|
54
|
-
input = result;
|
|
55
|
-
}
|
|
56
|
-
const isValid = ajv.validate(schema, input);
|
|
57
|
-
if (!isValid) {
|
|
58
|
-
throw new HttpException(HttpStatus.NULL, `Client-side validation failed. Invalid ${type}: ${ajv.errorsText()}`, {
|
|
59
|
-
input,
|
|
60
|
-
errors: ajv.errors,
|
|
61
|
-
endpoint,
|
|
62
|
-
});
|
|
63
68
|
}
|
|
64
|
-
}
|
|
65
69
|
};
|
|
66
70
|
const getConfig = (schema) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
+
const config = schema.meta?.config?.libs?.ajv;
|
|
72
|
+
const options = config?.options ?? {};
|
|
73
|
+
const target = config?.target;
|
|
74
|
+
return { options, target };
|
|
71
75
|
};
|
|
72
76
|
const validateOnClientAjv = createValidateOnClient({
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
+
},
|
|
84
88
|
});
|
|
85
|
-
const configure = ({ options: givenOptions, target: givenTarget }) =>
|
|
86
|
-
createValidateOnClient({
|
|
89
|
+
const configure = ({ options: givenOptions, target: givenTarget }) => createValidateOnClient({
|
|
87
90
|
validate: (input, schema, { endpoint, type, fullSchema }) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
+
});
|
|
97
100
|
},
|
|
98
|
-
|
|
101
|
+
});
|
|
99
102
|
export const validateOnClient = Object.assign(validateOnClientAjv, {
|
|
100
|
-
|
|
103
|
+
configure,
|
|
101
104
|
});
|