Welcome! Please see the About page for a little more info on how this works.

0 votes
in Spec by
Currently, there is no way to force function {{fdef}} validation on, outside of tests and immune to override with dynamic vars. Forcing a validation in a function can be done using either with a) {{:pre}} hook or b) a manual {{s/conform}} / {{s/valid?}} call but those do not contribute to the function documentation. There are many use-cases for functions that always validate the inputs, e.g. when configuring components on application startup, done once where the performance penalty doesn't matter and correctness is important.

Schema has the {{:always-validate}} metadata for this case:

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

(s/defn ^:always-validate interceptor-x
  [opts :- {:string? s/Bool}])

(interceptor-x {})
; Syntax error (ExceptionInfo) compiling at (test.cljc:150:1).
; Input to interceptor-x does not match schema:
;       [(named {:string? missing-required-key} opts)]

2 Answers

0 votes

Comment made by: ikitommi

Maybe something like:

(require '[clojure.spec.alpha :as s])

(defn plus1 [x]
(inc x))

(s/fdef ^:always-validate plus1

    :args (s/cat :x int?))

(plus1 "1")
; Syntax error (ExceptionInfo) compiling at (user.clj:18:1).
; Call to #'user/plus1 did not conform to spec.

0 votes
Reference: https://clojure.atlassian.net/browse/CLJ-2498 (reported by ikitommi)