我有一个包含一系列地图的请求对象。我想验证这些映射中给定键没有重复值。
例如,这是有效的:
[
{ :id 1, :data "foo" }
{ :id 2, :data "bar" }
]
这是无效的,因为它包含:id 1
的副本:
[
{ :id 1, :data "foo" }
{ :id 2, :data "bar" }
{ :id 1, :data "baz" }
]
目前我有类似的东西:
(def-map-schema item-schema
[[:id] integer?
[:data] string?])
(def-map-schema request-schema
[[:items] (sequence-of item-schema)])
如何使用clj-schema
表达此唯一性约束?
答案 0 :(得分:1)
clj-schema提供了一个函数clj-schema.schema/simple-schema
,可用于将任意谓词转换为模式。以下是如何使用它来实现maps-with-unique-key?
架构:
(defn maps-with-unique-key? [k]
(s/simple-schema [(s/sequence-of map?)
(fn [xs]
(= (count xs)
(count (distinct (map #(get % k) xs)))))]))
在REPL:
(v/valid? (maps-with-unique-key? :id)
[])
;= true
(v/valid? (maps-with-unique-key? :id)
[{:id 0 :foo "bar"} {:id 1 :foo "baz"} {:id 2 :foo "quux"}])
;= true
(v/valid? (maps-with-unique-key? :id)
[{:id 0 :foo "bar"} {:id 1 :foo "baz"} {:id 0 :foo "quux"}])
;= false
(v/valid? (maps-with-unique-key? :id)
[["not a map"] {:id 0 :foo "bar"} {:id 1 :foo "baz"} {:id 2 :foo "quux"}])
;= false
(以下是Prismatic架构的原始答案。)
我认为标准架构分发中没有现成的架构,但总是可以实现一个新架构 - 请参阅架构维基中的Defining New Schema Types页面。
这是草图:
(defrecord MapsWithUniqueKey [k]
s/Schema
(walker [this]
(fn [x]
(if (and (or (seq? x) (vector? x))
(every? map? x)
(every? #(contains? % k) x)
(== (count x)
(count (distinct (map #(get % k) x)))))
x
(schema.macros/validation-error
this x
(list 'maps-with-unique-key? k (schema.utils/value-name x))))))
(explain [this]
(list 'maps-with-unique-key? k)))
验证示例:
(s/check (->MapsWithUniqueKey :id)
[{:id 1 :foo "bar"} {:id 2 :foo "baz"} {:id 3 :foo "quux"}])
;= nil
(s/check (->MapsWithUniqueKey :id)
[{:id 1 :foo "bar"} {:id 2 :foo "baz"} {:id 1 :foo "quux"}])
;= (not (maps-with-unique-key? :id a-clojure.lang.PersistentVector))
第一个调用返回的nil
表示成功,而后一个返回值为schema.utils.ValidationError
。