Created
February 16, 2019 13:10
-
-
Save ikitommi/a64314c915cffe46fbd2cef95074a12f to your computer and use it in GitHub Desktop.
insano10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns user | |
(:require [clojure.spec.alpha :as s] | |
[reitit.ring :as ring] | |
[reitit.coercion.spec] | |
[muuntaja.core] | |
[reitit.ring.middleware.exception] | |
[reitit.ring.middleware.parameters] | |
[reitit.ring.middleware.muuntaja] | |
[reitit.ring.coercion] | |
[spec-tools.core :as st])) | |
;; a spec-tools transformer | |
(defn- decode-local-date [_ value] | |
(try | |
(if (string? value) | |
(java.time.LocalDate/parse value)) | |
(catch Exception _ | |
value))) | |
;; a local-date spec | |
(s/def ::local-date | |
(st/spec | |
{:spec (partial instance? java.time.LocalDate) | |
:json-schema/type {:type "string", :format "date"} | |
:decode/string decode-local-date | |
:decode/json decode-local-date})) | |
;; application specs | |
(s/def ::date-str ::local-date) | |
(s/def ::string-to-date-body (s/keys :req-un [::date-str])) | |
;; swagger? | |
(spec-tools.swagger.core/transform ::local-date) | |
; {:title "user/local-date", :type {:type "string", :format "date"}} | |
;; from json | |
(st/decode (s/keys :req-un [::date-str]) {:date-str "2016-01-01"} st/json-transformer) | |
; {:date-str #object[java.time.LocalDate 0x29d996ae "2016-01-01"]} | |
;; from strings | |
(st/coerce (s/keys :req-un [::date-str]) {:date-str "2016-01-01"} st/string-transformer) | |
; {:date-str #object[java.time.LocalDate 0x29d996ae "2016-01-01"]} | |
;; the app | |
(def app | |
(ring/ring-handler | |
(ring/router | |
[["/string-to-date" | |
{:post {:parameters {:body (s/keys :req-un [::date-str])} | |
:handler (fn [{{body :body} :parameters}] | |
(let [coerced-date (:date-str body)] | |
(if (instance? java.time.LocalDate coerced-date) | |
{:status 200, :body "its a LocalDate"} | |
{:status 500, :body (format "coerced type is %s" (type coerced-date))})))}}]] | |
{:data {:coercion reitit.coercion.spec/coercion | |
:muuntaja muuntaja.core/instance | |
:middleware [reitit.ring.middleware.exception/exception-middleware | |
reitit.ring.middleware.parameters/parameters-middleware | |
reitit.ring.middleware.muuntaja/format-negotiate-middleware | |
reitit.ring.middleware.muuntaja/format-response-middleware | |
reitit.ring.middleware.muuntaja/format-request-middleware | |
reitit.ring.coercion/coerce-response-middleware | |
reitit.ring.coercion/coerce-request-middleware]}}))) | |
;; testing | |
(app {:request-method :post, | |
:headers {"content-type" "application/json"} | |
:uri "/string-to-date", | |
:body (muuntaja.core/encode "application/json" {:date-str "2016-01-01"})}) | |
;; {:status 200, :body "its a LocalDate"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment