Welcome! Please see the About page for a little more info on how this works.

0 votes
in Spec by

When using spec-tools (metosin/spec-tools {:mvn/version "0.10.5"}) to spec a list of values, result spec produces inconsistent results when validated against in s/valid? and s/explain.

Minimal example:

(ns case
  (:require [spec-tools.data-spec :as ds]
            [clojure.spec.alpha :as s]))

(def invalid-data {:cards (list 66)})

(def valid-data {:cards [66]})

(def cards
  {:cards [int?]})

(defn do-stuff [data]
  (if (s/valid? (ds/spec ::sp cards) data)
    (println "Conforms to spec according to 'valid?'")
    (println "Does not conform to spec according to 'valid?'"))
  (if (s/explain-data (ds/spec ::sp cards) data)
    (println "Does not conform to spec according to 'explain-data'")
    (println "Conforms to spec according to 'explain-data'")))

(comment
  (do-stuff invalid-data)
  (do-stuff valid-data))

For some reason s/valid? and s/explain produce different result when a list is used, but same when a vector is used. When I tried to reproduce this issue with spec created directly with clojure.spec.alpha, everything was fine, so it seems the problem is with spec-tools. Nevertheless, it should produce same results in all validation functions, I guess

1 Answer

+1 vote
by
selected by
 
Best answer

Seems like you have narrowed it down to spec-tools and that clojure.spec.alpha is not to blame here. Given that, I'd post an issue to spec-tools' GitHub.

by
Yeah, but I thought that, even if spec-tools produces invalid spec, shouldn't `valid?` and `explain` still be consistent when validating against it?
by
`spec-tools` uses private API of `spec.alpha` and thus has a potential to create something that looks like a spec but behaves in an erratic way.
by
Didn't know that. I'll report it there then
...