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 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
  &nbsp;&nbsp;
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/createValidateOnClient';
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/createValidateOnClient';
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
- 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;
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
- if (input && schema) {
21
- if (input instanceof Blob) {
22
- return; // skip validation for binary data
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
- // Handle duplicate keys
40
- if (key in result) {
41
- // If the key already exists
42
- if (Array.isArray(result[key])) {
43
- // If it's already an array, push to it
44
- result[key].push(processedValue);
45
- } else {
46
- // If it's not an array yet, convert it to an array with both values
47
- result[key] = [result[key], processedValue];
48
- }
49
- } else {
50
- // First occurrence of this key
51
- 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
+ });
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
- const config = schema.meta?.config?.libs?.ajv;
68
- const options = config?.options ?? {};
69
- const target = config?.target;
70
- return { options, target };
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
- validate: (input, schema, { endpoint, type, fullSchema }) => {
74
- const { options, target } = getConfig(fullSchema);
75
- validate({
76
- input,
77
- schema,
78
- target,
79
- endpoint,
80
- options,
81
- type,
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
- const { options, target } = getConfig(fullSchema);
89
- validate({
90
- input,
91
- schema,
92
- target: givenTarget ?? target,
93
- endpoint,
94
- options: givenOptions ?? options,
95
- type,
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
- configure,
103
+ configure,
101
104
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "Client-side JSON Schema validation library for Vovk.ts",
5
5
  "files": [
6
6
  "index.js",