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? 😢
my problem isn't exactly the conversion per se, but how to use it with coerce request middleware
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
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
I have this, although I'm using malli...
;; 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)))
(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]]])
maybe that might help?
(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")
hmm where do you use date-range
?
In my posts
for example:
(def start (mu/merge data-range) [:map {:closed true} [:name string?]}))
:post {:handler (start app-config)
:parameters {:body specs/start}
:swagger {:produces [blah-blah-version]}}}]
I'll try to check how malli works to see if I can adapt this to schema
you could try functions from the core - https://clojuredocs.org/clojure.instant