Skip to content

Instantly share code, notes, and snippets.

@stuarthalloway
Created March 17, 2017 01:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save stuarthalloway/c2a38f3741b5d0661d4debf77f2f48bb to your computer and use it in GitHub Desktop.
Save stuarthalloway/c2a38f3741b5d0661d4debf77f2f48bb to your computer and use it in GitHub Desktop.
Clojure spec's missing piece
;; clojure.spec's missing piece, work in progress
;; this is only halfway done, somebody else will need to do the other 95%
(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))
)
@viebel
Copy link

viebel commented Mar 31, 2017

It works fine for simple cases but the spec is more complex, it doesn't work.
See the live demo of a fork of this gist:
http://app.klipse.tech/?eval_only=1&cljs_in.gist=viebel/2ba85fd79a31172e41d8173748a2f161

This is the forked gist: https://gist.github.com/viebel/2ba85fd79a31172e41d8173748a2f161

@arichiardi
Copy link

If the goal here is to produce "user friendly" (rather than dev friendly) error messages, my 2c is that it does not really accomplish that.
I think one would need to further parse the :problems key (maybe with something like this) in order to work against an easier data structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment