Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}]
like image 987
Oguz Bilgic Avatar asked Mar 09 '23 02:03

Oguz Bilgic


1 Answers

(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)))
like image 171
akond Avatar answered Mar 10 '23 16:03

akond