How to use express validator?


How to use express validator?

How to use express validator through full examples. I found express validator very powerful, but having a poor documentation. I decided to offer some examples that are very common and probably mainly searched.

The official documentation can be checked on Express Validator Documentation .

Validate nested object using express validator

Sometimes you need to validate an object. If the object is required that’s simple:

req.body = {
    user: {
        email: "[email protected]",
        password: "pass"
    }
}

the validator looks like:

app.post(
  '/user',
  // user is a required object
    body('user').exists().isObject(),
  // email must be an email
    body('user.email').exists().isEmail(),
  // password must be at least 5 chars long
    body('user.password').exists().isLength({ min: 3 }),

  (req, res, next) => {
    // Finds the validation errors in this request and wraps them in an object with handy functions
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    next()
  },
);

What happens if the user object is optional, but if present email and password are required?

app.post(
  '/user',
  // user is a required object
    body('user').exists().isObject(),
  // email must be an email
    body('user.email')
        .if(body('user').exists()) // check if user property exists
        .exists() // then check user.email to exists
        .isEmail(),
  // password must be at least 5 chars long
    body('user.password')
        .if(body('user').exists())
        .exists().isLength({ min: 3 }),

  (req, res, next) => {
    // Finds the validation errors in this request and wraps them in an object with handy functions
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    next()
  },
);

Newsletter


Related Posts

What I learned to never do in a presentation meeting

What I learned to never do in a presentation meeting. I had a very bad meeting experience with a gateway provider.

Startup graveyard is a series of killed projects that made millions in revenue per year

Startup graveyard is a series of killed projects that made millions in revenue per year. Just watch their graveyard and do it your own. What better validation do you expect.

How do you detect Credit card type based on the number?

How do you detect Credit card type based on the number using JavaScript? Simple solution using only code, no library required.

Download files in Javascript from Node.js server

How to download files in Javascript, either you use Vue.js, React, Angular, jQuery, or Vanilla JS. On the backend side, we run on Node.js using Express.js.

What books to read

The list of books I read. I decided one year ago to challenge myself and read as many books as I can this year. Take a look at this article and find all of them.

Startup tools for a better version

Are you a startup founder and are you looking for startup tools like invoice, planning or just making money with much automation? Check this article, from founder to founders.

How to check DNS propagation

How to check DNS propagation? Did you changed the DNS records recently and now is not working? Stop crying and check the DNS propagation.

LibraPay library for Nodejs

LibraPay library for Nodejs - integrate Libra Bank online payment gateway using this library for NodeJs.

Opensource Object Storage with Minio

Opensource Object Storage with Minio using Docker. An alternative to AWS S3, Linode Storage, Google Storage, Azure Storage.

Antiatomic bunker list in Romania

I created an antiatomic bunker list in Romania shared on GitHub repo in CSV and JSON format. Check this out and build your next app using it.