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
Implement malli-based coercion #341
Conversation
+1 for striping extra keys both ways. Also I suggest we do that as the default transformer so that users can override it if the want to. |
Just testing this out on my own project. A few things of note:
|
Added also an option |
stripping extra keys on return by default for all schemas... not sure if that is a good property. |
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.
Looks good! One thing I'd like to suggest is (and perhaps this is more of a malli issue) that errors be in a consistent shape. Right now errors are a single error string in the case that there is only one, and a vector if there are many. This forces the work of always remembering to handle the differing shapes onto the client (in this case, the web client that consumes the error response JSON).
In general I think it's easier to consistently handle a more complex shape (eg. a collection) than having to handle both a complex shape and a single shape.
As I said, this might be more of a malli issue, but seeing it in the reitit web response made me recall it.
Re: stipping by default - I think it's a good default. Without default stripping a careless developer could accidentally send sensitive data (eg. :user/encrypted-password
) without intending to. If someone wants to override the behavior, they specify their own :transformers
.
Thanks for the comments.
1) By default, only strip extra keys from
|
My personal taste would be to lean towards #1. Making closed schemas a first-class citizen in malli seems most useful as it allows the validators / encoders / decoders to be used at the serialization boundaries of an application, which would be great. Similarly, adding the option ( We could also have a function |
Let's go with 1, related PR on malli: metosin/malli#156 |
Default options are now: (def default-options
{:transformers {:body {:default default-transformer-provider
:formats {"application/json" json-transformer-provider}}
:string {:default string-transformer-provider}
:response {:default default-transformer-provider}}
;; set of keys to include in error messages
:error-keys #{:type :coercion :in :schema :value :errors :humanized #_:transformed}
;; schema identity function
:compile mu/closed-schema
;; strip-extra-keys (effects only default transformers!)
:strip-extra-keys true
;; add default values
:default-values true
;; malli options
:options nil}) we have an internal review of malli on friday, small tuning possible. |
TODO
Current defaults:
comments welcome on options & defaults