malli

https://github.com/metosin/malli :malli:
pithyless 2020-06-11T07:22:58.020100Z

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?

ikitommi 2020-06-11T10:41:10.022200Z

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.

ikitommi 2020-06-11T10:42:03.023200Z

in reitit-malli, there is a Corcion protocol and impl which does transform + validate for requests and transform + validate + transform for responses.

ikitommi 2020-06-11T10:42:46.024Z

for responses, it’s basically doing: 1. apply defaults, strip extra keys 2. validate the result 3. encode to whatever (string, json, xml)

ikitommi 2020-06-11T10:43:08.024400Z

If you have some minimal sample, could take a look.

pithyless 2020-06-11T11:23:05.028100Z

^ 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.)

ikitommi 2020-06-11T11:33:27.031400Z

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.

👍 1