This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-28
Channels
- # anglican (2)
- # announcements (2)
- # babashka (41)
- # babashka-circleci-builds (1)
- # beginners (45)
- # brompton (2)
- # calva (13)
- # cider (13)
- # clj-kondo (14)
- # cljsrn (6)
- # clojure (60)
- # clojure-europe (4)
- # clojure-nl (3)
- # clojurescript (1)
- # code-reviews (3)
- # conjure (2)
- # cryogen (5)
- # datomic (22)
- # fulcro (13)
- # graalvm (19)
- # introduce-yourself (1)
- # kaocha (2)
- # leiningen (2)
- # lsp (1)
- # malli (4)
- # meander (1)
- # music (1)
- # news-and-articles (2)
- # off-topic (17)
- # polylith (6)
- # practicalli (2)
- # react (15)
- # reactive (1)
- # reagent (8)
- # reitit (2)
- # releases (1)
- # rum (10)
- # shadow-cljs (6)
- # spacemacs (4)
- # sql (2)
- # tools-deps (4)
- # xtdb (21)
Hi guys (kinda new to malli), when walking the schema, how would I walk the schema and transform the value of each node with custom logic? I've looked at the src of the json-transformer, etc but they go over my head at the moment 😞 e.g.
(def my-schema [:map
[:a [string? {:custom/upper}]]
[:b [int? {:custom/square}]]])
(def my-data {:a "a", :b 2})
;; Transform, key :a to be upper-cased and :b squared
{:a "a", :b 5} => {:a "A", :b 25}
But that's just my opinion; perhaps other #malli users will weigh-in with their own perspectives :)
nice I came to ask pretty much this, I have a malli spec which takes keywords and goes through the default json transformer and failes because its a string, looks like I can just add :decode/json {:leave keyword} to the spec and it should transform to the correct format
Hmmm, the default interceptor transformers (which I admit I have not tried to customize) do not behave as I would have expected:
(m/decode [int? {:decode/json {:leave 'inc}}] 5 (mt/json-transformer))
;; => 6
(m/decode [int? {:decode/json {:leave 'inc}}] "5" (mt/json-transformer))
;; (expected) error! int is a valid json value, so should not have been represented as string
(m/decode [int? {:decode/string {:leave 'inc}}] 5 (mt/string-transformer))
;; => 6
(m/decode [int? {:decode/string {:leave 'inc}}] "5" (mt/string-transformer))
;; (unexpected) error! I would have expected the default string enter/leave interceptors to run by now, so we can `(inc 5)
`