This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-04
Channels
- # announcements (7)
- # babashka (32)
- # beginners (106)
- # bristol-clojurians (10)
- # cider (6)
- # clj-kondo (5)
- # cljdoc (10)
- # clojure (110)
- # clojure-australia (10)
- # clojure-dev (6)
- # clojure-europe (12)
- # clojure-nl (2)
- # clojure-norway (16)
- # clojure-spec (9)
- # clojure-uk (59)
- # clojurescript (105)
- # community-development (2)
- # conjure (46)
- # cursive (12)
- # data-science (1)
- # datalog (26)
- # datomic (37)
- # docker (4)
- # emacs (10)
- # events (1)
- # fulcro (8)
- # graalvm (2)
- # jobs (1)
- # jobs-discuss (1)
- # malli (24)
- # meander (13)
- # off-topic (52)
- # pathom (4)
- # polylith (17)
- # proletarian (4)
- # react (1)
- # rewrite-clj (4)
- # shadow-cljs (56)
- # sql (21)
- # xtdb (14)
(let [schema [:multi {:dispatch first}
[:human [:cat [:= :human]]]
[:bear [:cat [:= :bear] [:* :int]]]
[::m/default [:tuple :string :string]]]]
(testing "validate"
(is (m/validate schema [:human]))
(is (m/validate schema [:bear 1 2 3]))
(is (m/validate schema ["defaultit" "toimii"]))
(is (not (m/validate schema [:so :invalid]))))
(testing "explain"
(is (not (m/explain schema [:human])))
(is (not (m/explain schema [:bear 1 2 3])))
(is (not (m/explain schema ["defaultit" "toimii"])))
(is (results= {:schema schema,
:value [:so :invalid],
:errors [{:path [::m/default 0], :in [0], :schema :string, :value :so}
{:path [::m/default 1], :in [1], :schema :string, :value :invalid}]}
(m/explain schema [:so :invalid]))))
(testing "parser"
(is (= (miu/-tagged :human [:human]) (m/parse schema [:human])))
(is (= (miu/-tagged :bear [:bear [1 2 3]]) (m/parse schema [:bear 1 2 3])))
(is (= (miu/-tagged ::m/default ["defaultit" "toimii"]) (m/parse schema ["defaultit" "toimii"])))
(is (= ::m/invalid (m/parse schema [:so :invalid])))))
I've been using REBL recently and I noticed that Malli schemas are printed as regular one line strings, which is annoying. Although I found it is possible to provide custom viewer to REBL. Example:
(defn schema? [s]
(m/schema? (try (m/schema s) (catch Exception _))))
(defn setup-rebl-viewers []
(require '[cognitect.rebl])
(cognitect.rebl/update-viewers
{:malli/schema {:pred #'schema?
:ctor (fn [v]
(-> (m/form (m/schema v))
(fipp.edn/pprint {:width 80})
(with-out-str)
(cognitect.rebl.renderers/string-code-viewer)))}}))
#_(setup-rebl-viewers)
This is just a simple pretty print, but we could apply other strategies for formatting.
If you have an idea what's the best way of formatting Malli schemas, please let me know in the comment.Bit of black magic:
(require '[clojure.core.protocols :as p]
'[malli.core :as m])
(extend-protocol p/Datafiable
Object
(datafy [x]
(cond
(satisfies? m/Schema x)
(do
(extend-protocol p/Datafiable
(class x)
(datafy [y] (m/-form y)))
(p/datafy x))
(satisfies? m/IntoSchema x)
(do
(extend-protocol p/Datafiable
(class x)
(datafy [y] (m/-form (m/schema y))))
(p/datafy x))
:else x)))
I think it could be simplified to
(extend-protocol p/Datafiable
Object
(datafy [x]
(cond
(satisfies? m/Schema x) (p/datafy (m/form x))
(satisfies? m/IntoSchema x) (p/datafy (m/form (m/schema x)))
:else x)))
Still as I mentioned in the other post (https://clojurians.slack.com/archives/C03S1KBA2/p1628084493261600?thread_ts=1628082589.258900&cid=C03S1KBA2), I've got a feeling extending Object is not a good idea š¬Thanks for posting example. Still I think that would be yet another extreme if I add my own m/schema to keep it everywhere...
Actually, maybe this should be added to Malli itself, to m/schema
:thinking_face:
What do you think @U055NJ5CC about making Malli schemas datafy-able?
(it is just an idea, I'm not sure is it good or bad, i'm just trying to figure out best practices of using these new protocols for my own and in this very thread it stroke me Malli could be a good place to define that datafy behaviour)
Iām trying to make a recursive schema
doing something like this (simplified)
(malli/validate
[:vector {:registry {::property
[:map
[:property/type keyword?]
[:property/group {:optional true}
[:vector ::property]]]}}
::property]
[{:property/type :string}
{:property/type :key}])
but I get a stackoverflowerror
Iām not sure model this kind of thing correctly
oh I see it, i need :ref
?
Bit of black magic:
(require '[clojure.core.protocols :as p]
'[malli.core :as m])
(extend-protocol p/Datafiable
Object
(datafy [x]
(cond
(satisfies? m/Schema x)
(do
(extend-protocol p/Datafiable
(class x)
(datafy [y] (m/-form y)))
(p/datafy x))
(satisfies? m/IntoSchema x)
(do
(extend-protocol p/Datafiable
(class x)
(datafy [y] (m/-form (m/schema y))))
(p/datafy x))
:else x)))