This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-09
Channels
- # announcements (14)
- # architecture (42)
- # babashka (23)
- # beginners (37)
- # biff (8)
- # calva (2)
- # cider (3)
- # clara (42)
- # clerk (14)
- # clojure (55)
- # clojure-brasil (3)
- # clojure-dev (5)
- # clojure-europe (18)
- # clojure-hungary (88)
- # clojure-losangeles (3)
- # clojure-nl (1)
- # clojure-norway (66)
- # clojure-uk (9)
- # clojurescript (16)
- # core-logic (16)
- # datomic (6)
- # fulcro (32)
- # hyperfiddle (25)
- # instaparse (12)
- # joyride (2)
- # lsp (13)
- # malli (15)
- # off-topic (50)
- # polylith (11)
- # portal (3)
- # re-frame (2)
- # reitit (2)
- # sci (8)
- # shadow-cljs (16)
- # tools-deps (13)
- # xtdb (5)
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.
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
If you don't, it will just validate any map, not imposing the need for it to be empty
Not sure about the transformers part, though.
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
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}
(modulo the UUID being different, of corse)
Just to check, which version of Malli are you running?
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
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.
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}]})