metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.44k stars 205 forks source link

Feature mu/assoc and mu/update-in change MapEntry properties #909

Open dspiteself opened 1 year ago

dspiteself commented 1 year ago

Our company generates a lot of spec from data definitions We us [malli.util :as mu] to update those specs with vendor specific information. Currently we have to use mu/transform-entries when we want to change MapEntry properties. It would be much more straight forward to our new users is they could use mu/assoc with another argument to set the the properties. I do not know how we could enhance mu/update to be able to update properties maybe using :malli.core/value but that would be breaking. We are getting by with mu/transform-entries but any feedback would be nice. We would be willing to make a patch.

ikitommi commented 11 months ago

you can use vector to set the properties with mu/assoc:

(mu/assoc [:map [:x :int] [:y :int]] [:z {:optional true}] :int)
; => [:map [:x :int] [:y :int] [:z {:optional true} :int]]
dspiteself commented 10 months ago

thanks

dspiteself commented 10 months ago

Would it be possible to make it work with a function?

(mu/assoc [:map [:x :int] [:y :int]] [:z (fn [props] (assoc props :optional true)) :int)
; => [:map [:x :int] [:y :int] [:z {:optional true} :int]]
dspiteself commented 10 months ago

If you are interested I could make a pull request to detect a function and apply instead of overwrite. https://github.com/metosin/malli/blob/8ef1f98e8dda3e94eaefe87d4cb3b3f47c36faec/src/malli/core.cljc#L397