This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-07-13
Channels
- # babashka (5)
- # beginners (55)
- # calva (24)
- # cider (11)
- # cljsrn (14)
- # clojure (55)
- # clojure-europe (9)
- # clojure-houston (1)
- # clojure-italy (1)
- # clojure-nl (7)
- # clojure-uk (38)
- # clojurescript (4)
- # code-reviews (12)
- # cursive (3)
- # datomic (86)
- # figwheel-main (14)
- # helix (10)
- # hoplon (4)
- # interceptors (1)
- # jobs (2)
- # jobs-discuss (13)
- # kaocha (5)
- # leiningen (5)
- # local-first-clojure (1)
- # luminus (2)
- # malli (27)
- # membrane (1)
- # off-topic (43)
- # pedestal (5)
- # re-frame (32)
- # reagent (22)
- # reitit (24)
- # remote-jobs (2)
- # shadow-cljs (1)
- # spacemacs (1)
- # sql (1)
- # tools-deps (32)
- # xtdb (51)
I enhanced the Malli/Fork integration demo to use idiomatic keyword maps in the re-frame app-db and string keyword for Fork forms. @lucio
It has raised another question. @ikitommi when you have a chance, can you comment on the 2 questions?
@steveb8n sure:
1. m/explainer
returns a optimized pure function for explaining, e.g:
(m/explainer [:map [:x int?]])
2. mt/transformer
can be used to compose transformers, runs them in single sweep. Also, you could store the computed m/decoder
:
(m/decoder
[:map [:x int?]]
(mt/transformer (mt/key-transformer {:decode keyword}) (mt/string-transformer)))
thanks. for #2 I tried the composition via mt/transformer but couldn’t get it to transform properly. I am adding complexity by using it with a “multi-schema”
at best, the m/decoder
(or m/encoder
) know there is nothing to do and return identity
:
(m/decoder
[:map [:x int?]]
(mt/transformer
(mt/json-transformer)
(mt/json-transformer)
(mt/json-transformer)
(mt/json-transformer)))
; => #object[clojure.core$identity]
If you have and example of multi-schema that doesn’t work, please share, I could take a look
also, as the transformers are mounted from root schema towards leafs, the :multi
decoding is applied before it’s childs (as we don’t know which child is selected), so you might need to decode the :multi
dispatch key manually.
there is an example in the README:
(m/decode
[:multi {:dispatch :type
:decode/string '#(update % :type keyword)}
[:sized [:map [:type [:= :sized] [:size int?]]]
[:human [:map [:type [:= :human]] [:name string?] [:address [:map [:country keyword?]]]]]]
{:type "human"
:name "Tiina"
:age "98"
:address {:country "finland"
:street "this is an extra key"}}
(mt/transformer mt/strip-extra-keys-transformer mt/string-transformer))
;{:type :human
; :name "Tiina"
; :address {:country :finland}}
I’ll get back to you. making this work makes the fork/malli integration nice and idiomatic on both sides
oh, noticed you have the custom error message for :enum
: {:error/message "Must be London or Tampere"}
. Enums have something decend nowadays by default: https://github.com/metosin/malli/blob/master/src/malli/error.cljc#L63-L66
My guess is that you should have a new named transformer, something like:
(mt/transformer
{:name :before}
(mt/key-transformer {:decode keyword})
(mt/string-transformer))
and in the :multi
:
[:multi {:dispatch :type
:decode/before '#(update % :type keyword)} ...