Welcome! Please see the About page for a little more info on how this works.

+4 votes
in Spec by

I often need a spec that can validate non blank string:

`
(s/def ::non-blank-string (complement str/blank?))

(s/valid? ::non-blank-string "foo")
;; => true
`

It throws with non string values and lacks a generator:

`
(s/exercise ::non-blank-string)
;; => Unhandled clojure.lang.ExceptionInfo...

(s/valid? ::non-blank-string 42)
;; => Unhandled java.lang.ClassCastException...

`

It can be solved with:

`
(s/def ::non-blank-string (s/and string? (complement str/blank?)))

(s/exercise ::non-blank-string)
;; => (["8" "8"] ["pS" "pS"] ["RL" "RL"] ["3" "3"] ["c2ZUx" "c2ZUx"] ["24VF" "24VF"] ["0wc80" "0wc80"] ["Wr49vz" "Wr49vz"] ["1UDte" "1UDte"] ["2f" "2f"])

(s/valid? ::non-blank-string 42)
;; => false
`

The problem: The need in this spec seems to be recurring problem and I end up re-implementing it again and again. Sometimes I wrongly name it ::non-empty-string or create a separate namespace with the spec so I can reuse it.

Solution: Make it standard predicate. Mb it can be called text?

1 Answer

+2 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-2503 (reported by alex+import)
...