当使用 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 造成的。不过,我猜它应该在整个验证函数中产生相同的结果。