ring-swagger

ring-swagger & compojure-api
2019-01-11T11:43:57.013800Z

Hi, how do you guys apply spec for dates? specifically with clj-time if possible?

2019-01-11T11:44:04.014100Z

I’m using compojure-api

2019-01-11T11:44:32.014400Z

I want to coerce strings to clj-time dates

valtteri 2019-01-11T12:30:58.016300Z

Dates in general is a can of worms. 🙂 My current attempt is to use strictly ISO 8601 formatted strings when transferring dates over wire. I use regexp to validate the strings at the edges using spec and I do coercion to date instances in business logic if/when needed.

valtteri 2019-01-11T12:33:40.017Z

Time will show how much pain this approach causes.

1
ikitommi 2019-01-11T13:43:13.017700Z

doesn’t inst? predicate just work?

ikitommi 2019-01-11T13:47:24.017900Z

(st/encode inst? (java.util.Date.) st/json-transformer)
; "2019-01-11T13:46:36.469+0000"

(st/decode inst? "2019-01-11T13:46:36.469+0000" st/json-transformer)
; #inst"2019-01-11T13:46:36.469-00:00"

ikitommi 2019-01-11T13:48:46.019700Z

I guess clj-time (joda) would require custom Specs to work. But that should be relatively straightforward to do?

2019-01-11T13:48:50.019800Z

It works but I’d like to convert to clj-time, the spec itself is ok but I can’t make it nice in swagger

2019-01-11T13:49:08.020400Z

tks all

ikitommi 2019-01-11T13:51:01.020700Z

what does nice in swagger mean?

ikitommi 2019-01-11T13:51:29.021400Z

btw, you can attach the encode & decode functions to specs too:

(s/def ::date
  (st/spec
    {:spec (partial instance? DateTime)
     :type :date-time
     :reason "FAIL"
     :encode/json str->date-time
     :json-schema/default "2017-10-12T05:04:57.585Z"}))

ikitommi 2019-01-11T13:51:41.021700Z

also add swagger hints, like:

(s/def ::date
  (st/spec
    {:spec (partial instance? DateTime)
     :type :date-time
     :reason "FAIL"
     :encode/json str->date-time
     :swagger/type "date-time"
     :json-schema/default "2017-10-12T05:04:57.585Z"}))

2019-01-11T13:52:57.022500Z

oh thats pretty cool

2019-01-11T13:53:08.023Z

nice means the example value gets generated correctly

ikitommi 2019-01-11T13:53:15.023200Z

e.g. encode and decode namespaces control how the spec gets transformed, json-schema and swagger ns’s are used in the json-schema / swagger transformations

ikitommi 2019-01-11T13:53:21.023400Z

yes

2019-01-11T13:56:00.023700Z

was doing something like

(s/def ::clj-time (s/with-gen #(instance? DateTime (from-string %))
                              #(s/gen #{"2018-01-01" "2018-01-02"})))

ikitommi 2019-01-11T13:58:23.025100Z

adding all the possible data (encoder, decoder, gen, example, swagger-info etc.) means a lot of data, but then again, it’s just data and every bit has it’s own use case.

2019-01-11T13:59:36.026700Z

btw in your example is it encode/json or decode/json

2019-01-11T13:59:38.026900Z

?

ikitommi 2019-01-11T13:59:56.027100Z

btw, the swagger-spec-transformer uses json-schema-spec-transformer under the hoods, so with swagger, it merges the swagger namespaced over the json-schema namespaced.

ikitommi 2019-01-11T14:00:10.027600Z

oh, should be decode/json as it’s string->date. good catch!

2019-01-11T14:00:34.028300Z

👍 we like compojure-api 😉

👍 1
😉 1
2019-01-11T14:57:08.029400Z

one more thing @ikitommi, the ::date spec is working but apparently I can’t compose it : (s/def ::somemap (s/map-of ::date ::string))

2019-01-11T14:57:28.029900Z

I can check with s/explain but going to the api it doesn’t conform

ikitommi 2019-01-11T15:01:24.032Z

@mping map-of doesn't conform keys by default. You need to pass in some option to it, can't recall, but found in it's docstring

2019-01-11T15:05:15.032300Z

it didn’t work; it’s :conform-keys true

2019-01-11T15:05:25.032600Z

but apparently it’s receiving a keyword

2019-01-11T15:05:39.033Z

I should try disabling wrap-keyword-params

2019-01-11T15:08:52.033300Z

or maybe with the json parser

ikitommi 2019-01-11T15:12:09.034500Z

oh, I would do that in the :decode/json

ikitommi 2019-01-11T15:12:30.035200Z

e.g. take the name if it's a keyword.

2019-01-11T15:24:15.035400Z

got it! working