Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameter coercion can't work with schema validation #194

Open
igotti opened this issue Apr 8, 2020 · 3 comments
Open

parameter coercion can't work with schema validation #194

igotti opened this issue Apr 8, 2020 · 3 comments

Comments

@igotti
Copy link

igotti commented Apr 8, 2020

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?

@weavejester
Copy link
Owner

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

@igotti
Copy link
Author

igotti commented Apr 8, 2020

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

@weavejester
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants