在Clojure spec中将s / and与s / or组合

时间:2019-11-11 07:43:08

标签: clojure clojure.spec

我想为一个具有密钥: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时,这是一个条件。

1 个答案:

答案 0 :(得分:2)

keys规范中,您可以使用普通的orand来做到这一点:

(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