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

How to keep keys for optional fields in matchedData? #1273

Open
stenneepro opened this issue Jan 26, 2024 · 0 comments · May be fixed by #1275
Open

How to keep keys for optional fields in matchedData? #1273

stenneepro opened this issue Jan 26, 2024 · 0 comments · May be fixed by #1275
Labels

Comments

@stenneepro
Copy link

stenneepro commented Jan 26, 2024

Describe the bug

I want to keep keys for optional fields in request body in matchedData return value.

To Reproduce

Let's say we have user model.

interface User {
  firstName?: string;
  lastName?: string;
  email: string
}

In frontend, user removed first name. and other fields are not changed.
So frontend sends request with only changed fields.

{"firstName": null}

In this case matchedData function returns empty JSON, but I would like to keep firstName key.
There is a parameter called includeOptional, but if we set that parameter to True, matchedData function returns JSON which contains both firstName and lastName as key.

{
  "firstName": null,
  "lastName": null
}

Moreover, I think it's more accurate that if keys exist in both request and validation schema, it should exist in matchedData return value.

Environment:

  • Express-validator version: 7.0.1
  • Express version: 4.18.2
  • Node.js version: 20.11.0
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.

2 participants