Fork me on GitHub
#reitit
<
2023-04-07
>
frozenlock18:04:59

I have an endpoint expecting a Mailli schema of [:vector string?] , but when a single item is provided in the query string, it is parsed as a single string. For example, var=str1 gives "str1" instead of ["str1"] Is there a fix? Obviously it's breaking whenever var is a vector of 1 item. 😕

juhoteperi11:04:07

Presuming you are using the default ring middleware, and want to use e.g. ?foo=1&foo=2 for multiple query string values, you can provide your own decode function to wrap a single string to vector. :decode/string (fn [v] (if (string? v) [v] v)) (in the vector schema)

DrLjótsson13:04:35

I have also been struggling with this! Would be great to see how you implement this @U0ERZQ1K2

juhoteperi13:04:27

An example should be added somewhere

juhoteperi13:04:42

Unfortunately the Reitit examples folder is such a mess currently it is quite a hard to add anything there

frozenlock13:04:09

Hmm... I'm still getting errors. Is this how the declaration is expected?

[:vector [string? {:decode/string #(if (string? %) [%] %)}]]

juhoteperi13:04:08

You need to decode the string to vector

juhoteperi13:04:21

[:vector {:decode/string #(if (string? %) [%] %)} :string]

juhoteperi13:04:03

If you have the decode fn on string schema, coercion fails on :vector level and the children decode fn is never called

frozenlock13:04:36

Works flawlessly, thanks 👍

👍 2
DrLjótsson09:11:44

So happy I pinned this thread - I needed a vector query string now and this worked perfect. Thank you for raising the question @U0ERZQ1K2 and answering it @U061V0GG2 If someone else stumbles upon this, here is the complete parameters declaration

:parameters {:query [:map [:foo [:vector {:decode/string #(if (string? %) [%] %)} :string]]]}