Function validation using Prismatic/schema in Clojure

1.3k views Asked by At

I have a very simple question about using Prismatic/schema to validate functions. I have a schema for a map that has a single key, the value of which is a function that takes a Bar schema as its single argument and returns anything (used for side effects):

(require '[schema.core :as s])

(def Bar {:baz s/Int})
(def Action :???)
(def Foo {:action Action})

The question is, how do I define Action? I've tried this:

(require '[schema.macros :as sm])

(def Action (sm/=> s/Any Bar))

This looks promising, but I can't get it to fail validation:

(s/explain Action)
;=> (=> Any {:baz Int})

;; This should fail    
(s/validate Foo {:action :anything-goes})
;=> {:action :anything-goes}

What am I doing wrong here?

I read the docs and the tests in core_test, but I can't figure out how to do this.

1

There are 1 answers

1
Tom Dalling On BEST ANSWER

I've found this: https://github.com/Prismatic/schema/blob/a21cc0113ed497f6410c55d92d9088bd710f0b47/src/cljx/schema/core.cljx#L888

So it would be something like:

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

Although, the documentation does say this:

Currently function schemas are purely descriptive; they validate against any function, regardless of actual input and output types