@naturalcycles/backend-lib 4.12.0 → 4.13.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/index.d.ts CHANGED
@@ -23,6 +23,7 @@ export * from './server/reqValidationMiddleware';
23
23
  export * from './server/simpleRequestLoggerMiddleware';
24
24
  export * from './server/serverStatusMiddleware';
25
25
  export * from './server/validateMiddleware';
26
+ export * from './server/zodValidateMiddleware';
26
27
  export * from './server/request.log.util';
27
28
  export * from './server/startServer';
28
29
  export * from './server/startServer.model';
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ tslib_1.__exportStar(require("./server/reqValidationMiddleware"), exports);
28
28
  tslib_1.__exportStar(require("./server/simpleRequestLoggerMiddleware"), exports);
29
29
  tslib_1.__exportStar(require("./server/serverStatusMiddleware"), exports);
30
30
  tslib_1.__exportStar(require("./server/validateMiddleware"), exports);
31
+ tslib_1.__exportStar(require("./server/zodValidateMiddleware"), exports);
31
32
  tslib_1.__exportStar(require("./server/request.log.util"), exports);
32
33
  tslib_1.__exportStar(require("./server/startServer"), exports);
33
34
  tslib_1.__exportStar(require("./server/startServer.model"), exports);
@@ -0,0 +1,10 @@
1
+ import { ZodSchema, ZodValidationError } from '@naturalcycles/js-lib';
2
+ import { BackendRequestHandler } from './server.model';
3
+ import { ReqValidationOptions } from './reqValidationMiddleware';
4
+ /**
5
+ * Validates req property (body, params or query).
6
+ * Supports Joi schema or AjvSchema (from nodejs-lib).
7
+ * Able to redact sensitive data from the error message.
8
+ * Throws http 400 on error.
9
+ */
10
+ export declare function zodReqValidate(prop: 'body' | 'params' | 'query', schema: ZodSchema, opt?: ReqValidationOptions<ZodValidationError<any>>): BackendRequestHandler;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodReqValidate = void 0;
4
+ const js_lib_1 = require("@naturalcycles/js-lib");
5
+ const REDACTED = 'REDACTED';
6
+ /**
7
+ * Validates req property (body, params or query).
8
+ * Supports Joi schema or AjvSchema (from nodejs-lib).
9
+ * Able to redact sensitive data from the error message.
10
+ * Throws http 400 on error.
11
+ */
12
+ function zodReqValidate(prop, schema, opt = {}) {
13
+ const reportPredicate = typeof opt.report === 'function' ? opt.report : () => !!opt.report;
14
+ return (req, res, next) => {
15
+ const { error } = (0, js_lib_1.zSafeValidate)(req[prop], schema);
16
+ if (!error) {
17
+ return next();
18
+ }
19
+ const report = reportPredicate(error);
20
+ if (opt.redactPaths) {
21
+ redact(opt.redactPaths, req[prop], error);
22
+ }
23
+ return next(new js_lib_1.HttpError(error.message, {
24
+ httpStatusCode: 400,
25
+ report,
26
+ }));
27
+ };
28
+ }
29
+ exports.zodReqValidate = zodReqValidate;
30
+ /**
31
+ * Mutates error
32
+ */
33
+ function redact(redactPaths, obj, error) {
34
+ redactPaths
35
+ .map(path => (0, js_lib_1._get)(obj, path))
36
+ .filter(Boolean)
37
+ .forEach(secret => {
38
+ error.message = error.message.replace(secret, REDACTED);
39
+ });
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
- "version": "4.12.0",
3
+ "version": "4.13.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install && patch-package",
6
6
  "serve": "APP_ENV=dev nodemon",
package/src/index.ts CHANGED
@@ -23,6 +23,7 @@ export * from './server/reqValidationMiddleware'
23
23
  export * from './server/simpleRequestLoggerMiddleware'
24
24
  export * from './server/serverStatusMiddleware'
25
25
  export * from './server/validateMiddleware'
26
+ export * from './server/zodValidateMiddleware'
26
27
  export * from './server/request.log.util'
27
28
  export * from './server/startServer'
28
29
  export * from './server/startServer.model'
@@ -0,0 +1,57 @@
1
+ import {
2
+ HttpError,
3
+ _get,
4
+ ZodSchema,
5
+ ZodValidationError,
6
+ zSafeValidate,
7
+ } from '@naturalcycles/js-lib'
8
+ import { BackendRequestHandler } from './server.model'
9
+ import { ReqValidationOptions } from './reqValidationMiddleware'
10
+
11
+ const REDACTED = 'REDACTED'
12
+
13
+ /**
14
+ * Validates req property (body, params or query).
15
+ * Supports Joi schema or AjvSchema (from nodejs-lib).
16
+ * Able to redact sensitive data from the error message.
17
+ * Throws http 400 on error.
18
+ */
19
+ export function zodReqValidate(
20
+ prop: 'body' | 'params' | 'query',
21
+ schema: ZodSchema,
22
+ opt: ReqValidationOptions<ZodValidationError<any>> = {},
23
+ ): BackendRequestHandler {
24
+ const reportPredicate = typeof opt.report === 'function' ? opt.report : () => !!opt.report
25
+
26
+ return (req, res, next) => {
27
+ const { error } = zSafeValidate(req[prop], schema)
28
+ if (!error) {
29
+ return next()
30
+ }
31
+
32
+ const report = reportPredicate(error)
33
+
34
+ if (opt.redactPaths) {
35
+ redact(opt.redactPaths, req[prop], error)
36
+ }
37
+
38
+ return next(
39
+ new HttpError(error.message, {
40
+ httpStatusCode: 400,
41
+ report,
42
+ }),
43
+ )
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Mutates error
49
+ */
50
+ function redact(redactPaths: string[], obj: any, error: Error): void {
51
+ redactPaths
52
+ .map(path => _get(obj, path) as string)
53
+ .filter(Boolean)
54
+ .forEach(secret => {
55
+ error.message = error.message.replace(secret, REDACTED)
56
+ })
57
+ }