clojure clojure.spec Registering a spec


In addition to predicates functioning as specs, you can register a spec globally using clojure.spec/def. def requires that a spec being registered is named by a namespace-qualified keyword:

(clojure.spec/def ::odd-nums odd?)
;;=> :user/odd-nums

(clojure.spec/valid? ::odd-nums 1)
;;=> true
(clojure.spec/valid? ::odd-nums 2)
;;=> false

Once registered, a spec can be referenced globally anywhere in a Clojure program.

The ::odd-nums syntax is a shorthand for :user/odd-nums, assuming we are in the user namespace. :: will qualify the symbol it precedes with the current namesapce.

Rather than pass in the predicate, we can pass in the spec name to valid?, and it will work the same way.