Skip to content

Instantly share code, notes, and snippets.

@hvitorino
Created December 3, 2019 16:48
Show Gist options
  • Save hvitorino/834def3eb9cfae10cd1df82e5e32ebed to your computer and use it in GitHub Desktop.
Save hvitorino/834def3eb9cfae10cd1df82e5e32ebed to your computer and use it in GitHub Desktop.
(ns transaction-router.routing.interceptors.schema-validation
(:require
[io.pedestal.interceptor :refer [interceptor]]
[route-swagger.doc :as doc]
[schema.core :as skm]
[humanize.schema :as human]))
(defn- explain [schema data]
(human/explain (skm/check schema data)))
(def validate-schema
(interceptor
{:name ::validate-schema
:enter (fn [ctx]
(let [schema (-> ctx :route doc/annotation :parameters :body-params)
data (-> ctx :request :body-params)
explanation (explain schema data)]
(if (nil? explanation)
ctx
(throw (ex-info "Formato inválido. Verifique as mensagens de erro." {:errors explanation})))))}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment