question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No method for validating existence of property in req.params, req.body, or req.query

See original GitHub issue

Suppose I have a route expecting to receive the parameter req.body.heading. If this parameter exists and was sent by the client, then my validation will run as expected. I will check req.validationErrors() and proceed accordingly. However, if req.body.heading was not sent by the client, its value will be undefined. Based on my limited testing, when this happens, the undefined value will be coerced to an empty string and validation will proceed on that empty string. A validator deprecated message also gets printed to the console, and I understand this is going to be addressed in the future. However, the problem here is that there is currently no way to check if a value is undefined and produce an error when it is undefined. Currently, the undefined value gets coerced to an empty string and validated as an empty string.

This is problematic because there is a big difference between not receiving a value from the client and receiving an empty string. These constitute independent cases that one may wish to handle differently. In my case, I consider receiving no value (i.e. undefined) as an error, whereas receiving an empty string may or may not be valid depending on the context.

My solution has been to create a custom validator to check if the value exists:

customValidators: {
  exists: value => value !== undefined,
}

This achieves the desired behavior whereby if the value is undefined, an error will be produced in req.validationErrors(). In practice, I also use the optional() method immediately after exists() to stop any further validation when the value is undefined.

Typical usage would look something like this:

req.checkBody('heading').exists().optional()...;

With that in mind, I’d appreciate some feedback on whether or not this is the best way to address this issue. Likewise, if this makes sense and doesn’t conflict with any upgrade plans, I’d like to make a PR to add an exists() method. In this PR I would go ahead and bundle the optional() behavior into the exists() method since using it without that behavior doesn’t really make sense. Though I would add an option to allow exists() to be used without the optional() behavior.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gustavohenkecommented, Aug 27, 2017

Hello folks, v4.0.0 has finally been released.

It brings support for a new validator called .exists(), which you can see some example of how to use here: https://github.com/ctavan/express-validator/issues/382#issuecomment-325174919

Don’t forget to check the README for complete docs and the upgrade guide for version 3 users.

0reactions
lock[bot]commented, Jun 2, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do req.params, req.query and req.body exist?
req.body properties come from a form post where the form data (which is submitted in the body contents) has been parsed into properties...
Read more >
Modify Node.js req object parameters - Stack Overflow
I think if some parameter in params has already a value (e.g: foo: 'some value' ), it is not possible to change it's...
Read more >
Why do req.params, req.query and req.body exist
I'm new on Node.js and Express and now I'm observing that when I change the method on my calling app the params are...
Read more >
Express: req.params, req.query and req.body
These three, req.body, req.query and req.params are part of Express request object. They are used by the client to send data to the...
Read more >
Validation middlewares - express-validator
These methods are all available via `require('express-validator')`. ... req.body; req.cookies; req.headers; req.params; req.query.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found