explain-str

added

ns
clojure.spec

type
function

(explain-str spec x)

Given a spec and a value that fails to conform, returns an explanation as a string.

                ;; NOTE: This example applies to ClojureScript, I haven't tested it with Clojure.

;; explain-str is a simple way of producing "readable" output given a
;; spec and an object.
;; It does have its limitations:

;; Note that it ends in `\
`. I would assume explain-str is meant to be used
;; only in the repl or shell-kind-of enviroments
(s/explain-str string? "A string")
;; => "Success!\
" 

;; Note what happens if you pass an "anonymous spec":
(s/explain-str string? nil)
;; => "val: nil fails predicate: :cljs.spec/unknown\
"

;; If we try it with a named spec the output is slightly better
(s/def ::foo string?)
(s/explain-str ::foo nil)
;; => "val: nil fails spec: :cljs.user/foo predicate: string?\
"