Fork me on GitHub
#liberator
<
2022-04-15
Nundrum21:04:14

I feel I'm missing something. Can I use :malformed? to check a json POST with spec? And if so, is there some way to only parse the body once and make it available to :post! ?

ordnungswidrig22:04:32

I'd rather use :processible instead of malformed. Every decision method can return an updated context, or just a map which will be merged with the context. E.f.

(fn [ctx] 
  (let [[err value] (-> ctx :request :body (coerce some-schema))
    (if value 
       {:entity value}
       [false {:error err}])))
With some hypothetical function coerce. Any map value will habe handled like true for decision function. In the handlers you can extract the entity or error value from the context as you like.

Nundrum22:04:01

Ohhhhh? So [false {:error err}] is a vector of the return code and the map to merge into the context?

Nundrum22:04:58

I was trying to return just a map and that caused a "bad request".

Nundrum22:04:23

Which makes sense for malformed? because a map would be true. IOW: yes, it's malformed.

ordnungswidrig22:04:16

From the docs >Liberator merges a map that is returned from a decision function with the current context. This way we can easily store the value for the choice and retrieve it later in the handle-ok function: (get ctx :choice).

Nundrum22:04:48

Thanks!! I was working down the tutorial so I hadn't seen that yet, and didn't catch what was happening in the "putting it all together" section.