How to use the express-validator/check.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 mdn / express-locallibrary-tutorial / controllers / bookinstanceController.js View on Github external
(req, res, next) => {

        // Extract the validation errors from a request.
        const errors = validationResult(req);

        // Create a BookInstance object with escaped/trimmed data and current id.
        var bookinstance = new BookInstance(
          { book: req.body.book,
            imprint: req.body.imprint,
            status: req.body.status,
            due_back: req.body.due_back,
            _id: req.params.id
           });

        if (!errors.isEmpty()) {
            // There are errors so render the form again, passing sanitized values and errors.
            Book.find({},'title')
                .exec(function (err, books) {
                    if (err) { return next(err); }
                    // Successful, so render.
github FSecureLABS / dref / dref / api / src / routes / targets.js View on Github external
], function (req, res, next) {
  const errors = validationResult(req)

  if (!errors.isEmpty()) {
    return res.status(422).json({ errors: errors.array() })
  }

  console.log('dref: POST Target\n' + JSON.stringify(req.body, null, 4))

  const record = {
    target: req.body.target,
    script: req.body.script
  }
  if (typeof req.body.hang !== 'undefined') record.hang = req.body.hang
  if (typeof req.body.fastRebind !== 'undefined') record.fastRebind = req.body.fastRebind
  if (typeof req.body.args !== 'undefined') record.args = req.body.args

  Target.findOneAndUpdate({
github T-Systems-RUS / Portfolio / server / features / technology / technology.controller.ts View on Github external
router.post('/technology/create', technologyValidator.createValidators(), (req, res) => {

  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(422).json({errors: errors.mapped()});
  }

  return technologyService.doesTechnologyExist(req.body.name, req.body.id).then(doesExist => {
    if (doesExist) {
      res.status(409).json({errors: {latest: {msg: 'Technology already exists'}}});
    } else {
      technologyService.createTechnology(req.body).then(technology => {
        res.status(200).send(technology);
      }).catch(error => {
        res.status(500).json({errors: {er: {msg: error.message}}});
      });
    }
  }).catch(error => {
    res.status(500).json({errors: {er: {msg: error.message}}});
github pietrzakadrian / bank / server / controllers / transaction.controller.js View on Github external
exports.getRecipientdata = (req, res, next) => {
  function setAuthorizationStatus(status) {
    const authorizationStatus = status;
    return authorizationStatus;
  }
  const errors = validationResult(req);
  const id_recipient = req.params.recipientId;

  if (!errors.isEmpty()) {
    return next(newError(422, errors.array()));
  }

  Transaction.findAll({
    limit: 4,
    where: {
      id_recipient,
      authorization_status: setAuthorizationStatus(1),
    },
    attributes: [
      'amount_money',
      'date_time',
      'id_recipient',
github librewiki / liberty-engine / lib / routes / v1 / revisions.js View on Github external
async (req, res, next) => {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return new Response.BadRequest({ errors: errors.array() }).send(res);
      }
      const revision = await Revision.findByPk(req.params.revisionId, {
        include: [
          Revision.associations.author,
          Revision.associations.wikitext,
        ],
      });
      if (!revision) {
        return new Response.ResourceNotFound().send(res);
      }
      return new Response.Success({
        revision: {
          id: revision.id,
          changedLength: revision.changedLength,
github weseek / growi / src / server / middlewares / ApiV3FormValidator.js View on Github external
return (req, res, next) => {
      logger.debug('req.query', req.query);
      logger.debug('req.params', req.params);
      logger.debug('req.body', req.body);

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

      const errs = errObjArray.array().map((err) => {
        logger.error(`${err.location}.${err.param}: ${err.value} - ${err.msg}`);
        return new ErrorV3(`${err.param}: ${err.msg}`, 'validation_failed');
      });

      return res.apiv3Err(errs);
    };
  }
github pietrzakadrian / bank / backend / src / controllers / bills.controller.ts View on Github external
async (req: Request, res: Response, next: NextFunction) => {
      const billService = new BillService();
      const userService = new UserService();
      const validationErrors = validationResult(req);
      const accountBill: string = req.params.accountBill;

      if (!validationErrors.isEmpty()) {
        const err: IResponseError = {
          success: false,
          code: HttpStatus.BAD_REQUEST,
          error: validationErrors.array()
        };
        return next(err);
      }

      try {
        const user: User = await userService.getById(req.user.id);
        const bills = await billService.getUsersByAccountBill(
          accountBill,
          user
github bradtraversy / devconnector_2.0 / 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 {
      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);
github bradtraversy / devconnector_2.0 / routes / api / profile.js View on Github external
async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    const {
      school,
      degree,
      fieldofstudy,
      from,
      to,
      current,
      description
    } = req.body;

    const newEdu = {
      school,
github onehilltech / blueprint / packages / blueprint / lib / middleware / handle-validation-result.js View on Github external
module.exports = function (req, res, next) {
  const errors = validationResult (req);

  if (errors.isEmpty ())
    return next ();

  return next (new HttpError (400, 'validation_failed', 'The request validation failed.', {validation: errors.mapped ()}));
};