spec-tools icon indicating copy to clipboard operation
spec-tools copied to clipboard

Spec called several times on explain-data

Open jeans11 opened this issue 6 years ago • 4 comments

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 ?

jeans11 avatar Aug 10 '18 15:08 jeans11

Hi. No, this is not good. Will investigate. Thanks for reporting!

ikitommi avatar Sep 17 '18 18:09 ikitommi

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..

ikitommi avatar Sep 17 '18 19:09 ikitommi

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.

miikka avatar Nov 27 '18 09:11 miikka

This can be a problem when the spec call a database for example.

jeans11 avatar Nov 27 '18 10:11 jeans11