Skip to content

Instantly share code, notes, and snippets.

@alex-dixon
Created June 17, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-dixon/58b5a2c8ede2d4e7e3af045637631949 to your computer and use it in GitHub Desktop.
Save alex-dixon/58b5a2c8ede2d4e7e3af045637631949 to your computer and use it in GitHub Desktop.

Stupid Spec Tricks

Problem: I want to spec a coll of maps. I know I can use s/coll-of and specify :kind vector? or :kind list?, but either is fine, so I've omitted it. When I call s/explain for this spec with a map it fails properly, but I get explain data for each map entry that says "x" isn't a map. I'd prefer it say it's not a collection and stop there.

Solution:

(s/def ::example (s/coll-of (s/keys :req-un [::foo]) 
                          :kind sequential?))

(s/explain ::example {:foo 1 :bar 2})
=>
-- Spec failed --------------------

  {:foo "bar", :baz 2}

should satisfy

  sequential?

-------------------------
Detected 1 error

Why?:

(coll? {})
=> true

(sequential? {})
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment