Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchedData does not include nullable optionals #1048

Open
AndrewBarba opened this issue Jun 11, 2021 · 4 comments · May be fixed by #1275
Open

matchedData does not include nullable optionals #1048

AndrewBarba opened this issue Jun 11, 2021 · 4 comments · May be fixed by #1275
Labels

Comments

@AndrewBarba
Copy link

AndrewBarba commented Jun 11, 2021

Describe the bug

There's currently no easy way to allow a property to be set to null, and exclude any data that was not validated. A common pattern for us is to sanitize the request body post-validation using the matchedData function, for example:

// POST /user { username: "Andrew", age: null }

await validate(req, [
  body("username").isString(), 
  body("age").optional({ nullable: true }).isInt(),
  body("name").optional().isString()
])

req.body = matchedData(req, { locations: 'body' })

assert(req.body.age === null) // fails

I know matchedData provides the includeOptionals option but this option will take all optionals and then set them to undefined, which is definitely not what we want.

As is - there is no way to get access to just the nullable properties which I consider a bug in matchedData as it did in fact match against a null property and validated it according to optional({ nullable: true })

Expected behavior

assert(req.body.age === null) // succeeds

Current behavior

assert(req.body.age === null) // fails

Express-validator version:

  • Version: 6.x
@herbertpimentel
Copy link

+1 I agree, a nullable value should be inclueded in the matchedData, by definition, the rule says it can be null, and if it is null. it matches the definitions

@mrollinsiv

This comment was marked as off-topic.

@mrollinsiv
Copy link

Is there any update on this issue?

@erikhh92
Copy link

erikhh92 commented Jan 2, 2024

Yup, waiting here too. The problem is with this kind of types:

interface Body { age: number | null }

If i want to update a item that now has a value of age: 5 and make it null, i can't do this because the data come filtered without the null value, so the update item remains the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants