How to use the express-validator.validationResult function in express-validator

To help you get started, we’ve selected a few express-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WagonOfDoubt / kotoba.js / app / src / routes / auth.js View on Github external
async (req, res, next) => {
    const { login, password } = req.body;

    // check request params
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      req.flash('errors', errors.array());
      res.redirect('/manage/registration');
      return;
    }

    // check if login already taken
    const loginAlreadyUsed = await User.findOne({ login }).exec();
    if (loginAlreadyUsed) {
      req.flash('errors', ['This login already taken']);
      res.redirect('/manage/registration');
      return;
    }

    // if no users was created, first user will be admin
    const numberOfUsers = await User.count().exec();
github WagonOfDoubt / kotoba.js / app / src / middlewares / validation.js View on Github external
(req, res, next) => {
    try {
      if (validationResult(req).isEmpty()) {
        next();
      } else {
        return res.redirect(redirect);
      }
    } catch (errorWhileProcessingError) {
      next(errorWhileProcessingError);
    }
  };
github penglin0613 / bigEvents / utils / message.js View on Github external
errorMsg: (req, res, next) => {
    const valRes = validationResult(req)
    if (!valRes.isEmpty()) {
      let msg = ""
      valRes.errors.forEach(v => {
        msg += `${v.param},`
      })
      msg = msg.slice(0, -1)
      msg += " 参数有问题,请检查"
      return res.status(400).send({
        msg
      })
    }
    next()
  }
}
github paulrobertlloyd / indiekit / packages / app / controllers / config.js View on Github external
], (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.render('config/github', {errors: errors.mapped()});
  }

  publisher('github').setAll(req.body);
  res.redirect(req.query.referrer || '/config/publication');
});
github davellanedam / node-express-mongodb-jwt-rest-api-skeleton / app / middleware / utils.js View on Github external
exports.validationResult = (req, res, next) => {
  try {
    validationResult(req).throw()
    if (req.body.email) {
      req.body.email = req.body.email.toLowerCase()
    }
    return next()
  } catch (err) {
    return this.handleError(res, this.buildErrObject(422, err.array()))
  }
}
github NERC-CEH / datalab / code / workspaces / infrastructure-api / src / controllers / stacksController.spec.js View on Github external
function expectValidationError(fieldName, expectedMessage) {
  expect(validationResult(request).mapped()[fieldName].msg).toEqual(expectedMessage);
}
github NERC-CEH / datalab / code / workspaces / infrastructure-api / src / controllers / stackController.spec.js View on Github external
function expectValidationError(fieldName, expectedMessage) {
  expect(validationResult(request).mapped()[fieldName].msg).toEqual(expectedMessage);
}
github nerdeveloper / hackathon-starter-kit / src / controllers / postController.ts View on Github external
export const createPost = async (req: Request, res: Response) => {
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
        //@ts-ignore
        req.flash("error", errors.array().map(err => err.msg));
        res.render("create", {
            title: "Create Post",
            body: req.body,
            flashes: req.flash(),
        });
    } else {
        //@ts-ignore
        req.body.author = req.user._id;
        await new Post(req.body).save();
        req.flash("success", "Your Post has been created!");
        res.redirect("/posts");
    }
github tobihoff / fluant / routes / api / auth.js View on Github external
async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    const { email, password } = req.body;

    try {
      //See if user exist
      let user = await User.findOne({ email });
      if (!user) {
        return res
          .status(400)
          .json({ errors: [{ msg: "Invalid Credentials" }] });
      }

      const isMatch = await bcrypt.compare(password, user.password);