Created
March 1, 2018 15:33
Star
You must be signed in to star a gist
Short example of humanizing clojure.spec/explain-data output
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 my-app.errors | |
(:require [my-app.spec :as spec] | |
[cljs.spec.alpha :as s])) | |
(def error-map | |
{::spec/required "This field is required" | |
::spec/max-length "This field is too long" | |
:email/regex "Invalid email address" | |
:password/regex "Password must contain at least 1 letter and 1 number" | |
:password/min-length "Password must be at least 8 characters in length" | |
:password/confirm "You must confirm your password" | |
:password/equality "Passwords do not match" | |
}) | |
(defn- primary-error-spec | |
[exp-data] | |
(when exp-data | |
(-> (get-in exp-data [::s/problems]) | |
first | |
:via | |
last))) | |
(defn- humanize-explain-data | |
[exp-data] | |
(-> exp-data | |
primary-error-spec | |
error-map)) | |
(defn error-msg | |
"Error message component that humanizes the given s/explain-data output and renders to a span." | |
[{:keys [exp-data]}] | |
[:span.error (humanize-explain-data exp-data)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment