vovk-ajv 0.0.0-draft.63 → 0.0.0-draft.65

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 +1 -1
  2. package/index.js +12 -2
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Options } from 'ajv';
2
- import { type VovkValidateOnClient } from 'vovk';
3
2
  import ajvLocalize from 'ajv-i18n';
3
+ import { type VovkValidateOnClient } from 'vovk';
4
4
  type Lang = keyof typeof ajvLocalize;
5
5
  export type VovkAjvConfig = {
6
6
  options?: Options;
package/index.js CHANGED
@@ -6,24 +6,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.validateOnClient = void 0;
7
7
  const ajv_1 = require("ajv");
8
8
  const _2020_1 = __importDefault(require("ajv/dist/2020"));
9
- const vovk_1 = require("vovk");
10
9
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
11
10
  const ajv_i18n_1 = __importDefault(require("ajv-i18n"));
12
11
  const ajv_errors_1 = __importDefault(require("ajv-errors"));
12
+ const vovk_1 = require("vovk");
13
13
  const createAjv = (options, target) => {
14
14
  const AjvClass = target === 'draft-2020-12' ? _2020_1.default : ajv_1.Ajv;
15
15
  const ajv = new AjvClass({ allErrors: true, ...options });
16
16
  (0, ajv_formats_1.default)(ajv);
17
17
  (0, ajv_errors_1.default)(ajv);
18
18
  ajv.addKeyword('x-isDto');
19
+ ajv.addKeyword('x-formData');
19
20
  return ajv;
20
21
  };
21
22
  const validate = ({ data, schema, ajv, localize = 'en', type, endpoint, }) => {
22
23
  if (data && schema) {
24
+ const isForm = data instanceof FormData && schema['x-formData'];
25
+ if (isForm) {
26
+ data = Object.fromEntries(data.entries().map(([key, value]) => {
27
+ if (value instanceof File) {
28
+ return [key, 'File<' + value.name + '>'];
29
+ }
30
+ return [key, value];
31
+ }));
32
+ }
23
33
  const isValid = ajv.validate(schema, data);
24
34
  if (!isValid) {
25
35
  ajv_i18n_1.default[localize](ajv.errors);
26
- throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${type} on client: ${ajv.errorsText()}`, { data, errors: ajv.errors, endpoint });
36
+ throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid ${isForm ? 'form' : type} on client: ${ajv.errorsText()}`, { data, errors: ajv.errors, endpoint });
27
37
  }
28
38
  }
29
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vovk-ajv",
3
- "version": "0.0.0-draft.63",
3
+ "version": "0.0.0-draft.65",
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.193"
36
+ "vovk": "^3.0.0-draft.240"
37
37
  }
38
38
  }