在 Clojure 规范中结合 s/and 和 s/or
Combining s/and with s/or in 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
.
这两个键时才是条件
在 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
我想为具有键 :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
.
在 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