compojure icon indicating copy to clipboard operation
compojure copied to clipboard

parameter coercion can't work with schema validation

Open igotti opened this issue 5 years ago • 3 comments

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?

igotti avatar Apr 08 '20 04:04 igotti

Sorry, how are you using Schema? Can you explain a little further?

weavejester avatar Apr 08 '20 04:04 weavejester

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-parse to automatically parse certain args. e.g. id is converted from String to Integer via Integer/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-needed on 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 generated defroutes form.

  • Generates a super-sophisticated Markdown-formatted docstring

igotti avatar Apr 08 '20 05:04 igotti

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.

weavejester avatar Apr 19 '20 04:04 weavejester