vovk-ajv 0.0.0-beta.10

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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Andrey Gubanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ <p align="center">
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
+ &nbsp;&nbsp;
14
+ <a href="https://vovk.dev/quick-install">Quick Start</a>
15
+ &nbsp;&nbsp;
16
+ <a href="https://vovk.dev/performance">Performance</a>
17
+ </p>
18
+
19
+ ---
20
+
21
+ ## vovk-ajv [![npm version](https://badge.fury.io/js/vovk-ajv.svg)](https://www.npmjs.com/package/vovk-ajv)
22
+
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
+ ```
28
+
29
+ ```ts
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;
49
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { type Options } from 'ajv';
2
+ import { type VovkValidateOnClient } from 'vovk/createValidateOnClient';
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';
4
+ export type VovkAjvConfig = {
5
+ options?: Options;
6
+ localize?: Lang;
7
+ target?: 'draft-2020-12' | 'draft-07';
8
+ };
9
+ export declare const validateOnClient: VovkValidateOnClient<unknown> & {
10
+ configure: ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }: VovkAjvConfig) => VovkValidateOnClient<unknown>;
11
+ };
12
+ export {};
package/index.js ADDED
@@ -0,0 +1,105 @@
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
+ const createAjv = (options, target) => {
14
+ const AjvClass = target === 'draft-2020-12' ? Ajv2020 : Ajv;
15
+ const ajv = new AjvClass({ allErrors: true, ...options });
16
+ ajvFormats(ajv);
17
+ ajvErrors(ajv);
18
+ ajv.addKeyword('x-isForm');
19
+ ajv.addKeyword('x-tsType');
20
+ return ajv;
21
+ };
22
+ const validate = ({ input, schema, localize = 'en', type, endpoint, options, target, }) => {
23
+ if (input && schema) {
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
+ const isFormData = input instanceof FormData;
27
+ if (input instanceof FormData) {
28
+ const formDataEntries = Array.from(input.entries());
29
+ const result = {};
30
+ formDataEntries.forEach(([key, value]) => {
31
+ // Process the value (handle Blobs/Files)
32
+ let processedValue;
33
+ if (value instanceof Blob) {
34
+ processedValue = '<binary>';
35
+ }
36
+ else if (Array.isArray(value)) {
37
+ processedValue = value.map((item) => (item instanceof Blob ? '<binary>' : item));
38
+ }
39
+ else {
40
+ processedValue = value;
41
+ }
42
+ // Handle duplicate keys
43
+ if (key in result) {
44
+ // If the key already exists
45
+ if (Array.isArray(result[key])) {
46
+ // If it's already an array, push to it
47
+ result[key].push(processedValue);
48
+ }
49
+ else {
50
+ // If it's not an array yet, convert it to an array with both values
51
+ result[key] = [result[key], processedValue];
52
+ }
53
+ }
54
+ else {
55
+ // First occurrence of this key
56
+ result[key] = processedValue;
57
+ }
58
+ });
59
+ input = result;
60
+ }
61
+ const isValid = ajv.validate(schema, input);
62
+ if (!isValid) {
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
+ }
66
+ }
67
+ };
68
+ const getConfig = (schema) => {
69
+ const config = schema.meta?.config?.libs?.ajv;
70
+ const options = config?.options ?? {};
71
+ const localize = config?.localize ?? 'en';
72
+ const target = config?.target;
73
+ return { options, localize, target };
74
+ };
75
+ const validateOnClientAjv = createValidateOnClient({
76
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
77
+ const { options, localize, target } = getConfig(fullSchema);
78
+ validate({
79
+ input,
80
+ schema,
81
+ target,
82
+ localize,
83
+ endpoint,
84
+ options,
85
+ type,
86
+ });
87
+ },
88
+ });
89
+ const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) => createValidateOnClient({
90
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
91
+ const { options, localize, target } = getConfig(fullSchema);
92
+ validate({
93
+ input,
94
+ schema,
95
+ target: givenTarget ?? target,
96
+ localize: givenLocalize ?? localize,
97
+ endpoint,
98
+ options: givenOptions ?? options,
99
+ type,
100
+ });
101
+ },
102
+ });
103
+ export const validateOnClient = Object.assign(validateOnClientAjv, {
104
+ configure,
105
+ });
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "vovk-ajv",
3
+ "version": "0.0.0-beta.10",
4
+ "description": "Client-side JSON Schema validation library for Vovk.ts",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "default": "./index.js",
9
+ "types": "./index.d.ts"
10
+ }
11
+ },
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "tsc": "tsc --noEmit",
15
+ "npm-publish": "npm publish",
16
+ "ncu": "npm-check-updates -u"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/finom/vovk.git"
21
+ },
22
+ "keywords": [
23
+ "vovk",
24
+ "json",
25
+ "ajv",
26
+ "validation"
27
+ ],
28
+ "author": "Andrey Gubanov",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/finom/vovk/issues"
32
+ },
33
+ "homepage": "https://vovk.dev/imports",
34
+ "peerDependencies": {
35
+ "vovk": "^3.0.0-beta.111"
36
+ },
37
+ "dependencies": {
38
+ "ajv": "^8.17.1",
39
+ "ajv-errors": "^3.0.0",
40
+ "ajv-formats": "^3.0.1",
41
+ "ajv-i18n": "^4.2.0"
42
+ }
43
+ }