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

Add option to add default value to optional properties #646

Open
Deraen opened this issue Feb 17, 2022 · 5 comments
Open

Add option to add default value to optional properties #646

Deraen opened this issue Feb 17, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@Deraen
Copy link
Member

Deraen commented Feb 17, 2022

I have a case where I need default values to be added to the maps, even though the properties are optional.

Should be simple to add new condition to optional check to look at a new property, like :default/force or such, and then include default value if that is set, even if the property is optional.

https://github.com/metosin/malli/blob/master/src/malli/transform.cljc#L394

#351 #397

Some ideas for the option name

  • :default/always
  • :default/missing-key
  • :default/force
@Deraen Deraen added the enhancement New feature or request label Feb 17, 2022
@dharrigan
Copy link
Contributor

This would be great,

i.e., this presently doesn't work (whether optional or not)

(def locale-check [:map [:locale {:default "en_GB"} [:enum {:swagger/type "string"} "en_GB" "es_ES"]]])

(-> (c/explain locale-check {})
    (me/humanize)) ;; {:locale ["missing required key"]}

Funnily enough, decode does the right thing:

(c/decode locale-check {} mt/default-value-transformer) ;; {:locale "en_GB"}

ikitommi added a commit that referenced this issue Mar 20, 2022
@ikitommi
Copy link
Member

Are there really a use cases that the schema itself should control how to add the defaults? should it be recursive?

Simple solution would be add this option to mt/default-value-transformer to add the defaults.

@ikitommi
Copy link
Member

see #668

@Deraen
Copy link
Member Author

Deraen commented Mar 21, 2022

@dharrigan That is working as intended.

:default option doesn't do anything without default-value-transformer, so the explain wont see the default value if you didn't call decode to apply default-value-transformer first.
If you add :optional true to the property, you can see the decode doesn't add the default value.

@Deraen
Copy link
Member Author

Deraen commented Mar 21, 2022

@ikitommi Transformer level option probably makes sense. I think on the project where I had use for this, I used :default/force for every property which had :default value.

On Reitit I could also set :coercion route-data for specific route trees if I need to enable this only for certain routes.

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

No branches or pull requests

3 participants