bhb / expound

Human-optimized error messages for clojure.spec
Eclipse Public License 1.0
927 stars 24 forks source link

Improve grouping of spec errors #165

Closed kelvinqian00 closed 4 years ago

kelvinqian00 commented 5 years ago

I've been using Expound and overall it's a great library. However the grouping and numbering of spec errors seems to be haphazard, which is annoying when one is validating (for instance) a map with many keys predicates.

Here, we have a example keys spec:

(s/def ::foo string?)
(s/def ::bar string?)
(s/def ::qux string?)
(s/def ::ex-spec (s/keys :req-un [::foo ::bar ::qux]))

Then if I want to validate the following map:

(expound/expound ::ex-spec {:foo 1.2 :bar 123 :qux true})

I ideally want the following output (the Relevant Specs section has been removed for clarity):

-- Spec failed --------------------

  {:foo 1.2, :bar ..., :qux ...}
        ^^^

should satisfy

  string?

-- Spec failed --------------------

  {:foo ..., :bar 123, :qux ...}
                  ^^^

should satisfy

  string?

-- Spec failed --------------------

  {:foo ..., :bar ..., :qux true}
                            ^^^^

should satisfy

  string?

-------------------------
Detected 3 errors

where Expound prints out each failing spec in its own separate error message. However, what Expound actually does is print out this:

-- Spec failed --------------------

  {:foo 1.2, :bar ..., :qux ...}
        ^^^

should satisfy

  string?

or value

  {:foo ..., :bar 123, :qux ...}
                  ^^^

should satisfy

  string?

-- Spec failed --------------------

  {:foo ..., :bar ..., :qux true}
                            ^^^^

should satisfy

  string?

-------------------------
Detected 2 errors

which is not what I want. Not only do the ::foo and ::bar specs have nothing to do with each other (they are not two paths on an s/or or anything like that), but there are clearly 3, not 2, errors here.

Right now I've been using a hacky solution where I separate the original spec error map into a list of multiple error maps and using the expound method on each of them. But this comes with its own problems (it messes up the Detected Error count and overall it's just an ugly hack), so if this gets addressed that would be awesome.

bhb commented 5 years ago

@kelvinqian00 Thanks for reporting this! Yes, this appears to be a bug in how Expound groups specs.

bhb commented 4 years ago

@kelvinqian00 Thanks for reporting this and for your help with the fix! The fix is in version 0.8.0, which I've just released.