也许我的问题已经得到解答,但我坚持使用子图规范。
想象一下我有两种这样的可能性
{:type :a
:spec {:name "a"}}
{:type :b
:spec {:id "b"}}
简而言之::spec
密钥取决于类型。对于类型:a
,:spec
必须包含字段:name
,对于类型:b
,规范必须包含字段:id
。
我试过这个:
(s/def ::type keyword?)
(defmulti input-type ::type)
(defmethod input-type :a
[_]
(s/keys :req-un [::name]))
(defmethod input-type :b
[_]
(s/keys :req-un [::id]))
(s/def ::spec (s/multi input-type ::type))
(s/def ::input (s/keys :req-un [::type ::spec]))
这告诉我:没有方法([:spec nil])。我想我明白为什么了:也许 type 是不可访问的。所以我想制作一个更高级别的多规格(基于整个地图)。
问题:我不知道如何定义:spec
基于,:type
因为它们具有相同的名称。您知道如何执行此操作吗?
谢谢