liberator

Nundrum 2022-04-15T21:56:14.563479Z

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! ?

ordnungswidrig 2022-04-15T22:15:32.275039Z

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.

Nundrum 2022-04-15T22:17:01.049029Z

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

Nundrum 2022-04-15T22:17:58.817239Z

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

Nundrum 2022-04-15T22:18:23.632779Z

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

ordnungswidrig 2022-04-15T22:19:06.074439Z

Exactly.

ordnungswidrig 2022-04-15T22:20:16.031659Z

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

ordnungswidrig 2022-04-15T22:20:51.574009Z

This page should cover it in detail http://clojure-liberator.github.io/liberator/doc/execution-model.html

Nundrum 2022-04-15T22:27:48.571959Z

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.