如何检查规范/合集中的不同ID [英] How to check distinct id in spec/coll-of
本文介绍了如何检查规范/合集中的不同ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(s/def ::users (s/coll-of ::user :distinct true))
上面的规范要求每个用户映射都是不同的,但如何指定仅用于检查不同的:user / ids
The spec above requires each user map to be distinct but How can I specify it to check for distinct :user/ids
only
不应允许以下集合:
[{:id 10 :name "Jessica"} {:id 10 :name "Erica"}]
推荐答案
(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)))
这篇关于如何检查规范/合集中的不同ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文