Clojure Spec and Human Readable Error Messages

224 views
Skip to first unread message

David Simmons

unread,
Feb 3, 2020, 4:39:08 PM2/3/20
to Clojure
Hi All.

I've just come back to Clojure (after being away for a few years) and I'm incredibly impressed with how things have moved on. I particuarly like Clojure Spec but I'm struggling to understand how I can convert coercion erros to a human friendly for for end users to understand on a website - i..e user submits a form and gets an error back they can understand.

My Google Foo doesn't appear to have helped but I can't believe I'm the only one to find this a problem (or maybe I don't fully understand spec).

any help gratefully received.

cheers

Dave

John Shaffer

unread,
Feb 3, 2020, 5:27:08 PM2/3/20
to Clojure
The phrase library can create human-readable error messages:

I've been working on something for a more natural (to me) approach, with the ability to define error messages in-line with the predicate. E.g.,

(s/defop max-length [n]
  (sf/validator
   #(>= n (count %))
   (str "Must be " n " characters or less.")))

I got this working with spec-alpha2, but my plan was to write some reforms helpers, and I don't think there is any alpha2 support in ClojureScript. It seems a lot harder to write specs for the shipping version of spec, but I'll probably give it a shot this weekend.

David Simmons

unread,
Feb 4, 2020, 1:33:24 AM2/4/20
to Clojure
Many thanks John.

I'll check out phrase. If you do get something working I'd love to see it as your approach seems the simplest to me.

cheers

Dave
Reply all
Reply to author
Forward
0 new messages