Fork me on GitHub
#reitit
<
2021-05-12
>
matheusashton15:05:11

I'm still trapped in how to coerce a string datetime in iso format to a java.util.Date or something similar, in my requests with reitit and ring, can somebody help please? 😢

delaguardo15:05:15

you could try functions from the core - https://clojuredocs.org/clojure.instant

matheusashton17:05:56

my problem isn't exactly the conversion per se, but how to use it with coerce request middleware

matheusashton17:05:38

because it is already coercing the request to basic data, but I don't know how to make it recognize that a json field is a date and use some function to coerce it

matheusashton15:05:22

tried to follow how reitit works internally in reitit.ring.coercion.coerce-request-middleware with reitit.coercion.schema but couldn't understand how reitit get the matchers in order to call schema.coerce.coercer , I think I can just use schema-tools.coerce/json-coercion-matcher that will work, in the coercer call, but I don't know how to configure this in the middleware exaclty

dharrigan15:05:00

I have this, although I'm using malli...

dharrigan15:05:09

;; ISO_OFFSET_DATE_TIME format, e.g., 2011-12-03T10:15:30+01:00 or 2011-12-03T10:15:30Z
(defn ^:private date-time-parser
  [date-time]
  (try
   (.parse DateTimeFormatter/ISO_OFFSET_DATE_TIME date-time)
   (catch DateTimeParseException _
     nil)))

dharrigan15:05:17

(def date-range [:map
                 {:closed true}
                 [:from {:optional true} [:fn {:swagger/type "string" :swagger/format "date-time" :error/message iso8601-message} date-time-parser]]
                 [:to {:optional true} [:fn {:swagger/type "string" :swagger/format "date-time" :error/message iso8601-message} date-time-parser]]])

dharrigan15:05:22

maybe that might help?

dharrigan15:05:19

(def ^:private iso8601-message "is not in ISO DATE TIME format, e.g. 2020-07-03T10:15:30+01:00 or 2020-07-03T10:15:30Z")

matheusashton15:05:59

hmm where do you use date-range?

dharrigan15:05:35

for example:

dharrigan15:05:11

(def start (mu/merge data-range) [:map {:closed true} [:name string?]}))

dharrigan15:05:47

:post {:handler (start app-config)
               :parameters {:body specs/start}
               :swagger {:produces [blah-blah-version]}}}]

matheusashton15:05:22

I'll try to check how malli works to see if I can adapt this to schema