vovk-ajv 0.0.0-draft.27

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,22 @@
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>REST + RPC = ♥️</strong>
8
+ </p>
9
+
10
+ <p align="center">
11
+ Back-end meta-framework for <a href="https://nextjs.org/docs/app">Next.js</a>
12
+ </p>
13
+
14
+ ---
15
+
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)
17
+
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.
19
+
20
+ ```ts
21
+ export { default } from 'vovk-validate-client-ajv';
22
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Options } from 'ajv';
2
+ import { type VovkValidateOnClient } from 'vovk';
3
+ import ajvLocalize from 'ajv-i18n';
4
+ type Lang = keyof typeof ajvLocalize;
5
+ declare const validateOnClient: VovkValidateOnClient;
6
+ declare const configure: ({ options: givenOptions, localize: givenLocalize, }: {
7
+ options: Options;
8
+ localize: Lang;
9
+ }) => VovkValidateOnClient;
10
+ declare const validateOnClientAjv: typeof validateOnClient & {
11
+ configure: typeof configure;
12
+ };
13
+ export default validateOnClientAjv;
package/index.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ajv_1 = __importDefault(require("ajv"));
7
+ const vovk_1 = require("vovk");
8
+ const ajv_formats_1 = __importDefault(require("ajv-formats"));
9
+ const ajv_i18n_1 = __importDefault(require("ajv-i18n"));
10
+ const validate = ({ input, validation, ajv, localize = 'en', }) => {
11
+ if (validation.body) {
12
+ const isValid = ajv.validate(validation.body, input.body);
13
+ if (!isValid) {
14
+ ajv_i18n_1.default[localize](ajv.errors);
15
+ throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid request body on client for ${input.endpoint}. ${ajv.errorsText()}`, { body: input.body, endpoint: input.endpoint, errors: ajv.errors });
16
+ }
17
+ }
18
+ if (validation.query) {
19
+ const isValid = ajv.validate(validation.query, input.query);
20
+ if (!isValid) {
21
+ ajv_i18n_1.default[localize](ajv.errors);
22
+ throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid request query on client for ${input.endpoint}. ${ajv.errorsText()}`, { query: input.query, endpoint: input.endpoint, errors: ajv.errors });
23
+ }
24
+ }
25
+ if (validation.params) {
26
+ const isValid = ajv.validate(validation.params, input.params);
27
+ if (!isValid) {
28
+ ajv_i18n_1.default[localize](ajv.errors);
29
+ throw new vovk_1.HttpException(vovk_1.HttpStatus.NULL, `Ajv validation failed. Invalid request params on client for ${input.endpoint}. ${ajv.errorsText()}`, { params: input.params, endpoint: input.endpoint, errors: ajv.errors });
30
+ }
31
+ }
32
+ };
33
+ const getConfig = (fullSchema) => {
34
+ const config = fullSchema.config.custom;
35
+ const options = config?.ajvOptions || {};
36
+ const localize = config?.ajvLocalize || 'en';
37
+ return { options, localize };
38
+ };
39
+ let ajvScope = null;
40
+ const validateOnClient = (input, validation, fullSchema) => {
41
+ const { options, localize } = getConfig(fullSchema);
42
+ if (!ajvScope) {
43
+ ajvScope = new ajv_1.default(options);
44
+ (0, ajv_formats_1.default)(ajvScope);
45
+ }
46
+ return validate({ input, validation, ajv: ajvScope, localize });
47
+ };
48
+ const configure = ({ options: givenOptions, localize: givenLocalize, }) => (input, validation, fullSchema) => {
49
+ const { options, localize } = getConfig(fullSchema);
50
+ const ajv = new ajv_1.default({ ...options, ...givenOptions });
51
+ (0, ajv_formats_1.default)(ajv);
52
+ return validate({ input, validation, ajv, localize: givenLocalize || localize });
53
+ };
54
+ const validateOnClientAjv = Object.assign(validateOnClient, {
55
+ configure,
56
+ });
57
+ exports.default = validateOnClientAjv;
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "vovk-ajv",
3
+ "version": "0.0.0-draft.27",
4
+ "description": "Local validation for JSON schemas for vovk-zod and other validation libraries for Vovk.ts",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build": "tsc",
8
+ "tsc": "tsc --noEmit",
9
+ "npm-publish": "npm publish",
10
+ "ncu": "npm-check-updates -u"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/finom/vovk.git"
15
+ },
16
+ "keywords": [
17
+ "vovk",
18
+ "json",
19
+ "ajv",
20
+ "validation",
21
+ "zod"
22
+ ],
23
+ "author": "Andrey Gubanov",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/finom/vovk/issues"
27
+ },
28
+ "homepage": "https://github.com/finom/vovk#readme",
29
+ "dependencies": {
30
+ "ajv": "^8.17.1",
31
+ "ajv-formats": "^3.0.1",
32
+ "ajv-i18n": "^4.2.0"
33
+ },
34
+ "peerDependencies": {
35
+ "vovk": "^3.0.0-draft.91"
36
+ }
37
+ }