【问题标题】:Function validation using Prismatic/schema in Clojure在 Clojure 中使用 Prismatic/schema 进行函数验证
【发布时间】:2014-10-24 06:08:50
【问题描述】:

关于使用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}

我在这里做错了什么?

我阅读了docscore_test 中的测试,但我不知道该怎么做。

【问题讨论】:

    标签: clojure plumatic-schema


    【解决方案1】:

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

    所以应该是这样的:

    (def Action (s/make-fn-schema s/Any [[Bar]]))
    

    虽然,文档确实这样说:

    目前的功能模式纯粹是描述性的;无论实际输入和输出类型如何,它们都会针对任何函数进行验证

    【讨论】:

    • 好的,这很有意义。无论如何,很高兴为它们制作模式,因为将来可能会强制执行的描述性代码具有很大的价值。感谢您仔细阅读代码!
    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 2014-11-12
    • 2014-10-31
    • 2011-01-30
    • 2018-04-10
    • 1970-01-01
    • 2012-03-05
    • 2014-11-19
    相关资源
    最近更新 更多