I'm struggling to wrap my head around custom transformers and validators; is there some way to create a pipeline of validate -> transform -> validate (ala spec/conform
?) Or when defining a transform interceptor, can I in some way tell the the chain to stop executing further since some assumption is invalid?
currently no way to stop the chain in interceptors @pithyless, could be added easily with reduced
. There are Schemas that short-circuit in -transform
like the :or
.
in reitit-malli
, there is a Corcion
protocol and impl which does transform + validate for requests and transform + validate + transform for responses.
for responses, it’s basically doing: 1. apply defaults, strip extra keys 2. validate the result 3. encode to whatever (string, json, xml)
If you have some minimal sample, could take a look.
^ the reitit Coercer
case is the kind of thing I have in mind. I've been going back and forth on this, but malli has been forcing me to rethink my approach: perhaps, I should just assume there is only one canonical form of data inside my project runtime, and only at the boundaries do all the encoding/decoding via custom protocols (i.e. a custom Coercer protocol and implementation for reitit, datomic, etc.)
the reitit code is here: https://github.com/metosin/reitit/blob/master/modules/reitit-malli/src/reitit/coercion/malli.cljc. Basically, at route(r) creation time, for each Schema, a reified Coercion
is created, storing encoder
, decoder
, validator
and explainer
for that schema. At runtime, it’s just a calling those (optimized fns) in whatever order suits for the use case.