Fork me on GitHub
#malli
<
2019-11-12
>
ikitommi17:11:00

two new PRs in master: support for two-way transformations (`m/decode` & m/encode), :sequential schema and allowing any schema properties based (serializable!) encoders & decoders

ikitommi17:11:38

(let [schema (-> [:and {:title "lower-upper-string"
                        :decode/string '(constantly str/upper-case)
                        :encode/string '(constantly str/lower-case)}
                  string?]
                 (malli.edn/write-string)
                 (malli.edn/read-string))]
  (as-> "kikka" $
        (m/decode schema $ mt/string-transformer)
        (doto $ prn)
        (m/encode schema $ mt/string-transformer)
        (doto $ prn)
        schema))
; prints "KIKKA"
; prints "kikka"
; => [:and
;     {:title "lower-upper-string"
;      :decode/string (constantly str/upper-case)
;      :encode/string (constantly str/lower-case)}
;      string?]

ikitommi17:11:16

The two-way thing looks from outside the same as with spec-tools, but it actually works 🙂

ikitommi17:11:37

… thanks to clear separation of transformation and validation. the encode mostly creates values that are not valid (e.g. a string representation of a date), and had to add all kind of hacks to make that work with clojure.spec.