Annotate functions and records with Schema
masterSchema extends Clojure's type hinting system using the :- syntax. This allows you to specify schemas for function arguments, return values, and record fields. This provides precise documentation that can be inspected at runtime.
Key macros:
s/defprotocols/defrecords/defns/defs/fn
Example syntax: (defn my-func :- s/Str [arg :- s/Int]) means my-func takes an integer and returns a string.
To inspect the schema of a record or function, use s/explain. To enable runtime validation for all functions annotated with schemas, use s/with-fn-validation.
(s/defprotocol TimestampOffsetter
(offset-timestamp :- s/Int [this offset :- s/Int]))
(s/defrecord StampedNames
[date :- Long
names :- [s/Str]]
TimestampOffsetter
(offset [this offset] (+ date offset)))
(s/defn stamped-names :- StampedNames
[names :- [s/Str]]
(StampedNames. (str (System/currentTimeMillis)) names))
;; Inspecting schemas
(s/explain StampedNames)
;; Enabling validation
(s/with-fn-validation
(stamped-names ["bob"]))