clojure - 在 Clojure 中使用 Prismatic/schema 进行函数验证

标签 clojure plumatic-schema

我有一个关于使用 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}

我在这里做错了什么?

我读了docs以及 core_test 中的测试,但我不知道该怎么做。

最佳答案

我发现了这个:https://github.com/Prismatic/schema/blob/a21cc0113ed497f6410c55d92d9088bd710f0b47/src/cljx/schema/core.cljx#L888

所以它会是这样的:

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

相关文章:

clojure - 在 ClojureScript 中右键单击?

clojure - Clojure 序列推导式中是否存在变量?

emacs - 使用 emacs+swank+slime+clojure 的良好工作流程?

clojure - :body-params vs :form-params in compojure-api

clojure - 为什么我不能使用正则表达式来验证字符串作为映射键?

Clojure 项目值得一读吗?

clojure - ArityException 传递给 : core/max 的参数数量 (0) 错误

clojure - 使用 Plumatic Sc​​hema 强制转换为 bigdec

clojure - compojure-api 中的 body 和 body-params 有什么区别?