@sn1006/common 1.0.12 → 1.0.14

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.
@@ -0,0 +1,3 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ import Joi from "joi";
3
+ export default function joiValidateRequest(schema: Joi.AnySchema, options?: Joi.ValidationOptions): (req: Request, res: Response, next: NextFunction) => any;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const validation_error_1 = __importDefault(require("./validation-error"));
7
+ function joiValidateRequest(schema, options) {
8
+ return (req, res, next) => {
9
+ const { error } = schema.validate(req, options);
10
+ if (error) {
11
+ // normalized the error to be similar to express-validator error
12
+ const errors = error.details.map((err) => (Object.assign(Object.assign({}, err), { msg: err.message, path: err.path.join(".") })));
13
+ return next(new validation_error_1.default(errors));
14
+ }
15
+ return next();
16
+ };
17
+ }
18
+ exports.default = joiValidateRequest;
@@ -1,11 +1,14 @@
1
1
  import { NormalizedError } from './normalized-error';
2
- import { ValidationError } from 'express-validator';
2
+ interface NonSerializedValidationError {
3
+ msg: string;
4
+ path: string;
5
+ }
3
6
  declare class CustomValidationError extends NormalizedError {
4
- errors: ValidationError[];
7
+ errors: NonSerializedValidationError[];
5
8
  status: number;
6
- constructor(errors: ValidationError[]);
9
+ constructor(errors: NonSerializedValidationError[]);
7
10
  serializeErrors(): {
8
- message: any;
11
+ message: string;
9
12
  field: string;
10
13
  }[];
11
14
  }
@@ -10,8 +10,7 @@ class CustomValidationError extends normalized_error_1.NormalizedError {
10
10
  }
11
11
  serializeErrors() {
12
12
  return this.errors.map(err => {
13
- const ferr = err;
14
- return { message: ferr.msg, field: ferr.path };
13
+ return { message: err.msg, field: err.path };
15
14
  });
16
15
  }
17
16
  }
@@ -7,9 +7,10 @@ exports.validateRequest = void 0;
7
7
  const express_validator_1 = require("express-validator");
8
8
  const validation_error_1 = __importDefault(require("../errors/validation-error"));
9
9
  const validateRequest = (req, res, next) => {
10
- const errors = (0, express_validator_1.validationResult)(req);
11
- if (!errors.isEmpty()) {
12
- return next(new validation_error_1.default(errors.array()));
10
+ const error = (0, express_validator_1.validationResult)(req);
11
+ if (!error.isEmpty()) {
12
+ const errors = error.array();
13
+ return next(new validation_error_1.default(errors));
13
14
  }
14
15
  next();
15
16
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sn1006/common",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -26,6 +26,7 @@
26
26
  "cookie-session": "^2.0.0",
27
27
  "express": "^4.18.2",
28
28
  "express-validator": "^7.0.1",
29
+ "joi": "^17.12.1",
29
30
  "jsonwebtoken": "^9.0.1"
30
31
  }
31
32
  }