mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 12:38:36 +00:00
28 lines
911 B
TypeScript
28 lines
911 B
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import { validationResult } from 'express-validator';
|
|
import { BadRequestError, UnauthorizedRequestError, ValidationError } from '../utils/errors';
|
|
|
|
/**
|
|
* Validate intended inputs on [req] via express-validator
|
|
* @param req - express request object
|
|
* @param res - express response object
|
|
* @param next - express next function
|
|
* @returns
|
|
*/
|
|
const validate = (req: Request, res: Response, next: NextFunction) => {
|
|
// express validator middleware
|
|
|
|
try {
|
|
const errors = validationResult(req);
|
|
if (!errors.isEmpty()) {
|
|
return next(ValidationError({ context: { errors: `One or more of your parameters are invalid [error(s)=${(JSON.stringify(errors))}]` } }))
|
|
}
|
|
|
|
return next();
|
|
} catch (err) {
|
|
return next(UnauthorizedRequestError({ message: 'Unauthenticated requests are not allowed. Try logging in' }))
|
|
}
|
|
};
|
|
|
|
export default validate;
|