Fork me on GitHub
#malli
<
2022-03-10
>
Célio13:03:06

Maybe I’m doing it wrong, but is there a reason why transformers don’t handle enums?

Célio13:03:33

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

Ben Sless14:03:01

Try decode/json ?

☝️ 1
👍 1
Célio16:03:54

Thanks, that works.

👍 1
diego.videco21:03:19

Hello everyone. What’s the best way to use malli for generative testing?

👀 1
Felipe12:03:58

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

diego.videco18:03:12

Thanks @UA2U3KW0L I’ll try that.