Skip to content

Instantly share code, notes, and snippets.

@cjsauer
Created March 1, 2018 15:33
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cjsauer/10735e7a93af7d4eb1ff9f39b2fc5eea to your computer and use it in GitHub Desktop.
Short example of humanizing clojure.spec/explain-data output
(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