Spaces:
Sleeping
Sleeping
File size: 1,003 Bytes
e9affa5 433085e 2f0e1b7 433085e e9affa5 433085e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import { JsonResponse } from "@lib/responses/json-response";
import { NextFunction, Request, Response } from "express";
import { createValidator } from "express-joi-validation";
import Joi from "joi";
const validator = createValidator({ passError: true });
export const bodyValidator = validator.body;
export const queryValidator = validator.query;
export const paramsValidator = (schemaOrParam: Joi.Schema | string) =>
typeof schemaOrParam === "string"
? validator.params(Joi.object({ [schemaOrParam]: Joi.string().required() }))
: validator.params(schemaOrParam);
export const validationErrorHandler = (
err,
_req: Request,
res: Response,
next: NextFunction
) => {
if (err && err.error && err.error.isJoi) {
console.log(`err`, err.error);
const errors = err.error.details.map((detail) => detail.message);
return JsonResponse.validationError(
{
errors,
},
res
);
} else {
// pass on to another error handler
next(err);
}
};
|