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.
Files changed (4) hide show
  1. package/README.md +42 -13
  2. package/index.d.ts +3 -7
  3. package/index.js +92 -97
  4. 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
- Back-end meta-framework for <a href="https://nextjs.org/docs/app">Next.js</a>
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
+ &nbsp;&nbsp;
16
+ <a href="https://vovk.dev/quick-install">Quick Start</a>
17
+ &nbsp;&nbsp;
18
+ <a href="https://vovk.dev/performance">Performance</a>
12
19
  </p>
13
20
 
14
21
  ---
15
22
 
16
- ## vovk-validate-client-ajv [![npm version](https://badge.fury.io/js/vovk-validate-client-ajv.svg)](https://www.npmjs.com/package/vovk-validate-client-ajv)
23
+ ## vovk-ajv [![npm version](https://badge.fury.io/js/vovk-ajv.svg)](https://www.npmjs.com/package/vovk-ajv)
17
24
 
18
- A library that is re-exported from [vovk-zod/validateOnClient](https://vovk.dev/validation/vovk-zod) and [vovk-yup/validateOnClient](https://vovk.dev/validation/vovk-yup) that provides client-side validation for JSON schemas emitted by these libraries. If you build a custom validation library that also emits JSON schemas, you can use this package to create your `validateOnClient` function.
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
- export { default } from 'vovk-validate-client-ajv';
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 ajvLocalize from 'ajv-i18n';
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, localize: givenLocalize, target: givenTarget, }: VovkAjvConfig) => VovkValidateOnClient<unknown>;
8
+ configure: ({ options: givenOptions, target: givenTarget }: VovkAjvConfig) => VovkValidateOnClient<unknown>;
12
9
  };
13
- export {};
package/index.js CHANGED
@@ -1,109 +1,104 @@
1
- 'use strict';
2
- var __importDefault =
3
- (this && this.__importDefault) ||
4
- function (mod) {
5
- return mod && mod.__esModule ? mod : { default: mod };
6
- };
7
- Object.defineProperty(exports, '__esModule', { value: true });
8
- exports.validateOnClient = void 0;
9
- const ajv_1 = require('ajv');
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
- const AjvClass = target === 'draft-2020-12' ? _2020_1.default : ajv_1.Ajv;
17
- const ajv = new AjvClass({ allErrors: true, ...options });
18
- (0, ajv_formats_1.default)(ajv);
19
- (0, ajv_errors_1.default)(ajv);
20
- ajv.addKeyword('x-isDto');
21
- ajv.addKeyword('x-isForm');
22
- ajv.addKeyword('x-tsType');
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, localize = 'en', type, endpoint, options, target }) => {
26
- if (input && schema) {
27
- const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
28
- const ajv = createAjv(options ?? {}, target ?? schemaTarget);
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
- // Handle duplicate keys
44
- if (key in result) {
45
- // If the key already exists
46
- if (Array.isArray(result[key])) {
47
- // If it's already an array, push to it
48
- result[key].push(processedValue);
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
- } else {
54
- // First occurrence of this key
55
- result[key] = processedValue;
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
- const config = schema.meta?.config?.libs?.ajv;
73
- const options = config?.options ?? {};
74
- const localize = config?.localize ?? 'en';
75
- const target = config?.target;
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 = (0, vovk_1.createValidateOnClient)({
79
- validate: (input, schema, { endpoint, type, fullSchema }) => {
80
- const { options, localize, target } = getConfig(fullSchema);
81
- validate({
82
- input,
83
- schema,
84
- target,
85
- localize,
86
- endpoint,
87
- options,
88
- type,
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, localize: givenLocalize, target: givenTarget }) =>
93
- (0, vovk_1.createValidateOnClient)({
89
+ const configure = ({ options: givenOptions, target: givenTarget }) => createValidateOnClient({
94
90
  validate: (input, schema, { endpoint, type, fullSchema }) => {
95
- const { options, localize, target } = getConfig(fullSchema);
96
- validate({
97
- input,
98
- schema,
99
- target: givenTarget ?? target,
100
- localize: givenLocalize ?? localize,
101
- endpoint,
102
- options: givenOptions ?? options,
103
- type,
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
- exports.validateOnClient = Object.assign(validateOnClientAjv, {
108
- configure,
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.0.0-draft.99",
4
- "description": "Local validation for JSON schemas for vovk-zod and other validation libraries for Vovk.ts",
5
- "main": "index.js",
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://github.com/finom/vovk#readme",
38
+ "homepage": "https://vovk.dev/imports",
29
39
  "peerDependencies": {
30
- "vovk": "^3.0.0-draft.405",
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
  }