Schema: Is there away to be able to specify case-insensitive enum (string)?

53 views
Skip to first unread message

Wei Xun Ter

unread,
May 4, 2018, 5:57:45 PM5/4/18
to Plumbing and Graph: the Clojure utility belt
Currently s/enum will check for strict equivalent in a set of string enum. Is there away that we can have a case-insensitive version?

(def schema (s/enum ["SUCCESS", "FAILURE"]
i.e. "Success" can be coerce validated to "SUCCESS"

One way I was thinking is to create a new schema:

(defrecord CaseInsensitiveEnumSchema [vs]
Schema
(spec [this] (leaf/leaf-spec (spec/precondition this #(contains? vs (.toUpperCase %)) #(list vs %))))
(explain [this] (cons '
case-insensitive-enum vs)))

(defn case-insensitive-enum
"A value that must be = to some element of vs."
[& vs]
(EnumSchema. (set vs)))

That works for validation i.e. (s/validate schema "Success") => "Success".
But it doesn't work for generating swagger automatically.

Thanks.

Jason Wolfe

unread,
May 5, 2018, 2:31:05 PM5/5/18
to Plumbing and Graph: the Clojure utility belt
The custom schema type seems like a good solution.  I imagine you just have to tell swagger how to generate it, but I'm not sure how that works.

Another option if you're OK normalizing would be to use a regular enum schema plus coercion to normalize the case of the values up front. 

-Jason

Tommi Reiman

unread,
May 5, 2018, 2:49:25 PM5/5/18
to Plumbing and Graph: the Clojure utility belt
Reply all
Reply to author
Forward
0 new messages