@naturalcycles/backend-lib 9.33.2 → 9.34.1
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.
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ZodType } from '@naturalcycles/js-lib/zod';
|
|
2
|
+
import { AjvSchema, type AjvValidationError } from '@naturalcycles/nodejs-lib/ajv';
|
|
2
3
|
import type { BackendRequest } from '../../server/server.model.js';
|
|
3
4
|
import { type ReqValidationOptions } from '../validateRequest.util.js';
|
|
4
5
|
declare class AjvValidateRequest {
|
|
5
|
-
body<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
|
|
6
|
-
query<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
|
|
7
|
-
params<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
|
|
6
|
+
body<T>(req: BackendRequest, schema: AjvSchema<T> | ZodType<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
|
|
7
|
+
query<T>(req: BackendRequest, schema: AjvSchema<T> | ZodType<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
|
|
8
|
+
params<T>(req: BackendRequest, schema: AjvSchema<T> | ZodType<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
|
|
8
9
|
/**
|
|
9
10
|
* Does NOT mutate `req.headers`,
|
|
10
11
|
* but returns validated/transformed headers.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _deepCopy } from '@naturalcycles/js-lib/object';
|
|
2
|
+
import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
2
3
|
import { handleValidationError } from '../validateRequest.util.js';
|
|
3
4
|
class AjvValidateRequest {
|
|
4
5
|
body(req, schema, opt = {}) {
|
|
@@ -26,7 +27,8 @@ class AjvValidateRequest {
|
|
|
26
27
|
}
|
|
27
28
|
validate(req, reqProperty, schema, opt = {}) {
|
|
28
29
|
const input = req[reqProperty] || {};
|
|
29
|
-
const
|
|
30
|
+
const ajvSchema = AjvSchema.create(schema);
|
|
31
|
+
const [error, output] = ajvSchema.getValidationResult(input, {
|
|
30
32
|
inputName: `request ${reqProperty}`,
|
|
31
33
|
});
|
|
32
34
|
if (error) {
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { _deepCopy } from '@naturalcycles/js-lib/object'
|
|
2
|
-
import type {
|
|
2
|
+
import type { ZodType } from '@naturalcycles/js-lib/zod'
|
|
3
|
+
import { AjvSchema, type AjvValidationError } from '@naturalcycles/nodejs-lib/ajv'
|
|
3
4
|
import type { BackendRequest } from '../../server/server.model.js'
|
|
4
5
|
import { handleValidationError, type ReqValidationOptions } from '../validateRequest.util.js'
|
|
5
6
|
|
|
6
7
|
class AjvValidateRequest {
|
|
7
8
|
body<T>(
|
|
8
9
|
req: BackendRequest,
|
|
9
|
-
schema: AjvSchema<T>,
|
|
10
|
+
schema: AjvSchema<T> | ZodType<T>,
|
|
10
11
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
11
12
|
): T {
|
|
12
13
|
return this.validate(req, 'body', schema, opt)
|
|
@@ -14,7 +15,7 @@ class AjvValidateRequest {
|
|
|
14
15
|
|
|
15
16
|
query<T>(
|
|
16
17
|
req: BackendRequest,
|
|
17
|
-
schema: AjvSchema<T>,
|
|
18
|
+
schema: AjvSchema<T> | ZodType<T>,
|
|
18
19
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
19
20
|
): T {
|
|
20
21
|
return this.validate(req, 'query', schema, opt)
|
|
@@ -22,7 +23,7 @@ class AjvValidateRequest {
|
|
|
22
23
|
|
|
23
24
|
params<T>(
|
|
24
25
|
req: BackendRequest,
|
|
25
|
-
schema: AjvSchema<T>,
|
|
26
|
+
schema: AjvSchema<T> | ZodType<T>,
|
|
26
27
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
27
28
|
): T {
|
|
28
29
|
return this.validate(req, 'params', schema, opt)
|
|
@@ -50,12 +51,13 @@ class AjvValidateRequest {
|
|
|
50
51
|
private validate<T>(
|
|
51
52
|
req: BackendRequest,
|
|
52
53
|
reqProperty: 'body' | 'params' | 'query' | 'headers',
|
|
53
|
-
schema: AjvSchema<T>,
|
|
54
|
+
schema: AjvSchema<T> | ZodType<T>,
|
|
54
55
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
55
56
|
): T {
|
|
56
57
|
const input: T = req[reqProperty] || {}
|
|
58
|
+
const ajvSchema = AjvSchema.create(schema)
|
|
57
59
|
|
|
58
|
-
const [error, output] =
|
|
60
|
+
const [error, output] = ajvSchema.getValidationResult(input, {
|
|
59
61
|
inputName: `request ${reqProperty}`,
|
|
60
62
|
})
|
|
61
63
|
|