Fork me on GitHub
#ring-swagger
<
2018-05-29
>
dev96410:05:30

Whats the best way to coerce dates with compojure-api 2.0 and spec?

ikitommi10:05:40

I would say:

(require '[spec-tools.spec :as spec])
spec/inst?

ikitommi10:05:29

… but mostly the spec wrapping is done automatically, so this should work too:

:body-params [date :- inst?]

ikitommi10:05:45

hope this helps

dev96410:05:32

would it work with “yyyy-MM-dd” as well? or only full ISO date format?

dev96410:05:34

thanks a lot!

luwh36413:05:14

[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

ikitommi13:05:16

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"}

ikitommi13:05:14

if you want it to be LocalDate, just change the predicate and create custom decode function.

ikitommi13:05:10

would be happy to take predicates for all java8 java.time classes. inst? is kinda too loose for real world.

luwh36413:05:30

thanks for the answer Tommi

ikitommi13:05:24

you’re welcome