当使用spec-tools (metosin/spec-tools {:mvn/version "0.10.5"}
)来规范一个值列表时,结果规范在s/valid?和s/explain的验证中产生了不一致的结果。
最小示例:
(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))
不知何种原因,当使用列表时,s/valid?和s/explain会产生不同的结果,但当使用向量时,它们的会相同。当我尝试使用clojure.spec.alpha直接创建spec来重现这个问题时,一切正常,所以问题似乎在于spec-tools。尽管如此,我猜测所有验证函数都应该产生相同的结果。