This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-04-16
Channels
- # announcements (1)
- # architecture (319)
- # babashka (27)
- # beginners (101)
- # biff (1)
- # calva (30)
- # cider (6)
- # clj-kondo (38)
- # clojure (41)
- # clojure-boston (1)
- # clojure-europe (80)
- # clojure-nl (1)
- # clojure-norway (21)
- # clojure-uk (4)
- # community-development (7)
- # conjure (1)
- # data-science (18)
- # datalevin (6)
- # datascript (30)
- # datomic (2)
- # events (2)
- # fulcro (1)
- # graalvm (3)
- # holy-lambda (2)
- # hyperfiddle (10)
- # jobs (3)
- # lsp (2)
- # malli (9)
- # matcher-combinators (3)
- # missionary (72)
- # nbb (40)
- # off-topic (1)
- # other-languages (14)
- # planck (5)
- # re-frame (2)
- # releases (4)
- # rewrite-clj (22)
- # shadow-cljs (3)
- # sql (2)
- # squint (17)
- # yamlscript (1)
How can i use malli to validate if the value of a map entry is a valid malli schema?
you can call (m/schema value)
on it at least:
(def Schema [:fn {:error/fn
(fn [{:keys [value]} _]
(str value " is not a valid Schema"))}
(m/-safe-pred m/schema)])
(->> :int (m/explain Schema) (me/humanize))
; => nil
(->> :intz (m/explain Schema) (me/humanize))
; => [":intz is not a valid Schema"]
Specifically i want to check if the form is a valid map schema form
I am building a schema based on an external standard that makes a lot of use of inheritance. Certain objects inherit from parent definitions, optionally adding additional attributes. By preference I am using a local registry ("the data way") and would like to be able to capture these relationships in the registry definitions. Naively I imagined I could do something similar to this simplified example:
(def MySchema
[:schema {:registry {::parent [:map
[:x string?]
[:y number?]]
::child [:merge
[:ref ::parent]
[:map
[:z boolean?]]]}}
[:map
[:foo ::parent]
[:bar ::child]]])
It seems as though I can't use :merge
within the registry, however. I expect I could extract these definitions into separate vars and combine them with (merge)
, but perhaps I am missing a more idiomatic approach? Any advice appreciated.I have an experimental branch with map-like schema supporting :extends
and :abstract
.
also, you can use :merge
with registry, just have to ensure :merge
etc. utility schemas are available:
;; add utility schema to global registry
(mr/set-default-registry!
(mr/composite-registry
(m/default-schemas)
(mu/schemas)))
(def MySchema
(m/schema
[:schema {:registry {::parent [:map
[:x string?]
[:y number?]]
::child [:merge
[:ref ::parent]
[:map
[:z boolean?]]]}}
[:map
[:foo ::parent]
[:bar ::child]]]))
(mg/sample MySchema)
;({:foo {:x "", :y 2.0}, :bar {:x "", :y -1.0, :z true}}
; {:foo {:x "7", :y -1.0}, :bar {:x "X", :y -0.5, :z true}}
; {:foo {:x "", :y -1}, :bar {:x "", :y 2.0, :z false}}
; {:foo {:x "K", :y 0.75}, :bar {:x "w6", :y 0, :z false}}
; {:foo {:x "", :y 2.5}, :bar {:x "T", :y -1.125, :z false}}
; {:foo {:x "y", :y -11}, :bar {:x "po4", :y 0, :z false}}
; {:foo {:x "0n", :y 1.0}, :bar {:x "36", :y -2.0, :z true}}
; {:foo {:x "8xc", :y -0.875}, :bar {:x "", :y -1.40625, :z false}}
; {:foo {:x "zwSC", :y 51}, :bar {:x "2bHg50G", :y 0.25, :z false}}
; {:foo {:x "oS5", :y 92}, :bar {:x "75rj74", :y -0.453125, :z false}})
Thanks for the info @U055NJ5CC! I'll give this a closer look but at a glance it looks correct.
you can call (m/schema value)
on it at least:
(def Schema [:fn {:error/fn
(fn [{:keys [value]} _]
(str value " is not a valid Schema"))}
(m/-safe-pred m/schema)])
(->> :int (m/explain Schema) (me/humanize))
; => nil
(->> :intz (m/explain Schema) (me/humanize))
; => [":intz is not a valid Schema"]