Fork me on GitHub

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.


Ah, I had to use and :query {(ds/opt :subject) string?}

Toni Vanhala14:04:39

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


^^ 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]))

  {:name :blah
   :parameters {:query ::optional-params}}]



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.