Forked from stuarthalloway/clojure_spec_missing_piece.clj
Created
March 31, 2017 04:45
-
-
Save viebel/2ba85fd79a31172e41d8173748a2f161 to your computer and use it in GitHub Desktop.
Clojure spec's missing piece
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require | |
'[clojure.spec :as s] | |
'[clojure.spec.test :as test]) | |
(defn naive-english-explain | |
"Copy and paste this into your app. Figure out what it does by | |
trying it in production." | |
([] (naive-english-explain (ex-data *e))) | |
([spec-explain-data] | |
(let [p1 (-> spec-explain-data ::s/problems first)] | |
(str "\nThis value looks wrong:" | |
"\n\n\t" | |
(pr-str (:val p1)) | |
"\n\nIt should have matched the spec:\n\n\t" | |
(pr-str (:pred p1)) | |
"\n")))) | |
(comment | |
(s/assert (s/coll-of int?) [1 2 :blah]) | |
(println (naive-english-explain)) | |
) | |
(println "when the error is in a primitive type spec - it works ok") | |
(println (-> (s/explain-data (s/coll-of int?) [1 2 :blah]) | |
naive-english-explain)) | |
(s/def ::int-or-string (s/or :int int? :str string?)) | |
(println "when the error is in a spec that contains branching - only the error of the first failing branch is displayed") | |
(println (-> (s/explain-data ::int-or-string [1 2 :blah]) | |
naive-english-explain)) | |
(print "when the predicate that fails contains code - the code is returned (oops...)") | |
(println (-> (s/explain-data (s/coll-of any? :min-count 5 :max-count 5) [1 2 :blah]) | |
naive-english-explain)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment