Fork me on GitHub
#malli
<
2021-08-28
>
anonimitoraf06:08:32

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} 

pithyless08:08:44

But that's just my opinion; perhaps other #malli users will weigh-in with their own perspectives :)

oly15:08:16

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

pithyless08:08:05

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)
`