reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
ben741 2021-04-20T13:22:42.126500Z

Hi, I might be missing something obvious, but how can I make query parameters optional in reitit.coercion.spec/coercion? i.e. I'd like to accept both requests to /images but also /images?subject=cat . Something like :query {:subject (s/nilable string?)} still just gives a 400 bad request because :subject is a required key.

jumar 2021-04-20T13:25:10.126800Z

Use s/?

ben741 2021-04-20T13:44:19.127700Z

Ah, I had to use spec-tools.data-spec and :query {(ds/opt :subject) string?}

2021-04-20T14:22:39.128400Z

I think you could use :req-un and :opt-un keys with spec coercion

athomasoriginal 2021-04-20T15:11:41.130400Z

^^ Indeed. It wasn’t immediately intuitive to me either, but this works

(s/def ::param boolean?)

(s/def ::optional-params
  (s/keys :opt-un [::param]))

["/your-route"
  {:name :blah
   :parameters {:query ::optional-params}}]

"/your-route?param=true"

ben741 2021-04-20T20:03:16.133700Z

From the Reitit source it looks like that should work, but it doesn't for me for some reason. Anyways, it also looks like directly using a spec there would let in any other unexpected params without coercing them, which isn't necessarily what I want.