Fork me on GitHub
#malli
<
2020-03-20
>
eskos10:03:21

General consensus on whether this should work or not?

(require '[malli.util :as mu])
(mu/get-in [:map [keyword? int?]] [:a])
;=> returns nil, but I expected int?
I have nested maps with a few generic bits (my use case doesn’t allow composing schemas from reusable parts, yet) and noticed this behavior. Considering it’s supposed to mirror https://clojuredocs.org/clojure.core/get-in in functionality I’m hesitant to call this a bug, just wondering out loud… 🙂

ikitommi10:03:31

(m/validate [:map [keyword? int?]] {:a 1})
; => false

(m/validate [:map [keyword? int?]] {keyword? 1})
; => true

ikitommi10:03:44

do you mean :map-of here?

eskos11:03:04

Hm, I think I broke something else in the process as well… a moment 🙂

eskos11:03:55

Yes indeed, :map-of

eskos11:03:14

This of course changes the nature of the error, although I’m still not willing to call this a bug; I’m kind of hoping for magic which obviously isn’t there… :)

=> (mu/get-in [:map-of keyword? int?] [:a])
Execution error (IllegalArgumentException) at malli.core/fn$G (core.cljc:26).
No implementation of method: :-get of protocol: #'malli.core/LensSchema found for class: malli.core$_map_of_schema$reify$reify__692

ikitommi13:03:34

would you like to do a PR of making :map-of implement the LensSchema protocol?

eskos14:03:05

So had a quick stab at it, -get is literally just

(-get [_ key default] (if (key-valid? key) value-schema default))
but -set is a lot harder as semantically it doesn’t really make sense as the result should probably be some kind of weirdo map schema, eg.
(mu/assoc [:map-of keyword? int?] :foo boolean?)
;=> should probably create
[:or
 [:map [:foo boolean?]]
 [:map-of keyword? int?]]
and that wouldn’t work for subsequent calls, assoc-in etc… One way to do this would be to allow :map to have default validators for kv pairs, effectively :map-of within :map, but that’s way too much work and decisions to do at this point of week 😅

eskos08:03:15

Coming back to this, would it make sense to have it as an option for open maps that one could specify default k/v validators for :map schema? This way the -get could work if the given validator matches and -set would switch the default k/v validator to whatever is given if it doesn't match any of the actual keys.

eskos13:03:25

Hmm, I could take a look at it at least. Haven’t delved into malli code base that much yet so no promises on that side but what the hey, it’s not like I’d have any other places to be… 😄

ikitommi13:03:24

maybe we should do a form-heavy work project together and finalize all the things 😉

4