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(rest): improve validation errors for invalid parameter value #6105

Merged
merged 1 commit into from
Aug 17, 2020

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Aug 13, 2020

Rework the validation code to use exiting RestHttpErrors.invalidData error instead of a custom BadRequest error. This way the error object includes the parameter name in the error message & properties and has a machine-readable code property.

See also #5808 which introduced parameter coercion.

Surprisingly, all existing tests are passing with my change in place, so I didn't make any changes in the test suite. Please let me know if you think I should write a new test 🤷🏻

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@bajtos bajtos added feature Validation REST Issues related to @loopback/rest package and REST transport in general labels Aug 13, 2020
@bajtos bajtos requested a review from jannyHou August 13, 2020 12:57
export function invalidRequestBody(): HttpErrors.HttpError {

export function invalidRequestBody(
details: ValidationErrorDetails[],
Copy link
Member

Choose a reason for hiding this comment

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

would there be backward compatibility issue for adding a parameter?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

In the strict interpretation, this is a breaking change because existing callers of invalidRequestBody must be updated. However, I consider invalidRequestBody as an internal helper that's not a part of our public API, therefore I think it's ok to make this change in a semver-minor release.

@hacksparrow
Copy link
Contributor

What about tests?

@bajtos
Copy link
Member Author

bajtos commented Aug 14, 2020

@hacksparrow

What about tests?

See PR description:

Surprisingly, all existing tests are passing with my change in place, so I didn't make any changes in the test suite. Please let me know if you think I should write a new test 🤷🏻

Do you have any suggestions on where to put the new tests?

@hacksparrow
Copy link
Contributor

@bajtos A test showing the expected behavior for invalid parameter values. If the code works the same for "valid" and "invalid" values, it means there is no distinction between them for the program.

It seems to me, we'll have to dig deeper and make some changes somewhere, and add a test that will fail for invalid values.

Rework the validation code to use exiting `RestHttpErrors.invalidData`
error instead of a custom `BadRequest` error. This way the error object
includes the parameter name in the error message & properties and has
a machine-readable `code` property.

Signed-off-by: Miroslav Bajtoš <[email protected]>
@bajtos bajtos force-pushed the fix/query-param-validation-errors branch from 28d375f to 84f3d7f Compare August 17, 2020 14:53
@bajtos
Copy link
Member Author

bajtos commented Aug 17, 2020

@bajtos bajtos merged commit 54f762c into master Aug 17, 2020
@bajtos bajtos deleted the fix/query-param-validation-errors branch August 17, 2020 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature REST Issues related to @loopback/rest package and REST transport in general Validation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants