当使用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在List使用时产生不同的结果,但是vector使用时却相同。当我尝试直接使用clojure.spec.alpha创建spec时,一切都正常,所以看起来问题是出在spec-tools上。无论如何,我想它应该在所有的验证函数中都产生相同的结果。