This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-10
Channels
- # announcements (3)
- # asami (4)
- # babashka (21)
- # beginners (97)
- # calva (32)
- # cider (4)
- # clj-kondo (7)
- # cljdoc (1)
- # clojure (70)
- # clojure-europe (27)
- # clojure-nl (10)
- # clojure-norway (18)
- # clojure-uk (8)
- # clojure-ukraine (1)
- # clojurescript (5)
- # datalevin (7)
- # docker (1)
- # emacs (3)
- # fulcro (4)
- # girouette (4)
- # graalvm (2)
- # graphql (9)
- # gratitude (3)
- # honeysql (4)
- # hoplon (3)
- # hyperfiddle (7)
- # jobs (3)
- # kaocha (31)
- # lsp (23)
- # malli (7)
- # missionary (6)
- # nextjournal (9)
- # off-topic (6)
- # pathom (13)
- # polylith (13)
- # practicalli (3)
- # remote-jobs (3)
- # reveal (7)
- # schema (1)
- # sci (23)
- # shadow-cljs (31)
- # tools-deps (62)
- # xtdb (8)
An example with malli.transform/json-transformer
:
(require '[malli.core :as m])
(require '[malli.transform :as mt])
(def schema [:enum {:encode name :decode keyword} :a :b :c])
(m/encode schema :x mt/json-transformer)
;; => :x
(m/decode schema "x" mt/json-transformer)
;; => "x"
Then I created a custom json transformer based on malli.transform/json-transformer
where it adds codecs for :enum
:
(defn my-json-transformer
...
(mt/transformer
{:name :json
:decoders (-> (mt/-json-decoders)
...
(assoc :enum mt/-string->keyword))
:encoders (-> (mt/-json-encoders)
(assoc :enum m/-keyword->string))}))
(m/encode schema :x my-json-transformer)
;; => "x"
(m/decode schema "x" my-json-transformer)
;; => :x
if I’m not mistaken malli’s generators are compatible with clojure.test.check, which is what I’d use https://github.com/clojure/test.check
Thanks @UA2U3KW0L I’ll try that.