-
Notifications
You must be signed in to change notification settings - Fork 254
Schema Quick Reference
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.
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.
-
(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
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)
-
(s/maybe <S>)
a non-nil value must satisfy this schema
-
#"..."
satisfies a regular expression -
java.lang.String
,java.lang.Number
(etc) Java classes -
long
,double
, (etc) Java primitive casts -
js/Element
(etc) JavaScript prototypes
-
[<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
-
(s/one <S>)
a single, required position in a sequence -
(s/optional <S>)
a single, optional position in a sequence
-
(s/required-key <literal>)
or just:foo
if literal is a keyword (s/optional-key <literal>)
-
(s/recursive #'foo)
where foo is the name of the outer schema
-
(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
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
<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
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
These are used for making Schema declarations explicit or assigning names.
(s/defschema)
(s/named)