-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
export function invalidRequestBody(): HttpErrors.HttpError { | ||
|
||
export function invalidRequestBody( | ||
details: ValidationErrorDetails[], |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
What about tests? |
See PR description:
Do you have any suggestions on where to put the new tests? |
@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]>
28d375f
to
84f3d7f
Compare
@hacksparrow added a new test, see https://github.com/strongloop/loopback-next/pull/6105/files#diff-f1805dfae45a4ba243a038f5794b800cR257 LGTY now? |
Rework the validation code to use exiting
RestHttpErrors.invalidData
error instead of a customBadRequest
error. This way the error object includes the parameter name in the error message & properties and has a machine-readablecode
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 machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈