vovk-ajv 0.0.0-draft.84 → 0.0.0-draft.87

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 (3) hide show
  1. package/index.d.ts +2 -4
  2. package/index.js +35 -50
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -7,9 +7,7 @@ export type VovkAjvConfig = {
7
7
  localize?: Lang;
8
8
  target?: 'draft-2020-12' | 'draft-07';
9
9
  };
10
- declare const validateOnClientAjv: VovkValidateOnClient;
11
- declare const configure: ({ options: givenOptions, localize: givenLocalize, target: givenTarget }: VovkAjvConfig) => VovkValidateOnClient;
12
- export declare const validateOnClient: typeof validateOnClientAjv & {
13
- configure: typeof configure;
10
+ export declare const validateOnClient: VovkValidateOnClient<unknown> & {
11
+ configure: ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }: VovkAjvConfig) => VovkValidateOnClient<unknown>;
14
12
  };
15
13
  export {};
package/index.js CHANGED
@@ -24,49 +24,20 @@ const createAjv = (options, target) => {
24
24
  });
25
25
  return ajv;
26
26
  };
27
- const validate = ({ data, schema, localize = 'en', type, endpoint, options, target, }) => {
28
- if (data && schema) {
29
- const schemaTarget = schema.$schema.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
27
+ const validate = ({ input, schema, localize = 'en', type, endpoint, options, target, }) => {
28
+ if (input && schema) {
29
+ const schemaTarget = schema.$schema?.includes('://json-schema.org/draft-07/schema') ? 'draft-07' : 'draft-2020-12';
30
30
  const ajv = createAjv(options ?? {}, target ?? schemaTarget);
31
- if (data instanceof FormData) {
32
- data = Object.fromEntries(data.entries());
31
+ if (input instanceof FormData) {
32
+ input = Object.fromEntries(input.entries());
33
33
  }
34
- const isValid = ajv.validate(schema, data);
34
+ const isValid = ajv.validate(schema, input);
35
35
  if (!isValid) {
36
36
  ajv_i18n_1.default[localize](ajv.errors);
37
- throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${data instanceof FormData ? 'form' : type} on client: ${ajv.errorsText()}`, { data, errors: ajv.errors, endpoint });
37
+ throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${input instanceof FormData ? 'form' : type} on client: ${ajv.errorsText()}`, { input, errors: ajv.errors, endpoint });
38
38
  }
39
39
  }
40
40
  };
41
- const validateAll = ({ input, validation, localize = 'en', target, options, }) => {
42
- validate({
43
- data: input.body,
44
- schema: validation.body,
45
- target,
46
- localize,
47
- endpoint: input.endpoint,
48
- options,
49
- type: 'body',
50
- });
51
- validate({
52
- data: input.query,
53
- schema: validation.query,
54
- target,
55
- localize,
56
- endpoint: input.endpoint,
57
- options,
58
- type: 'query',
59
- });
60
- validate({
61
- data: input.params,
62
- schema: validation.params,
63
- target,
64
- localize,
65
- endpoint: input.endpoint,
66
- options,
67
- type: 'params',
68
- });
69
- };
70
41
  const getConfig = (schema) => {
71
42
  const config = schema.meta?.config?.libs?.ajv;
72
43
  const options = config?.options ?? {};
@@ -74,20 +45,34 @@ const getConfig = (schema) => {
74
45
  const target = config?.target;
75
46
  return { options, localize, target };
76
47
  };
77
- const validateOnClientAjv = (input, validation, schema) => {
78
- const { options, localize, target } = getConfig(schema);
79
- return validateAll({ input, validation, target, localize, options });
80
- };
81
- const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget }) => (input, validation, schema) => {
82
- const { options, localize, target } = getConfig(schema);
83
- validateAll({
84
- input,
85
- validation,
86
- target: givenTarget ?? target,
87
- localize: givenLocalize ?? localize,
88
- options: givenOptions ?? options,
89
- });
90
- };
48
+ const validateOnClientAjv = (0, vovk_1.createValidateOnClient)({
49
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
50
+ const { options, localize, target } = getConfig(fullSchema);
51
+ validate({
52
+ input,
53
+ schema,
54
+ target,
55
+ localize,
56
+ endpoint,
57
+ options,
58
+ type,
59
+ });
60
+ },
61
+ });
62
+ const configure = ({ options: givenOptions, localize: givenLocalize, target: givenTarget, }) => (0, vovk_1.createValidateOnClient)({
63
+ validate: (input, schema, { endpoint, type, fullSchema }) => {
64
+ const { options, localize, target } = getConfig(fullSchema);
65
+ validate({
66
+ input,
67
+ schema,
68
+ target: givenTarget ?? target,
69
+ localize: givenLocalize ?? localize,
70
+ endpoint,
71
+ options: givenOptions ?? options,
72
+ type,
73
+ });
74
+ },
75
+ });
91
76
  exports.validateOnClient = Object.assign(validateOnClientAjv, {
92
77
  configure,
93
78
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.84",
3
+ "version": "0.0.0-draft.87",
4
4
  "description": "Local validation for JSON schemas for vovk-zod and other validation libraries for Vovk.ts",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -33,6 +33,6 @@
33
33
  "ajv-i18n": "^4.2.0"
34
34
  },
35
35
  "peerDependencies": {
36
- "vovk": "^3.0.0-draft.340"
36
+ "vovk": "^3.0.0-draft.352"
37
37
  }
38
38
  }