Fork me on GitHub
#reitit
<
2020-04-22
>
ccann14:04:12

Can anyone point me in the right direction for coercing a request parameter that has the inst? spec attached? We’re using json-transformer and the payload is JSON

jaime18:04:10

Do you have the middleware and coercion in the route data? Then I think you just need to have the request header content-type to "application/json"?

ikitommi04:04:03

yes, it should just work if you have the coercion configured on. examples have good starting point with the needed mw configuration (and to test stuff)

firstclassfunc18:04:58

hello all. Can't seem to find in the documentation how to declare optional query parameters by default all parameters are defined as required

{:swagger {:tags ["path"]}
          :get     {:summary    "Forward Path Search"
                    :parameters {:query {:src string? :dst string? :network string?}
                                 }
                    :handler    (fn [{{{:keys [src dst network]} :query} :parameters}]
                                  (path/path-search-handler src dst network))
                    }}

4
jaime18:04:03

I'm trying to learn spec myself as well, it may not be the best solution. Assuming src and dst are required, and network as optional.

(s/def ::src string?)
(s/def ::dst string?)
(s/def ::network string?)

# Then define parameters as
{:parameters {:query (s/keys :req-un [::src ::dst] :opt-un [::network])}
EDIT: s here is
[clojure.spec.alpha :as s]

firstclassfunc18:04:43

ahh that worked

"in": "query",
"name": "network",
"description": "",
"type": "string",
"required": false

jaime18:04:54

glad to help 🙂

ikitommi04:04:52

you can also use ds/opt if you use the data-spec syntax, see https://cljdoc.org/d/metosin/spec-tools/CURRENT/doc/data-specs

ikitommi04:04:30

a doc PR would be awesome to have an example of optional parameter, in both ways. Would anyone have time for that?

👍 4
jaime08:04:34

@U055NJ5CC I'll review the docs, and see if I can contribute an example. Just for clarification, what do you mean by "in both ways"?

ikitommi08:04:58

looking forward! with both way, I mean - using data-specs and normal spec1 keys syntax for optional keys

ikitommi08:04:54

(optional keys should be documented also for plumaric schema and malli, but that’s a separate thing)

jaime08:04:10

ok got it!

emil0r09:04:34

Funny when you join a channel to ask a question, and it’s just been answered 🙂

amarus23:04:02

Hello! Can I use clj-ajax with reitit to get data from backend or I must use reitit on both frontend and backend? Sorry for the probably noobish question, but I'm really tired of all this frontend stuff 😓