我想为一个具有密钥:rule/children
或具有两个密钥-:condition/field
和:condition/predicate
的地图编写规范。这是我尝试过的:
(s/keys :req [(s/or :children :rule/children :condition (s/and :condition/field :condition/predicate))])
它会导致错误消息:
Caused by: java.lang.AssertionError: Assert failed: all keys must be namespace-qualified keywords
(every? (fn* [p1__1917#] (c/and (keyword? p1__1917#) (namespace p1__1917#))) (concat req-keys req-un-specs opt opt-un))
我知道必须为s/or
命名每个路径。这里有两条路径-此映射可以具有:children
或可以是:condition
。仅当它具有两个键:condition/field
和:condition/predicate
时,这是一个条件。
答案 0 :(得分:2)
在keys
规范中,您可以使用普通的or
和and
来做到这一点:
(s/def ::map-spec
(s/keys :req [(or :rule/children (and :condition/field :condition/predicate))]))
(s/conform ::map-spec {:rule/children 1}) ;; valid
(s/conform ::map-spec {:condition/field 1}) ;; invalid
(s/conform ::map-spec {:condition/field 1 :condition/predicate 2}) ;; valid