@naturalcycles/backend-lib 9.38.0 → 9.39.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.
|
@@ -2,9 +2,9 @@ import { type AjvValidationError, type SchemaHandledByAjv } from '@naturalcycles
|
|
|
2
2
|
import type { BackendRequest } from '../../server/server.model.js';
|
|
3
3
|
import { type ReqValidationOptions } from '../validateRequest.util.js';
|
|
4
4
|
declare class AjvValidateRequest {
|
|
5
|
-
body<
|
|
6
|
-
query<
|
|
7
|
-
params<
|
|
5
|
+
body<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
|
|
6
|
+
query<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
|
|
7
|
+
params<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
|
|
8
8
|
/**
|
|
9
9
|
* Does NOT mutate `req.headers`,
|
|
10
10
|
* but returns validated/transformed headers.
|
|
@@ -13,7 +13,7 @@ declare class AjvValidateRequest {
|
|
|
13
13
|
* We want to non-mutate the `req.headers`, because we anticipate that
|
|
14
14
|
* there may be additional consumers for `req.headers` (e.g middlewares, etc).
|
|
15
15
|
*/
|
|
16
|
-
headers<
|
|
16
|
+
headers<IN, OUT>(req: BackendRequest, schema: SchemaHandledByAjv<IN, OUT>, opt?: ReqValidationOptions<AjvValidationError>): OUT;
|
|
17
17
|
private validate;
|
|
18
18
|
}
|
|
19
19
|
export declare const ajvValidateRequest: AjvValidateRequest;
|
package/package.json
CHANGED
|
@@ -8,27 +8,27 @@ import type { BackendRequest } from '../../server/server.model.js'
|
|
|
8
8
|
import { handleValidationError, type ReqValidationOptions } from '../validateRequest.util.js'
|
|
9
9
|
|
|
10
10
|
class AjvValidateRequest {
|
|
11
|
-
body<
|
|
11
|
+
body<IN, OUT>(
|
|
12
12
|
req: BackendRequest,
|
|
13
|
-
schema: SchemaHandledByAjv<
|
|
13
|
+
schema: SchemaHandledByAjv<IN, OUT>,
|
|
14
14
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
15
|
-
):
|
|
15
|
+
): OUT {
|
|
16
16
|
return this.validate(req, 'body', schema, opt)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
query<
|
|
19
|
+
query<IN, OUT>(
|
|
20
20
|
req: BackendRequest,
|
|
21
|
-
schema: SchemaHandledByAjv<
|
|
21
|
+
schema: SchemaHandledByAjv<IN, OUT>,
|
|
22
22
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
23
|
-
):
|
|
23
|
+
): OUT {
|
|
24
24
|
return this.validate(req, 'query', schema, opt)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
params<
|
|
27
|
+
params<IN, OUT>(
|
|
28
28
|
req: BackendRequest,
|
|
29
|
-
schema: SchemaHandledByAjv<
|
|
29
|
+
schema: SchemaHandledByAjv<IN, OUT>,
|
|
30
30
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
31
|
-
):
|
|
31
|
+
): OUT {
|
|
32
32
|
return this.validate(req, 'params', schema, opt)
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -40,24 +40,24 @@ class AjvValidateRequest {
|
|
|
40
40
|
* We want to non-mutate the `req.headers`, because we anticipate that
|
|
41
41
|
* there may be additional consumers for `req.headers` (e.g middlewares, etc).
|
|
42
42
|
*/
|
|
43
|
-
headers<
|
|
43
|
+
headers<IN, OUT>(
|
|
44
44
|
req: BackendRequest,
|
|
45
|
-
schema: SchemaHandledByAjv<
|
|
45
|
+
schema: SchemaHandledByAjv<IN, OUT>,
|
|
46
46
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
47
|
-
):
|
|
47
|
+
): OUT {
|
|
48
48
|
const originalHeaders = _deepCopy(req.headers)
|
|
49
49
|
const headers = this.validate(req, 'headers', schema, opt)
|
|
50
50
|
req.headers = originalHeaders
|
|
51
51
|
return headers
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
private validate<
|
|
54
|
+
private validate<IN, OUT>(
|
|
55
55
|
req: BackendRequest,
|
|
56
56
|
reqProperty: 'body' | 'params' | 'query' | 'headers',
|
|
57
|
-
schema: SchemaHandledByAjv<
|
|
57
|
+
schema: SchemaHandledByAjv<IN, OUT>,
|
|
58
58
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
59
|
-
):
|
|
60
|
-
const input:
|
|
59
|
+
): OUT {
|
|
60
|
+
const input: IN = req[reqProperty] || {}
|
|
61
61
|
const ajvSchema = AjvSchema.create(schema)
|
|
62
62
|
|
|
63
63
|
const [error, output] = ajvSchema.getValidationResult(input, {
|