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.

(optional) null should be part of matchedData

See original GitHub issue

Hi,

TLDR: matchedData() is not returning my body field with JSON value of null, while I try to validate the data with the check API.

I don’t know if I use your validator incorrect or if it’s a feature request… but… I use your validator for my backend app before sending the JSON object further to the database (eg. MariaDB).

I have a database field what either can be of type integer or have the value of NULL. So I tried to use your validator in this way (at least this is what I thought should work):

body('journal_bank_id').optional({nullable: true}).isInt()

However, I’m also using the filter matchedData() function in order to guarantee that my data I put in my database is fully validated first by your validator package 👍

object = matchedData(req)

Now, when I try to request an API call to the Express backend using your validator with the HTTP body:

{"journal_bank_id": null}

Expected situation: I would expect the JSON object is now validated correctly and the journal_bank_id with null value becomes part of the returned object from matchedData(). Meaning the MySQL query will set the corresponding field to NULL accordantly. So basically I would expect matchedData() returns: {"journal_bank_id": null}

Actual situation: matchedData() is not returning journal_bank_id with value null at all. Meaning my database field can’t be set to NULL because of your validator.

Do I miss something here? Should I maybe not use optional()? I couldn’t find isNull()… I have no clue anymore…

Thanks in advance!

Kind regards, Melroy van den Berg

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gustavohenkecommented, Jul 23, 2018

Shipped on v5.3.0! 🚢

0reactions
lock[bot]commented, May 31, 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 is Express Validator not allowing null values in optional ...
On a hunch I tried reversing the method calls. This validator worked for me: body("date_field").isDate().optional({ nullable: true }),.
Read more >
matchedData() - express-validator
Extracts data validated or sanitized by express-validator from the request and builds an object with them. Nested paths and wildcards are properly handled ......
Read more >
anyMatch instead of Stream::findFirst or Stream::findAny ...
When the matched data returned by Stream::findFirst or Stream::findAny is not used anywhere else, using Stream::anyMatch is more readable and convenient ...
Read more >
Guide To Java 8 Optional | Baeldung
A value is present only if we have created Optional with a non-null value. We'll look at the isPresent() method in the next...
Read more >
Java Optional Tutorial with Examples - CalliCoder
of() is null, then it will throw a NullPointerException immediately and the Optional object won't be created. 3. Create an Optional with a...
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