parameter coercion can't work with schema validation
When dealing with an optional boolean parameter, string literals like #{"1" "y" "yes" "on" "true"}) treated as true while #{"0" "n" "no" "off" "false"}) as false and nil for others. i wrote a function named as-bool to coerce boolean literal refer to Destructuring-Syntax#parameter-coercion , while schema (s/maybe s/Bool) validates the parameter.
Failed the coercion when data binding if the boolean parameter isn't presented and route entirely as per wiki, there is no chance for schema validation comes to play.
Is it more reasonable to accept nil value at data binding and hand route flow over to schema validation at data validating?
Sorry, how are you using Schema? Can you explain a little further?
what plumatic/schema is.
my sample code:
(api/defendpoint GET "/:card-id/alerts"
"Getting alerts for card with specifications"
[card-id enabled :<< as-bool :as {{:keys [page size] :or {page 1 size 20}} :params}]
{enabled (s/maybe s/Bool)
page su/IntGreaterThanZero
size su/IntGreaterThanZero}
(do-something))
where s alias for schema.core and su things alike, api/defendpoint refers to:
(doc metabase.api.common/defendpoint)
([method route docstr? args schemas-map? & body]) Macro Define an API function. This automatically does several things:
-
calls
auto-parseto automatically parse certain args. e.g.idis converted fromStringtoIntegerviaInteger/parseInt -
converts ROUTE from a simple form like
"/:id"to a typed one like["/:id" :id #"[0-9]+"] -
sequentially applies specified annotation functions on args to validate them.
-
automatically calls
wrap-response-if-neededon the result of BODY -
tags function's metadata in a way that subsequent calls to
define-routes(see below) will automatically include the function in the generateddefroutesform. -
Generates a super-sophisticated Markdown-formatted docstring
We can't break existing functionality in Compojure, so I see only two ways forward.
The first is to create another symbol or object to denote a missing value, and create an appropriate schema function. For example, (def missing (Object.)), or ::missing.
The second is to add in a new symbol for coercion that matches even on nil. For example, enabled :<<? as-bool.