Garm is a library that decorates Clojure Specs for better understanding.
[garm "0.7.1"]
(require '[clojure.spec.alpha :as s]
'[garm.core :as garm]
'[garm.specs :as specs]))
;; let's define specs
(s/def ::id specs/uuid)
(s/def ::price (s/and specs/decimal specs/pos))
(s/def ::status (specs/enum #{:dispatched :received}))
(s/def ::order (s/keys :req-un [::id ::price ::status]))
;; `validate` function iterates over explained-data and returns a result as
;; a tuple of `[data errors]`
;; if data are valid, the `errors` will be `nil` and vice versa
(garm/validate ::order {:price -1, :status :foo})
;; the result
[nil {:id [{:args []
:id ::specs/must-be-uuid
:message "Must be a UUID"}]
:price [{:args []
:id ::specs/must-be-decimal
:message "Must be a decimal"}]
:status [{:args []
:id :garm.core/missing-key
:message "This field is required"}]}]
;; with valid data
(garm/validate ::order {:status :dispatched
:price 1000
:id #uuid "0428a077-96fd-4050-870e-ed1449266142"})
[{:status :dispatched
:price 1000
:id #uuid "0428a077-96fd-4050-870e-ed1449266142"}
nil]
Note:
When an error is in a nested object a vector is used for path instead of keyword. E.g.:
{[:object-wo-reason :missing-reason] ;; <-- a key path
[{:args []
:id :garm.core/missing-key
:message "This field is required"}]}]
It converts a result tuple of validate
function to JSON API errors format.
It returns nil
when the result of validate
function is successful.
(-> ::order
(garm/validate {:status :foo
:price 1000
:id #uuid "0428a077-96fd-4050-870e-ed1449266142"}
(garm/->json-api-response))
{:errors [{:id :status
:meta {:data [{:id :garm.specs/must-be-one-of
:message "Must be one of %s"
:args [#{:dispatched :received}]
:title "Must be one of received, dispatched"}]}}]}
;; see formatted message in `:title`
.editorconfig
and The Clojure Style Guide#
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close