Hi all!
I’m using Malli to decode and validate some data containing datetimes, and I’m a bit stuck trying to figure out how to write a customer transformer to deal with them. I’m using tick to deal with the datetimes themselves.
Here’s what I’ve got so far:
(require '[malli.core :as m]
'[malli.transform :as mt]
'[tick.core :as t])
;; Malli transformer for datetime strings; see
;; https://github.com/metosin/malli/issues/49#issuecomment-763957635
(def date-time-transformer
(mt/transformer
{:encoders {:date-time {:compile (fn [_ _] {:enter str})}}}
{:decoders {:date-time {:compile (fn [_ _] {:enter t/date-time})}}}))
(def schema-registry
{:registry {:date-time (m/-simple-schema {:pred t/date-time?
:type :date-time})}})
(m/decode :date-time
"2021-03-29T10:00:00"
schema-registry
date-time-transformer)
;; => #time/date-time "2021-03-29T10:00"
(m/encode :date-time
#time/date-time "2021-03-29T10:00:00"
schema-registry
date-time-transformer)
;; => "2021-03-29T10:00"
All good! But when I try sticking my datetime in a vector:
(m/decode [:catn [:timestamp :date-time]]
["2021-03-29T10:00:00"]
schema-registry
date-time-transformer)
I get an exception:
1. Unhandled clojure.lang.ExceptionInfo
:malli.core/invalid-schema {:schema :catn}
{:type :malli.core/invalid-schema,
:message :malli.core/invalid-schema,
:data {:schema :catn}}
My guess is one of three things is going wrong:
- My transformer needs to handle stuff that isn’t
:date-time
- My custom registry also needs to be merged with the default registry instead of replacing it
- Something else
If someone can help nudge me in the right direction and/or point me to some documentation of how to write a custom transformer, I’d be really grateful!