Fork me on GitHub
#malli
<
2023-10-23
>
hanDerPeder18:10:45

Running into an issue with map entry attributes in the map schema. I haven't studied the implementation here so maybe I'm in undefined behaviour land, but since this works:

(mg/generate
 [:map {:registry {:foo/bar :string}}
  :foo/bar]) ;; #:foo{:bar "mWG3BP81X49181eod38930Y122oV"}
and this works
(mg/generate
 [:map {:registry {:foo/bar :string}}
  [:foo/bar]]) ;; #:foo{:bar "7IuHp3NFf"}
I would expect this to work:
;; expecting this to work
(mg/generate
 [:map {:registry {:foo/bar :string}}
  [:foo/bar {:min 100 :max 101}]]) ;; #:foo{:bar "7pU1jsi6NEnBa30MBp"} <-- too short :(
works if I write it out as documented:
(mg/generate
 [:map {:registry {:foo/bar :string}}
  [:foo/bar [:foo/bar {:min 100 :max 101}]]]) ;; #:foo{:bar "q348W......"} <-- works
Is this a bug, undefined behaviour or just not implementing?

pithyless10:10:55

IIUC, in the third example, you are applying options to the key, not the value of the key-val association:

[:map [:foo/bar {:min 100 :max 101}]]
;; i.e.
 [:map [:foo/bar {:min 100 :max 101} :foo/bar]]
Malli already has some amount of convenience (ie. magic) with how it tries to interpret these things, e.g:
[:map [:foo/bar]]
;; i.e.
[:map [:foo/bar :foo/bar]]
I don't see how Malli could figure out you want the third version to apply to the value and not to the key (e.g. to set {:optional true})

pithyless10:10:03

(malli/ast
 [:map {:registry {:foo/bar :string}}
  [:foo/bar {:min 100 :max 101}]])
;; => {:type :map,
;;     :keys
;;     #:foo{:bar
;;           {:order 0,
;;            :value {:type :malli.core/schema, :value :foo/bar},
;;            :properties {:min 100, :max 101}}},
;;     :registry #:foo{:bar {:type :string}}}
^ yep, property of key not value

hanDerPeder16:10:25

Haven't used ast before, thanks!