Search code examples
clojureclojure.spec

How to check distinct id in spec/coll-of


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

The spec above requires each user map to be distinct but How can I specify it to check for distinct :user/ids only

The collection bellow shouldn't be allowed:

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

Solution

  • (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)))