This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-11
Channels
- # announcements (1)
- # beginners (67)
- # calva (4)
- # cider (6)
- # clj-kondo (26)
- # clojure (61)
- # clojure-belgium (2)
- # clojure-sweden (1)
- # clojurescript (12)
- # community-development (27)
- # cursive (2)
- # datascript (4)
- # datomic (20)
- # emacs (4)
- # funcool (1)
- # graphql (11)
- # honeysql (3)
- # malli (15)
- # membrane (6)
- # nbb (4)
- # nextjournal (7)
- # pathom (8)
- # polylith (7)
- # rdf (1)
- # re-frame (1)
- # releases (2)
- # shadow-cljs (42)
- # specter (3)
- # tools-deps (25)
- # xtdb (17)
Hi,
We've a use case where we need to apply schema validation on the body based on the type
value in the header.
For example if the type is foo
then should apply schema/foo
. If the type is bar
then should apply schema/bar
.. dynamically
You can always add a middleware which adds it to the body from header before coercion
Thank you @UK0810AQ2..I'm new to malli & reitit
, can you please give me a reference or sample on middleware
I use malli for schema validation and reitit.swagger for API definition and routes.
["/dosomething"
{:swagger {:tags ["do something"]}
:post {:summary "route the call after validating the payload based on foo or bar"
:scope [:api:write]
:parameters {:header [:map
[:authorization schema/authorization]
[:type schema/type-header]] ;; foo or bar
:body [:map
[:events schema/general-body]]} ;; this is the part we need to call schema/foo or schema/bar based on type
:handler (h/process-api config)}}]
the part events
is a generic such that based on the type, we will apply one schema vs the other
oops.. sorry
(defn process-api [config]
(fn [{{:keys [header body]} :parameters}]
(let [events (body :events)
total (count events)]
(let [results (k/send-events config events header)
successful (count (remove :error results))]
{:status (cond
(zero? successful) 400
(= total successful) 200
:else 206)
:body {:events results}}))))
@UK0810AQ2, Thank you for your advice.. I resolved it.. this is what I did..
(defn process-api [config]
(fn [{{:keys [header body]} :parameters}]
(if-let [errors (m/explain i/custom-schema body)] ;; the additional validations
{:error 400
:body (-> errors m/humanize...)}
(let [events (body :events)
total (count events)]
(let [results (k/send-events config events header)
successful (count (remove :error results))]
{:status (cond
(zero? successful) 400
(= total successful) 200
:else 206)
:body {:events results}})))))
This is the very much not what I suggested You should build a full reitit ring handler like in the examples in the reitit repo, then add a middleware to add the parameter from the header to the body
oops!!