This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-11
Channels
- # aleph (38)
- # announcements (6)
- # aws (1)
- # beginners (47)
- # calva (21)
- # cider (47)
- # cljs-dev (18)
- # clojure (40)
- # clojure-europe (7)
- # clojure-india (2)
- # clojure-italy (9)
- # clojure-nl (11)
- # clojure-norway (2)
- # clojure-sanfrancisco (1)
- # clojure-spec (17)
- # clojure-sweden (2)
- # clojure-uk (73)
- # clojurescript (10)
- # cursive (6)
- # datascript (12)
- # datavis (2)
- # defnpodcast (1)
- # duct (5)
- # emacs (36)
- # figwheel (2)
- # figwheel-main (10)
- # juxt (12)
- # leiningen (1)
- # midje (1)
- # nrepl (9)
- # off-topic (25)
- # pedestal (3)
- # portkey (3)
- # quil (2)
- # re-frame (45)
- # reagent (1)
- # ring (3)
- # ring-swagger (36)
- # rum (1)
- # shadow-cljs (48)
- # spacemacs (1)
- # speculative (50)
- # testing (2)
- # tools-deps (27)
- # yada (4)
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.
(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"
I guess clj-time (joda) would require custom Specs to work. But that should be relatively straightforward to do?
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
ended up using something like https://github.com/metosin/compojure-api/blob/master/test19/compojure/api/coercion/spec_coercion_test.clj
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"}))
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"}))
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
was doing something like
(s/def ::clj-time (s/with-gen #(instance? DateTime (from-string %))
#(s/gen #{"2018-01-01" "2018-01-02"})))
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.
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.
https://github.com/metosin/spec-tools/blob/master/src/spec_tools/swagger/core.cljc#L73-L76 https://github.com/metosin/spec-tools/blob/master/src/spec_tools/json_schema.cljc#L272-L280
one more thing @ikitommi, the ::date spec is working but apparently I can’t compose it :
(s/def ::somemap (s/map-of ::date ::string))