Skip to content

Schema Quick Reference

shaun edited this page Oct 19, 2021 · 7 revisions

Please search the test examples for details on how to use anything mentioned in this reference.

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

Any code example below using <S> (or any capitalized word) means you can replace it with any Schema. Any lower-case <foo> means it’s not a schema, but a different kind of value whose type is explained inline by the name or adjoining comment.

Use Schemas in “Schematized” defs

Schemas are used as annotations inside special “schematized” versions of Clojure’s core def functions.

(s/def foo :- <S>)
;; or
(s/def ^<S> foo)
  • s/def
  • s/defn s/fn s/letfn
  • s/defmethod
  • s/defrecord

All values below listed as “Schemas“ can be used as annotations in the forms above.

Literal Schemas (exact values)

  • (s/eq <foo>) must be equal to the given literal value foo
  • (s/enum <a> <b> <c> ...) must be equal to any of the given literal values

Primitive Schemas (Leaf Schemas)

  • s/Any
  • s/Bool
  • s/Int
  • s/Inst (#inst date type)
  • s/Keyword
  • s/Num
  • s/Regex
  • s/Str
  • s/Symbol
  • s/Uuid (#uuid type)

Can be Nil

  • (s/maybe <S>) a non-nil value must satisfy this schema

Native Types as Schemas

  • #"..." satisfies a regular expression
  • java.lang.String, java.lang.Number (etc) Java classes
  • long, double, (etc) Java primitive casts
  • js/Element (etc) JavaScript prototypes

Composite Schemas

  • [<S>] a sequence of values, each satisfying the same schema
  • {<K> <V>} a map of values, each satisfying the same key and value schema
  • #{<S>} a set of values, each satisfying the same schema
  • (s/queue <S>) a queue of values, each satisfying the same schema
  • (s/pair <A> <B>) a sequence of two values matching the two schemas

Children: Sequence Elements

  • (s/one <S>) a single, required position in a sequence
  • (s/optional <S>) a single, optional position in a sequence

Children: Map Keys

  • (s/required-key <literal>) or just :foo if literal is a keyword
  • (s/optional-key <literal>)

Children: Recursive

  • (s/recursive #'foo) where foo is the name of the outer schema

Abstract Schemas

  • (s/isa ...) is a child of a given parent in a hierarchy
  • (s/protocol Foo) satisfies a protocol Foo
  • (s/record Foo {...}) is a record Foo satisfying the map schema
  • (s/atom <S>) is an atom with a value satisfying this schema

Function Signature Schemas

These are purely descriptive and don’t do validation:

  • (s/=> <Return> <Arg1> <Arg2> ...) single-arity function with explicit args
  • (s/=> <Return> <Arg1> <Arg2> ... & [<ArgRest>]) single-arity function with rest args
  • (s/=> <Return> [...] [...] ...) multi-arity function

Arbitrary Function Schemas

<f> is a function:

  • (s/pred <f>) must satisfy the given function
  • (s/if <f> <A> <B>) choose a schema based on the predicate function
  • (s/conditional <f> <A> <f> <B> ...) choose a schema based on first matching predicate function

Prescriptive Checking Schemas

Unlike other Schema functions, these are named after what they should be used for, rather than how they work. They are roughly prescibed for checking pre- and post-conditions.

  • (s/cond-pre <A> <B> <C> ...) satisfies any of the given schemas (as pre-conditions?)
  • (s/constrained <A> <B>) satisfies a normal schema A and a post-condition B

The above functions replaced similar functions below, now deprecated:

  • (s/either <A> <B> <C> ...) satisfy any of the given schemas
  • (s/both <A> <B> <C> ...) satisfy all the given schemas

Clarifying Schemas

These are used for making Schema declarations explicit or assigning names.

  • (s/defschema)
  • (s/named)
Clone this wiki locally