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

欢迎!请参阅关于页面,以了解更多关于该功能的详细信息。

+1
Spec

我想通过以下声明扩展问题 https://clojure.atlassian.net/browse/CLJ-2648: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

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

0

间接规格引用通常会导致此异常。有一些解决方法

用单句 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))

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

...