I'm trying to create a custom middleware function to use with Joi that I will place on my routes that I can pass a Joi schema to validate.
I have made middlewares for checking JWTs before, but those did not pass a parameter with them, and were fine.
Setting up the route and passing the middleware function
app.route('/identity').post([validator(identitySchema)], this.identityController.createIdentity);
validator middleware function
export const validator = (schema: object) => {
return async (req: Request, res: Response, next: NextFunction): Promise<void> => {
console.log('req: ' + req.body); //outputs req: [object Object]
console.log('schema: ' + schema); //outputs schema: [object Object]
//DO VALIDATION HERE
};
};
If I had just done
app.route('/identity').post([validator], this.identityController.createIdentity);
export const validator(req: Request, res: Response, next: NextFunction) => {
It would have picked up the req, res, etc...
schematype isobject, because from my typescriptobjectis error, so I change it toany. export const validator = (schema: any) => { ... } and it's working fine. - Titus Sutio Fanpula