spec-tools
spec-tools copied to clipboard
Spec called several times on explain-data
Hi guys! Thanks for the awesome librairy!
I don't know it's normal or not but I have the following behaviour:
(def my-schema-spec
(ds/spec
::my-schema-spec
{:foo (st/spec #(do (prn "HELLO") (>= (count %) 5)))}))
(s/valid? my-schema-spec {:foo ""})
HELLO
=> false
(s/explain-data my-schema-spec {:foo ""})
HELLO
HELLO
HELLO
HELLO
=> #clojure.spec.aplha/problems.....
You know this behaviour ?
Hi. No, this is not good. Will investigate. Thanks for reporting!
s/explain-data
calls the spec twice by default, but for some reason, data-specs are called twice on explain, so 4 times in total. Hm..
One explanation is that explain*
for st/Spec
calls both conform*
and explain*
on the underlying spec.
(s/def ::a #(do (prn ::a %) (int? %)))
(s/def ::b (st/spec ::a))
(s/explain-data ::a 1)
;; :user/a 1
(s/explain-data ::b 1)
;; :user/a 1
;; :user/a 1
I'm not sure if this is a huge problem in practice, though.
This can be a problem when the spec call a database for example.