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

feat(common): adding the fieldname to validation errors #12403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sezanzeb
Copy link

@sezanzeb sezanzeb commented Sep 18, 2023

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #12357

It's impossible to know based on the BadRequest response payload which field is causing problems

What is the new behavior?

The error message contains the field name

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@sezanzeb sezanzeb force-pushed the feat/validation-error-fieldname branch 2 times, most recently from 79d9eca to 70401a0 Compare September 18, 2023 07:53
@sezanzeb sezanzeb force-pushed the feat/validation-error-fieldname branch from 70401a0 to 7ddc06e Compare September 18, 2023 07:53
@coveralls
Copy link

coveralls commented Sep 18, 2023

Pull Request Test Coverage Report for Build 4bfe98f8-c530-4503-87ed-d0e96d8d1b74

  • 3 of 4 (75.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 92.653%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/pipes/parse-array.pipe.ts 3 4 75.0%
Totals Coverage Status
Change from base Build df58eebf-9777-4621-ad6a-397c0c18d45a: 0.04%
Covered Lines: 6469
Relevant Lines: 6982

💛 - Coveralls

@sezanzeb sezanzeb marked this pull request as ready for review September 18, 2023 10:05
Copy link

@benjGam benjGam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks mate for this update, it will be usefull to do not do it manually for each new project.

@jmcdo29
Copy link
Member

jmcdo29 commented Sep 29, 2023

Should we also add the metadata.type so if there are multiple foo fields the user knows which one specifically caused the error (body, query, param, custom)?

@sezanzeb
Copy link
Author

sezanzeb commented Oct 4, 2023

I have started using those pipes together with a custom pipe to extract values from a nested payload.

If my body is like

{
  "foo": {
    "bar": "qux"
  }
}

I'll have to use something like @Body('foo', GetBar, ParseIntPipe)

This would cause the error message to be Validation failed (numeric string is expected in "foo").

I would much rather like this to be something like Validation failed (numeric string is expected in "foo.bar")

One solution I can think of, which would conveniently work for my use-case, is to create a custom decorator, like @BodyFoo('bar'), and to add a new optional field to metadata called name. My custom decorator could then overwrite metadata.name with "foo.bar".

@sezanzeb
Copy link
Author

sezanzeb commented Oct 4, 2023

Another one might be to support using an array instead of a string: @Body(['foo', 'bar'], ParseIntPipe) for nested attributes.

@sezanzeb
Copy link
Author

sezanzeb commented Oct 4, 2023

A third option would be to overwrite the name via the ParseIntPipeOptions, but I wouldn't be a huge fan of that one, because it adds redundant strings in the controllers class.

Any thoughts?

Copy link

@benjGam benjGam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, i looked to your code, and i enjoy the changes, but is it possible to add an way to turn off/on this ? Like do not force anybody to include field name in message, but instead add a (decorator ?) to apply or not this behavior ?

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

Successfully merging this pull request may close these issues.

None yet

7 participants