# How to Associate Custom Messages with Spec Violations?

**URL:** https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448
**Category:** How to?
**Created:** [February 7, 2020, 1:31am UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448 "2020-02-07T01:31:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ari](https://clojureverse.org/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@Ari](https://clojureverse.org/u/Ari)
#### Post date: [February 7, 2020, 1:31am UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448/1 "2020-02-07T01:31:37Z")

</div>

I’d like to leverage the spec library to validate form input. The idea is to update values in re-frame’s db, validate against spec, and display the corresponding error message. While `explain-data` get me close, it doesn’t quite provide the functionality I need. With the following example, I understand that the issue is the missing `::last-name`, but that’s more involved to programmatically determine. Does anyone know of a way to achieve the desired goal? Thanks.

```
(require '[cljs.spec.alpha :as s])

(s/def ::first-name string?)

(s/def ::last-name string?)

(s/def ::person (s/keys :req [::first-name ::last-name]))

(def p1 {::first-name "Ari"})

(:cljs.spec.alpha/problems (s/explain-data ::person p1))
```

---

<div class="post-metadata">

### Author: ![seancorfield](https://clojureverse.org/user_avatar/clojureverse.org/seancorfield/32/195_2.png) [@seancorfield](https://clojureverse.org/u/seancorfield)
#### Post date: [February 7, 2020, 4:12am UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448/2 "2020-02-07T04:12:26Z")

</div>

Take a look at [https://github.com/alexanderkiel/phrase](https://github.com/alexanderkiel/phrase)

---

<div class="post-metadata">

### Author: ![ikitommi](https://clojureverse.org/user_avatar/clojureverse.org/ikitommi/32/5797_2.png) [@ikitommi](https://clojureverse.org/u/ikitommi)
#### Post date: [February 7, 2020, 9:52am UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448/3 "2020-02-07T09:52:24Z")

</div>

spec-tools also has something for this: the `:reason` spec property is attached to errors: [https://cljdoc.org/d/metosin/spec-tools/0.10.1/doc/spec-records#custom-errors](https://cljdoc.org/d/metosin/spec-tools/0.10.1/doc/spec-records#custom-errors)

---

<div class="post-metadata">

### Author: ![l3nz](https://clojureverse.org/user_avatar/clojureverse.org/l3nz/32/713_2.png) [@l3nz](https://clojureverse.org/u/l3nz)
#### Post date: [February 10, 2020, 2:58pm UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448/4 "2020-02-10T14:58:29Z")

</div>

While I can appreciate the ingenuity, I cannot help but think this is a ugly, ugly hack.

---

<div class="post-metadata">

### Author: ![mauricioszabo](https://clojureverse.org/user_avatar/clojureverse.org/mauricioszabo/32/2482_2.png) [@mauricioszabo](https://clojureverse.org/u/mauricioszabo)
#### Post date: [February 17, 2020, 4:53pm UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448/5 "2020-02-17T16:53:28Z")

</div>

For form validations, I tend to prefer Prismatic Schema or metosin/malli. All spec solutions to custom error messages / coercers / form validations seem too hacky and too dependent of internal state (that will probably change with specs2).

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [August 18, 2020, 4:53am UTC](https://clojureverse.org/t/how-to-associate-custom-messages-with-spec-violations/5448/6 "2020-08-18T04:53:30Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
