我正在使用 clojure.spec 来验证地图条目的向量。向量看起来像:
[{:point {:x 30 :y 30}}
{:point {:x 34 :y 33}}
{:user "joe"}]
我想将规范构建为需要 1..N::point
个条目并且只有一个::user
条目。
这是我构建此规范的(不成功的)尝试:
(s/def ::coord (s/and number? #(>= % 0)))
(s/def ::x ::coord)
(s/def ::y ::coord)
(s/def ::point (s/keys :req-un [::x ::y]))
(s/def ::user (s/and string? seq))
(s/def ::vector-entry (s/or ::pt ::user))
(s/def ::my-vector (s/coll-of ::vector-entry :kind vector))
当我只运行一个::point
条目的验证时,它可以工作:
spec> (s/valid? ::point {:point {:x 0 :y 0}})
true
spec> (s/valid? ::my-vector [{:point {:x 0 :y 0}}])
false
关于如何构造s/or
部分以使向量条目可以是任何一个::user
或::point
类型的任何想法?
此外,关于如何在向量中要求一个且仅一个::user
条目和 1..N个条目的任何想法?::point