如何在spec / coll-of中检查不同的id

时间:2017-09-09 21:06:28

标签: clojure clojure.spec

(s/def ::users (s/coll-of ::user :distinct true))

上述规范要求每个用户地图都是不同的,但我如何指定它以仅检查不同的:user/ids

不应该允许收集:

[{:id 10 :name "Jessica"} {:id 10 :name "Erica"}]

1 个答案:

答案 0 :(得分:3)

(s/def ::id (s/int-in 0 40)) ; just for testing purposes
(s/def ::name string?)
(s/def ::user (s/and (s/keys :req-un [::id ::name])))
(s/def ::user-list (s/and
                       (s/coll-of ::user :distinct true :into [])
                       #(if (empty? %) true (apply distinct? (mapv :id %)))))

(deftest so-test
    (let [users [{:id 11 :name "Jessica"} {:id 11 :name "Erica"}]]
        (prn (g/generate (s/gen ::user-list)))
        (s/assert ::user-list users)))