@naturalcycles/backend-lib 9.22.1 → 9.24.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.
@@ -5,6 +5,14 @@ declare class AjvValidateRequest {
5
5
  body<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
6
6
  query<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
7
7
  params<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
8
+ /**
9
+ * Does NOT mutate `req.headers`,
10
+ * but returns validated/transformed headers.
11
+ *
12
+ * For headers we have a different behavior compared to body/query/params.
13
+ * We want to non-mutate the `req.headers`, because we anticipate that
14
+ * there may be additional consumers for `req.headers` (e.g middlewares, etc).
15
+ */
8
16
  headers<T>(req: BackendRequest, schema: AjvSchema<T>, opt?: ReqValidationOptions<AjvValidationError>): T;
9
17
  private validate;
10
18
  }
@@ -1,3 +1,4 @@
1
+ import { _deepCopy } from '@naturalcycles/js-lib/object';
1
2
  import { handleValidationError } from '../validateRequest.util.js';
2
3
  class AjvValidateRequest {
3
4
  body(req, schema, opt = {}) {
@@ -9,14 +10,23 @@ class AjvValidateRequest {
9
10
  params(req, schema, opt = {}) {
10
11
  return this.validate(req, 'params', schema, opt);
11
12
  }
13
+ /**
14
+ * Does NOT mutate `req.headers`,
15
+ * but returns validated/transformed headers.
16
+ *
17
+ * For headers we have a different behavior compared to body/query/params.
18
+ * We want to non-mutate the `req.headers`, because we anticipate that
19
+ * there may be additional consumers for `req.headers` (e.g middlewares, etc).
20
+ */
12
21
  headers(req, schema, opt = {}) {
13
- return this.validate(req, 'headers', schema, opt);
22
+ const originalHeaders = _deepCopy(req.headers);
23
+ const headers = this.validate(req, 'headers', schema, opt);
24
+ req.headers = originalHeaders;
25
+ return headers;
14
26
  }
15
27
  validate(req, reqProperty, schema, opt = {}) {
16
- const { mutateInput = true } = opt;
17
28
  const input = req[reqProperty] || {};
18
29
  const [error, output] = schema.getValidationResult(input, {
19
- mutateInput,
20
30
  inputName: `request ${reqProperty}`,
21
31
  });
22
32
  if (error) {
@@ -23,7 +23,6 @@ class ValidateRequest {
23
23
  return this.validate(req, 'headers', schema, opt);
24
24
  }
25
25
  validate(req, reqProperty, schema, opt = {}) {
26
- const { mutateInput } = opt;
27
26
  const originalProperty = req[reqProperty] || {};
28
27
  // Joi does not mutate the input
29
28
  const [error, value] = getValidationResult(originalProperty, schema, `request ${reqProperty}`);
@@ -34,9 +33,8 @@ class ValidateRequest {
34
33
  }
35
34
  handleValidationError(error, originalProperty, opt);
36
35
  }
37
- if (mutateInput) {
38
- req[reqProperty] = value;
39
- }
36
+ // Kirill: decide to not do it, let's see
37
+ // req[reqProperty] = value
40
38
  return value;
41
39
  }
42
40
  }
@@ -11,11 +11,4 @@ export interface ReqValidationOptions<ERR extends AppError> {
11
11
  * If true - `genericErrorHandler` will report it to errorReporter (aka Sentry).
12
12
  */
13
13
  report?: boolean | ((err: ERR) => boolean);
14
- /**
15
- * Defaults to true.
16
- *
17
- * When set to true, input (body, query, headers or params) will be mutated.
18
- * So, e.g req.body will already contain mutated data post-validation.
19
- */
20
- mutateInput?: boolean;
21
14
  }
@@ -23,16 +23,14 @@ class ZodValidateRequest {
23
23
  return this.validate(req, 'headers', schema, opt);
24
24
  }
25
25
  validate(req, reqProperty, schema, opt = {}) {
26
- const { mutateInput } = opt;
27
26
  const originalProperty = req[reqProperty] || {};
28
27
  // Zod does not mutate the input
29
28
  const [error, data] = zSafeValidate(originalProperty, schema);
30
29
  if (error) {
31
30
  handleValidationError(error, originalProperty, opt);
32
31
  }
33
- if (mutateInput) {
34
- req[reqProperty] = data;
35
- }
32
+ // Kirill: decide to not do it, let's see
33
+ // req[reqProperty] = data
36
34
  return data;
37
35
  }
38
36
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.22.1",
4
+ "version": "9.24.0",
5
5
  "peerDependencies": {
6
- "@sentry/node": "^9"
6
+ "@sentry/node": "^10"
7
7
  },
8
8
  "dependencies": {
9
9
  "@naturalcycles/db-lib": "^10",
@@ -26,7 +26,7 @@
26
26
  "tslib": "^2"
27
27
  },
28
28
  "devDependencies": {
29
- "@sentry/node": "^9",
29
+ "@sentry/node": "^10",
30
30
  "@types/ejs": "^3",
31
31
  "fastify": "^5",
32
32
  "@naturalcycles/dev-lib": "18.4.2"
@@ -1,3 +1,4 @@
1
+ import { _deepCopy } from '@naturalcycles/js-lib/object'
1
2
  import type { AjvSchema, AjvValidationError } from '@naturalcycles/nodejs-lib/ajv'
2
3
  import type { BackendRequest } from '../../server/server.model.js'
3
4
  import { handleValidationError, type ReqValidationOptions } from '../validateRequest.util.js'
@@ -27,12 +28,23 @@ class AjvValidateRequest {
27
28
  return this.validate(req, 'params', schema, opt)
28
29
  }
29
30
 
31
+ /**
32
+ * Does NOT mutate `req.headers`,
33
+ * but returns validated/transformed headers.
34
+ *
35
+ * For headers we have a different behavior compared to body/query/params.
36
+ * We want to non-mutate the `req.headers`, because we anticipate that
37
+ * there may be additional consumers for `req.headers` (e.g middlewares, etc).
38
+ */
30
39
  headers<T>(
31
40
  req: BackendRequest,
32
41
  schema: AjvSchema<T>,
33
42
  opt: ReqValidationOptions<AjvValidationError> = {},
34
43
  ): T {
35
- return this.validate(req, 'headers', schema, opt)
44
+ const originalHeaders = _deepCopy(req.headers)
45
+ const headers = this.validate(req, 'headers', schema, opt)
46
+ req.headers = originalHeaders
47
+ return headers
36
48
  }
37
49
 
38
50
  private validate<T>(
@@ -41,11 +53,9 @@ class AjvValidateRequest {
41
53
  schema: AjvSchema<T>,
42
54
  opt: ReqValidationOptions<AjvValidationError> = {},
43
55
  ): T {
44
- const { mutateInput = true } = opt
45
56
  const input: T = req[reqProperty] || {}
46
57
 
47
58
  const [error, output] = schema.getValidationResult(input, {
48
- mutateInput,
49
59
  inputName: `request ${reqProperty}`,
50
60
  })
51
61
 
@@ -51,7 +51,6 @@ class ValidateRequest {
51
51
  schema: AnySchema<T>,
52
52
  opt: ReqValidationOptions<JoiValidationError> = {},
53
53
  ): T {
54
- const { mutateInput } = opt
55
54
  const originalProperty = req[reqProperty] || {}
56
55
 
57
56
  // Joi does not mutate the input
@@ -66,9 +65,8 @@ class ValidateRequest {
66
65
  handleValidationError(error, originalProperty, opt)
67
66
  }
68
67
 
69
- if (mutateInput) {
70
- req[reqProperty] = value
71
- }
68
+ // Kirill: decide to not do it, let's see
69
+ // req[reqProperty] = value
72
70
 
73
71
  return value
74
72
  }
@@ -50,12 +50,4 @@ export interface ReqValidationOptions<ERR extends AppError> {
50
50
  * If true - `genericErrorHandler` will report it to errorReporter (aka Sentry).
51
51
  */
52
52
  report?: boolean | ((err: ERR) => boolean)
53
-
54
- /**
55
- * Defaults to true.
56
- *
57
- * When set to true, input (body, query, headers or params) will be mutated.
58
- * So, e.g req.body will already contain mutated data post-validation.
59
- */
60
- mutateInput?: boolean
61
53
  }
@@ -50,7 +50,6 @@ class ZodValidateRequest {
50
50
  schema: ZodType<T>,
51
51
  opt: ReqValidationOptions<ZodValidationError> = {},
52
52
  ): T {
53
- const { mutateInput } = opt
54
53
  const originalProperty = req[reqProperty] || {}
55
54
 
56
55
  // Zod does not mutate the input
@@ -64,9 +63,8 @@ class ZodValidateRequest {
64
63
  handleValidationError(error, originalProperty, opt)
65
64
  }
66
65
 
67
- if (mutateInput) {
68
- req[reqProperty] = data
69
- }
66
+ // Kirill: decide to not do it, let's see
67
+ // req[reqProperty] = data
70
68
 
71
69
  return data
72
70
  }