我有一个关于使用 Prismatic/schema 的非常简单的问题来验证功能。我有一个具有单个键的映射架构,其值是一个函数,该函数将 Bar 架构作为其单个参数并返回任何内容(用于副作用):
(require '[schema.core :as s])
(def Bar {:baz s/Int})
(def Action :???)
(def Foo {:action Action})
问题是,如何定义 Action
?我试过这个:
(require '[schema.macros :as sm])
(def Action (sm/=> s/Any Bar))
这看起来很有希望,但我无法让它验证失败:
(s/explain Action)
;=> (=> Any {:baz Int})
;; This should fail
(s/validate Foo {:action :anything-goes})
;=> {:action :anything-goes}
我在这里做错了什么?
最佳答案
所以它会是这样的:
(def Action (s/make-fn-schema s/Any [[Bar]]))
尽管如此,文档确实是这样说的:
Currently function schemas are purely descriptive; they validate against any function, regardless of actual input and output types
关于clojure - 在 Clojure 中使用 Prismatic/schema 进行函数验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25581671/