Created
May 12, 2020 10:39
-
-
Save fmnoise/174a50bab17a8b39e908e3b7440fde6d to your computer and use it in GitHub Desktop.
Spec metadata
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
(defn form-spec [spec err-format] | |
(with-meta (s/spec spec) {:err-format err-format})) | |
(s/def ::number (form-spec int? (fn [k v] (str (name k) " is not a valid number: " v)))) | |
(s/def :address/apt (form-spec ::number (fn [_ v] (str "wrong apartment number: " v)))) | |
(s/def :address/strno ::number) | |
(s/def :address/street string?) | |
(s/def ::addr (s/keys :req-un [:address/apt :address/street :address/strno])) | |
(defn err [k v] (str (name k) " is invalid: " v)) | |
(->> (s/explain-data ::addr {:apt "1" :street 23 :strno "stt"}) | |
::s/problems | |
(map (fn [{:keys [path val via]}] | |
(let [errfn (-> via last s/get-spec meta :err-format (or err))] | |
(errfn (last path) val))))) | |
;; => ("wrong apartment number: 1" "street is invalid: 23" "strno is not a valid number: stt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment