Search code examples
typescriptexpressexpress-validator

no response from endpoint when using express-validator with typescript


I'm trying to validate the response body for my endpoint, however, I get no response from hat endpoint when I use express-validator. I'm pretty sure that I followed the official documentation:

Here is my endpoint:

import { body, validationResult } from "express-validator";

export const accountRouter = express.Router();

accountRouter.post( "/signIn", body('userName').notEmpty, body('password').notEmpty, async ( req: express.Request, res: express.Response)  =>  {
    const errors = validationResult(req);
    if (!errors.isEmpty()) return res.sendStatus(400);
    
    ...doStuff();
})

Solution

  • You missed the brackets on the validation chain functions. Here is a corrected route:

    accountRouter.post( "/signIn", body('userName').notEmpty(), body('password').notEmpty(), async ( req: express.Request, res: express.Response)  =>  {
        const errors = validationResult(req);
        if (!errors.isEmpty()) return res.sendStatus(400);
        
        ...doStuff();
    })
    

    Here is the documentation for it: https://express-validator.github.io/docs/validation-chain-api.html#notempty