Skip to content

Documentation issue #730

@castri1

Description

@castri1

In your documentation page https://express-validator.github.io/docs/running-imperatively.html you have a mistake:

The async function should be the closure function (in this case the request handler), and not the validate function itself. Example:

// can be reused by many routes
const validate = validations => {
  return async (req, res, next) => {
   await Promise.all(validations.map(validation => validation.run(req)));

    const errors = validationResult(req);
    if (errors.isEmpty()) {
      return next();
    }

    res.status(422).json({ errors: errors.array() });
  };
};

app.post('/api/create-user', validate([
  body('email').isEmail(),
  body('password').isLength({ min: 6 })
]), async (req, res, next) => {
// request is guaranteed to not have any validation errors.
  const user = await User.create({ ... });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions