2024 Clojure 状态调查!中分享你的想法。

欢迎!请参阅关于页面以获取更多关于这是如何工作的信息。

+1投票
Spec

我想通过说明generate/valid?/exercise在所有带有嵌套模式规范的方案中都不工作来扩展问题https://clojure.atlassian.net/browse/CLJ-2648

(s/def :another-entity/id uuid?)
(s/def :another-entity/some-attr int?)
(s/def ::another-entity (s/schema [:another-entity/id :another-entity/some-attr]))

(s/def :entity/id uuid?)
(s/def :entity/another-attr string?)
(s/def :entity/ref ::another-entity)
(s/def ::complex-entity (s/schema [:entity/id :entity/another-attr :entity/ref]))

(s/valid? ::complex-entity {:entity/id (random-uuid)
                            :entity/another-attr "oi"})
;;=> true
(s/valid? ::complex-entity {:entity/id (random-uuid)
                            :entity/another-attr "oi"
                            :entity/ref #:another-entity{:id (random-uuid)}})
;;=> Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1960$fn$G (protocols.clj:11).
;    No implementation of method: :conform* of protocol: #'clojure.alpha.spec.protocols/Spec found for class: clojure.lang.Keyword

(generators/generate (s/gen ::complex-entity))
;;=> Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1960$fn$G (protocols.clj:11).
;    No implementation of method: :conform* of protocol: #'clojure.alpha.spec.protocols/Spec found for class: clojure.lang.Keyword

谢谢 :)

2 答案

+1投票

我认为嵌套模式规范是否会以这种方式工作还不确定,因此这仍然处于待定状态。

0投票
by

间接规范引用经常导致此异常。有一些解决方案

使用单子句s/and定义:entity/ref

(s/def :entity/ref (s/and ::another-entity))

或者使用s/register和s/get-spec注册它

(s/register :entity/ref (s/get-spec ::another-entity))

在任何情况下,这可能不是预期的行为。

...