请在2024年Clojure状态调查中分享您的想法!

欢迎!请查看关于页面以了解此工作的更多信息。

+1
in Spec by

我想通过声明指出,generate/valid?/exercise 对于任何带有嵌套模式规范的规范都不起作用。

(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
by

我认为嵌套模式规范是否将按这种方式工作尚未确定,因此这仍然是待定领域。

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))

无论如何,这很可能不是预期的行为。

...