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

Optionally coerce all params even if one fails. #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aeriksson
Copy link

This PR targets issue #362.

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).

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])
Copy link
Author

@aeriksson aeriksson Feb 9, 2020

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?

@aeriksson
Copy link
Author

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 <>}`"
Copy link
Author

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.

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

Successfully merging this pull request may close these issues.

1 participant