Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only return error message string? #77

Closed
leontalbot opened this issue Mar 28, 2018 · 1 comment
Closed

Only return error message string? #77

leontalbot opened this issue Mar 28, 2018 · 1 comment

Comments

@leontalbot
Copy link

Is there a way to pass a fn to spec and value and that it would return error message string.

(def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$")

(expound/def :example/email (s/and string? #(re-matches email-regex %)) "should be a valid email address")

(expound/expound-error :example/email "sally@")

;; => "should be a valid email address"
@leontalbot leontalbot changed the title How can I access only the error messages? On return error message string? Mar 28, 2018
@leontalbot leontalbot changed the title On return error message string? Only return error message string? Mar 28, 2018
@bhb
Copy link
Owner

bhb commented Mar 28, 2018

I discussed this on slack with @leontalbot but in case someone else finds this, I suggested the following:

(expound/def :user/name string? "should be a valid name")

(defn msg [spec val]
  (if (s/valid? spec val)
    nil
    (expound/error-message spec)))

(msg :user/name "John") ; => nil
(msg :user/name :John) ;  => "should be a valid name"

@bhb bhb closed this as completed Mar 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants