-
Notifications
You must be signed in to change notification settings - Fork 257
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
Optionally coerce all params even if one fails. #363
base: master
Are you sure you want to change the base?
Optionally coerce all params even if one fails. #363
Conversation
This commit adds a `:reitit.coercion/coerce-all-on-error?` option that ensures that all parts of requests are coerced even if one fails. Previously, only the first coercion failure would be returned. Since the current coercion exception ex-data format can't really be used to accomodate multiple errors, this format changes from: `{ "in": [ "request", "path-params" ], "errors": ... }` ..to: `{ "path-params": { "errors": ... }, "body": { "errors": ... } ...}` when `:coerce-all-on-error?` has been set. It probably makes sense to switch to the new format even when `coerce-all-on-error?` is disabled, as well as and enable `coerce-all-on-error?` by default at some point. However, it's probably best to hold off on this until 1.0.0, since the format change means it would break backwards compatability for downstream middleware and interceptors. I also made sure that response coercion behaves the same way as request coercion wrt the new option, even though there's only one supported response coercion type (body) currently. I also added a missing `:` in the `::keys` option destructuring in `response-coercer` (previously, users would have had to set `:reitit.coercion/extract-request-format` for requests but `:extract-response-format` for responses).
(update :coercion -get-name) | ||
(->> (-encode-error (:coercion data))))) | ||
(if (not-any? #(contains? data %) | ||
[:body :form-params :query-params :headers :path-params]) |
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.
This might not be the cleanest way to tell the old and new format apart. Perhaps it'd been better to handle this by adding an additional key to the ex-info
data?
Note: the tests seem to be failing on master, so I haven't actually run them on this branch. I can do a rebase, fix and force-push whenever master is working again. |
@@ -53,7 +55,27 @@ | |||
:in [:request in] | |||
:request request})))) | |||
|
|||
(defn ^:no-doc response-coercion-failed! [result coercion value request response] | |||
(defn ^:no-doc merge-coercion-errors | |||
"Coalesces a list of coercion errors on the form `{:in <>}`" |
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.
Reminder to self: fix this docstring.
This PR targets issue #362.
This commit adds a
:reitit.coercion/coerce-all-on-error?
option thatensures that all parts of requests are coerced even if one fails.
Previously, only the first coercion failure would be returned.
Since the current coercion exception ex-data format can't really be
used to accomodate multiple errors, this format changes from:
{ "in": [ "request", "path-params" ], "errors": ... }
..to:
{ "path-params": { "errors": ... }, "body": { "errors": ... } ...}
when
:coerce-all-on-error?
has been set.It probably makes sense to switch to the new format even when
coerce-all-on-error?
is disabled, as well as and enablecoerce-all-on-error?
by default at some point. However, it's probablybest to hold off on this until 1.0.0, since the format change means it
would break backwards compatability for downstream middleware and
interceptors.
I also made sure that response coercion behaves the same way as request
coercion wrt the new option, even though there's only one supported
response coercion type (body) currently.
I also added a missing
:
in the::keys
option destructuring inresponse-coercer
(previously, users would have had to set:reitit.coercion/extract-request-format
for requests but:extract-response-format
for responses).