当使用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上。尽管如此,我猜想它应该在所有验证函数中产生相同的结果。