Whats the best way to coerce dates with compojure-api 2.0 and spec?
I would say:
(require '[spec-tools.spec :as spec])
spec/inst?
… but mostly the spec wrapping is done automatically, so this should work too:
:body-params [date :- inst?]
hope this helps
would it work with “yyyy-MM-dd” as well? or only full ISO date format?
here are the tests: https://github.com/metosin/spec-tools/blob/master/test/cljc/spec_tools/transform_test.cljc#L37-L44
thanks a lot!
np
[date :- inst?]
would display date-time
data type in swagger ui, what if i want date
type(2018-05-20 format), not the date-time one, with spec
currently, there are no other date-style specs available, but you can create those yourself. something like:
(require '[spec-tools.core :as st])
(def date?
(st/spec
{:spec inst?
:json-schema/format "date"}))
(date? (java.util.Date.))
;; true
(st/decode date? "2018-05-20" st/json-transformer)
; #inst"2018-05-20T00:00:00.000-00:00"
(require '[spec-tools.swagger.core :as swagger])
(swagger/transform date?)
; {:type "string", :format "date"}
if you want it to be LocalDate
, just change the predicate and create custom decode
function.
would be happy to take predicates for all java8 java.time
classes. inst?
is kinda too loose for real world.
thanks for the answer Tommi
you’re welcome