ring-swagger

ring-swagger & compojure-api
2018-05-29T10:26:30.000253Z

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

ikitommi 2018-05-29T10:33:40.000320Z

I would say:

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

ikitommi 2018-05-29T10:37:29.000075Z

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

:body-params [date :- inst?]

ikitommi 2018-05-29T10:37:45.000186Z

hope this helps

2018-05-29T10:38:32.000022Z

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

2018-05-29T10:52:34.000458Z

thanks a lot!

ikitommi 2018-05-29T10:52:37.000393Z

np

2018-05-29T13:10:14.000587Z

[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

ikitommi 2018-05-29T13:20:16.000140Z

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

ikitommi 2018-05-29T13:21:14.000142Z

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

ikitommi 2018-05-29T13:22:10.000781Z

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

2018-05-29T13:23:30.000269Z

thanks for the answer Tommi

ikitommi 2018-05-29T13:24:24.000019Z

you’re welcome