Fork me on GitHub
#clojure-spec
<
2018-03-16
>
ikitommi05:03:20

Toyed more with spec coercion, with idea that coercion functions could be defined in spec meta-data so that the specs could remain self-contained . Currently requires spec-tools to work.

(require '[spec-tools.core :as st])
(require '[spec-tools.conform :as conform])

(def spec
  (st/spec
    {:spec string?
     :description "a string spec"
     ::conform/json #(str %2 "-json")
     ::conform/string #(str %2 "-string")}))

(st/conform spec "kikka")
; "kikka"

(st/conform spec "kikka" st/json-conforming)
; "kikka-json"

(st/conform spec "kikka" st/string-conforming)
; "kikka-string"

jjfine18:03:01

Anyone know why my function doesnt-work throws a java.lang.AssertionError:

(s/def ::bar (fn [x] true))
(defn return-true [_] true)

(defn doesnt-work [something & bar]
  {:pre [(s/assert ::bar bar)]}
  true)

(defn works [something & bar]
  {:pre [(return-true bar)]}
  true)

(deftest failing-test
  (is (works 1))
  (is (thrown? AssertionError (doesnt-work 1))))
This didn't seems spec-specific at first but this test shows I'm probably misunderstanding something about s/def

taylor18:03:06

@jjfine I think the problem is assert is returning its input if it’s valid, and bar is nil, and :pre is interpreting nil as an assertion failure

taylor18:03:31

try {:pre [(s/valid? ::bar bar)]}