@naturalcycles/backend-lib 5.9.1 → 5.11.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.
- package/dist/server/validation/validateMiddleware.d.ts +1 -0
- package/dist/server/validation/validateMiddleware.js +5 -1
- package/dist/server/validation/validateRequest.d.ts +14 -0
- package/dist/server/validation/validateRequest.js +20 -2
- package/package.json +1 -1
- package/src/server/validation/validateMiddleware.ts +9 -2
- package/src/server/validation/validateRequest.ts +32 -3
|
@@ -5,3 +5,4 @@ import { ReqValidationOptions } from './validateRequest';
|
|
|
5
5
|
export declare function validateBody(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
|
|
6
6
|
export declare function validateParams(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
|
|
7
7
|
export declare function validateQuery(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
|
|
8
|
+
export declare function validateHeaders(schema: JsonSchema | JsonSchemaBuilder | AjvSchema, opt?: ReqValidationOptions<AjvValidationError>): BackendRequestHandler;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.validateBody = validateBody;
|
|
4
4
|
exports.validateParams = validateParams;
|
|
5
5
|
exports.validateQuery = validateQuery;
|
|
6
|
+
exports.validateHeaders = validateHeaders;
|
|
6
7
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
7
8
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
8
9
|
const REDACTED = 'REDACTED';
|
|
@@ -15,6 +16,9 @@ function validateParams(schema, opt = {}) {
|
|
|
15
16
|
function validateQuery(schema, opt = {}) {
|
|
16
17
|
return validateObject('query', schema, opt);
|
|
17
18
|
}
|
|
19
|
+
function validateHeaders(schema, opt = {}) {
|
|
20
|
+
return validateObject('headers', schema, opt);
|
|
21
|
+
}
|
|
18
22
|
/**
|
|
19
23
|
* Validates req property (body, params or query).
|
|
20
24
|
* Supports Joi schema or AjvSchema (from nodejs-lib).
|
|
@@ -51,6 +55,6 @@ function redact(redactPaths, obj, error) {
|
|
|
51
55
|
.map(path => (0, js_lib_1._get)(obj, path))
|
|
52
56
|
.filter(Boolean)
|
|
53
57
|
.forEach(secret => {
|
|
54
|
-
error.message = error.message.
|
|
58
|
+
error.message = error.message.replaceAll(secret, REDACTED);
|
|
55
59
|
});
|
|
56
60
|
}
|
|
@@ -11,11 +11,25 @@ export interface ReqValidationOptions<ERR extends Error> {
|
|
|
11
11
|
* If true - `genericErrorHandler` will report it to errorReporter (aka Sentry).
|
|
12
12
|
*/
|
|
13
13
|
report?: boolean | ((err: ERR) => boolean);
|
|
14
|
+
/**
|
|
15
|
+
* When set to true, the validated object will not be replaced with the Joi-converted value.
|
|
16
|
+
*/
|
|
17
|
+
keepOriginal?: boolean;
|
|
14
18
|
}
|
|
15
19
|
declare class ValidateRequest {
|
|
16
20
|
body<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
|
|
17
21
|
query<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
|
|
18
22
|
params<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
|
|
23
|
+
/**
|
|
24
|
+
* Validates `req.headers` against the provided Joi schema.
|
|
25
|
+
*
|
|
26
|
+
* Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
|
|
27
|
+
* i.e. schemas that cast values will not have any effect.
|
|
28
|
+
*
|
|
29
|
+
* If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
|
|
30
|
+
* Keep in mind that this will also remove all values that are not in the schema.
|
|
31
|
+
*/
|
|
32
|
+
headers<T>(req: BackendRequest, schema: AnySchema<T>, opt?: ReqValidationOptions<JoiValidationError>): T;
|
|
19
33
|
private validate;
|
|
20
34
|
}
|
|
21
35
|
export declare const validateRequest: ValidateRequest;
|
|
@@ -12,7 +12,7 @@ function redact(redactPaths, obj, error) {
|
|
|
12
12
|
.map(path => (0, js_lib_1._get)(obj, path))
|
|
13
13
|
.filter(Boolean)
|
|
14
14
|
.forEach(secret => {
|
|
15
|
-
error.message = error.message.
|
|
15
|
+
error.message = error.message.replaceAll(secret, REDACTED);
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
class ValidateRequest {
|
|
@@ -25,6 +25,22 @@ class ValidateRequest {
|
|
|
25
25
|
params(req, schema, opt = {}) {
|
|
26
26
|
return this.validate(req, 'params', schema, opt);
|
|
27
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Validates `req.headers` against the provided Joi schema.
|
|
30
|
+
*
|
|
31
|
+
* Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
|
|
32
|
+
* i.e. schemas that cast values will not have any effect.
|
|
33
|
+
*
|
|
34
|
+
* If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
|
|
35
|
+
* Keep in mind that this will also remove all values that are not in the schema.
|
|
36
|
+
*/
|
|
37
|
+
headers(req, schema, opt = {}) {
|
|
38
|
+
const options = {
|
|
39
|
+
keepOriginal: true,
|
|
40
|
+
...opt,
|
|
41
|
+
};
|
|
42
|
+
return this.validate(req, 'headers', schema, options);
|
|
43
|
+
}
|
|
28
44
|
validate(req, reqProperty, schema, opt = {}) {
|
|
29
45
|
const { value, error } = (0, nodejs_lib_1.getValidationResult)(req[reqProperty], schema, `request ${reqProperty}`);
|
|
30
46
|
if (error) {
|
|
@@ -47,7 +63,9 @@ class ValidateRequest {
|
|
|
47
63
|
});
|
|
48
64
|
}
|
|
49
65
|
// mutate req to replace the property with the value, converted by Joi
|
|
50
|
-
|
|
66
|
+
if (!opt.keepOriginal) {
|
|
67
|
+
req[reqProperty] = value;
|
|
68
|
+
}
|
|
51
69
|
return value;
|
|
52
70
|
}
|
|
53
71
|
}
|
package/package.json
CHANGED
|
@@ -26,6 +26,13 @@ export function validateQuery(
|
|
|
26
26
|
return validateObject('query', schema, opt)
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
export function validateHeaders(
|
|
30
|
+
schema: JsonSchema | JsonSchemaBuilder | AjvSchema,
|
|
31
|
+
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
32
|
+
): BackendRequestHandler {
|
|
33
|
+
return validateObject('headers', schema, opt)
|
|
34
|
+
}
|
|
35
|
+
|
|
29
36
|
/**
|
|
30
37
|
* Validates req property (body, params or query).
|
|
31
38
|
* Supports Joi schema or AjvSchema (from nodejs-lib).
|
|
@@ -33,7 +40,7 @@ export function validateQuery(
|
|
|
33
40
|
* Throws http 400 on error.
|
|
34
41
|
*/
|
|
35
42
|
function validateObject(
|
|
36
|
-
prop: 'body' | 'params' | 'query',
|
|
43
|
+
prop: 'body' | 'params' | 'query' | 'headers',
|
|
37
44
|
schema: JsonSchema | JsonSchemaBuilder | AjvSchema,
|
|
38
45
|
opt: ReqValidationOptions<AjvValidationError> = {},
|
|
39
46
|
): BackendRequestHandler {
|
|
@@ -75,6 +82,6 @@ function redact(redactPaths: string[], obj: any, error: Error): void {
|
|
|
75
82
|
.map(path => _get(obj, path) as string)
|
|
76
83
|
.filter(Boolean)
|
|
77
84
|
.forEach(secret => {
|
|
78
|
-
error.message = error.message.
|
|
85
|
+
error.message = error.message.replaceAll(secret, REDACTED)
|
|
79
86
|
})
|
|
80
87
|
}
|
|
@@ -16,6 +16,11 @@ export interface ReqValidationOptions<ERR extends Error> {
|
|
|
16
16
|
* If true - `genericErrorHandler` will report it to errorReporter (aka Sentry).
|
|
17
17
|
*/
|
|
18
18
|
report?: boolean | ((err: ERR) => boolean)
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* When set to true, the validated object will not be replaced with the Joi-converted value.
|
|
22
|
+
*/
|
|
23
|
+
keepOriginal?: boolean
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
/**
|
|
@@ -26,7 +31,7 @@ function redact(redactPaths: string[], obj: any, error: Error): void {
|
|
|
26
31
|
.map(path => _get(obj, path) as string)
|
|
27
32
|
.filter(Boolean)
|
|
28
33
|
.forEach(secret => {
|
|
29
|
-
error.message = error.message.
|
|
34
|
+
error.message = error.message.replaceAll(secret, REDACTED)
|
|
30
35
|
})
|
|
31
36
|
}
|
|
32
37
|
|
|
@@ -55,9 +60,30 @@ class ValidateRequest {
|
|
|
55
60
|
return this.validate(req, 'params', schema, opt)
|
|
56
61
|
}
|
|
57
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Validates `req.headers` against the provided Joi schema.
|
|
65
|
+
*
|
|
66
|
+
* Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
|
|
67
|
+
* i.e. schemas that cast values will not have any effect.
|
|
68
|
+
*
|
|
69
|
+
* If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
|
|
70
|
+
* Keep in mind that this will also remove all values that are not in the schema.
|
|
71
|
+
*/
|
|
72
|
+
headers<T>(
|
|
73
|
+
req: BackendRequest,
|
|
74
|
+
schema: AnySchema<T>,
|
|
75
|
+
opt: ReqValidationOptions<JoiValidationError> = {},
|
|
76
|
+
): T {
|
|
77
|
+
const options: ReqValidationOptions<JoiValidationError> = {
|
|
78
|
+
keepOriginal: true,
|
|
79
|
+
...opt,
|
|
80
|
+
}
|
|
81
|
+
return this.validate(req, 'headers', schema, options)
|
|
82
|
+
}
|
|
83
|
+
|
|
58
84
|
private validate<T>(
|
|
59
85
|
req: BackendRequest,
|
|
60
|
-
reqProperty: 'body' | 'params' | 'query',
|
|
86
|
+
reqProperty: 'body' | 'params' | 'query' | 'headers',
|
|
61
87
|
schema: AnySchema<T>,
|
|
62
88
|
opt: ReqValidationOptions<JoiValidationError> = {},
|
|
63
89
|
): T {
|
|
@@ -84,7 +110,10 @@ class ValidateRequest {
|
|
|
84
110
|
}
|
|
85
111
|
|
|
86
112
|
// mutate req to replace the property with the value, converted by Joi
|
|
87
|
-
|
|
113
|
+
if (!opt.keepOriginal) {
|
|
114
|
+
req[reqProperty] = value
|
|
115
|
+
}
|
|
116
|
+
|
|
88
117
|
return value
|
|
89
118
|
}
|
|
90
119
|
}
|