Fork me on GitHub
#reitit
<
2019-07-15
>
nbdam08:07:02

How to add new datatypes to spec coercions? I am trying to add datetime to my JSON (testing via reitit swagger ui)

(defn localdatetime-from-string [v]
  (LocalDateTime/parse v dtfmt))

(defn localdatetime-to-string [v]
  (.format dtfmt v))

(def timestamp-str
  (st/spec
    {:spec jt/local-date-time?
     :description "java 8 localdatetime string"
     :json-schema/type {:type "string", :format "timestamp"}
     :json-schema/example "2019-07-17T10:00:00"
     :decode/string #(localdatetime-from-string %2)
     :encode/string #(localdatetime-to-string %2)}))
encoding JSON works fine, but decoding gives a long spec error, this is the part I think is relevant:
{
      "path": [
        "active_from"
      ],
      "pred": "java-time/local-date-time?",
      "val": "2019-07-17T10:00:00",
      "via": [
        "spec$51946/active_from"
      ],
      "in": [
        "active_from"
      ]
}

nbdam08:07:58

Actually, this is not working at all... I thought I can make a spec like this and use it as datatype in model definition like this

(def item
  {(ds/req :id) int?
   (ds/req :active_from) timestamp-str})