Fork me on GitHub
#malli
<
2023-11-09
>
Akiz08:11:34

Hi, how would you describe a hash-map that can be empty or has the exact structure? This [:or [:map [:id :uuid] [:name string?] [:age integer?]] [:map]] doesn’t work well with transformers.

Martín Varela09:11:55

If you want the map to be empty, you could make it closed:

(m/validate [:map {:closed true}] {})
;; => true

(m/validate [:map {:closed true}] {:a :b})
;; => false

Martín Varela09:11:30

If you don't, it will just validate any map, not imposing the need for it to be empty

Martín Varela09:11:02

Not sure about the transformers part, though.

Akiz13:11:24

Sorry, I left it out because It doesn’t make any difference. (malli.core/encode [:or [:map {:closed true} [:id :uuid] [:name :string] [:age :int]] [:map {:closed true}]] {:id (random-uuid) :name "john" :age 15} malli.transform/json-transformer) => {:id #uuid "e707c673-61c3-42f6-b5ad-94b6d42dfe73", :name "john", :age 15} and (malli.core/encode [:map {:closed true} [:id :uuid] [:name :string] [:age :int]] {:id (random-uuid) :name "john" :age 15} malli.transform/json-transformer) => {:id "d9073882-8310-426f-85c9-c8263569b37f", :name "john", :age 15} In second case, the UUID get’s transformed

Martín Varela13:11:55

For me it results in the same (json-encoded) value:

(malli.core/encode [:or
                    [:map {:closed true} [:id :uuid] [:name :string] [:age :int]]
                    [:map {:closed true}]]
                   {:id (random-uuid) :name "john" :age 15}
                   malli.transform/json-transformer)
;; => {:id "b1c74ece-f881-4a8f-8a19-a572a7a4d1bc", :name "john", :age 15}

(malli.core/encode [:map {:closed true} [:id :uuid] [:name :string] [:age :int]] {:id (random-uuid) :name "john" :age 15}
                   malli.transform/json-transformer)
;; => {:id "defafe57-e589-4e7b-9e57-ea9fe413f3bb", :name "john", :age 15}

1
Martín Varela13:11:31

(modulo the UUID being different, of corse)

Martín Varela13:11:33

Just to check, which version of Malli are you running?

Akiz14:11:12

i am on old version probably, thanks for head up. I will try to upgrade.

Martín Varela16:11:39

Not sure if it's that (I ran this on 0.12), but the exact code you had pasted worked for me. Maybe try upgrading, just in case

Akiz08:11:03

Yeah, it was really the old version 🙂!

Martín Varela08:11:34

Awesome! Glad it's sorted 🙂

👍 1
hadils17:11:15

Hi! I am programming a GraphQL interface in Clojurescript. I have a recursive schema for my data structures in Malli, and I wanted to use multi-schemas as well, to select the correct starting point for a recursive schema. How would i go about doing this? I appreciate any comments or feedback offered.

dvingo20:11:32

Something like this?

(m/validate
  (m/schema
    [:schema {:registry {"top-level"
                         [:multi {:dispatch :type}
                          [:one
                           [:map
                            [:type [:= :one]] 
                            [:one-prop :string]
                            [:children {:optional true} [:vector [:ref "top-level"]]]]]
                          [:two
                           [:map
                            [:type [:= :two]]
                            [:two-prop :int]
                            [:children {:optional true} [:vector [:ref "top-level"]]]
                            ]]]}}
     [:ref "top-level"]]
    )
  {:type :one :one-prop "hi" :children [{:type :two :two-prop 400}]})