Liking cljdoc? Tell your friends :D

schema.core

A library for data shape definition and validation. A Schema is just Clojure data, which can be used to document and validate Clojure functions and data.

For example,

(def FooBar {:foo Keyword :bar [Number]}) ;; a schema

(check FooBar {:foo :k :bar [1.0 2.0 3.0]}) ==> nil

representing successful validation, but the following all return helpful errors describing how the provided data fails to measure up to schema FooBar's standards.

(check FooBar {:bar [1.0 2.0 3.0]}) ==> {:foo missing-required-key}

(check FooBar {:foo 1 :bar [1.0 2.0 3.0]}) ==> {:foo (not (keyword? 1))}

(check FooBar {:foo :k :bar [1.0 2.0 3.0] :baz 1}) ==> {:baz disallowed-key}

Schema lets you describe your leaf values using the Any, Keyword, Symbol, Number, String, and Int definitions below, or (in Clojure) you can use arbitrary Java classes or primitive casts to describe simple values.

From there, you can build up schemas for complex types using Clojure syntax (map literals for maps, set literals for sets, vector literals for sequences, with details described below), plus helpers below that provide optional values, enumerations, arbitrary predicates, and more.

Assuming you (:require [schema.core :as s :include-macros true]), Schema also provides macros for defining records with schematized elements (s/defrecord), and named or anonymous functions (s/fn and s/defn) with schematized inputs and return values. In addition to producing better-documented records and functions, these macros allow you to retrieve the schema associated with the defined record or function. Moreover, functions include optional validation, which will throw an error if the inputs or outputs do not match the provided schemas:

(s/defrecord FooBar [foo :- Int bar :- String])

(s/defn quux :- Int [foobar :- Foobar mogrifier :- Number] (* mogrifier (+ (:foo foobar) (Long/parseLong (:bar foobar)))))

(quux (FooBar. 10 "5") 2) ==> 30

(fn-schema quux) ==> (=> Int (record user.FooBar {:foo Int, :bar java.lang.String}) java.lang.Number)

(s/with-fn-validation (quux (FooBar. 10.2 "5") 2)) ==> Input to quux does not match schema: [(named {:foo (not (integer? 10.2))} foobar) nil]

As you can see, the preferred syntax for providing type hints to schema's defrecord, fn, and defn macros is to follow each element, argument, or function name with a :- schema. Symbols without schemas default to a schema of Any. In Clojure, class (e.g., clojure.lang.String) and primitive schemas (long, double) are also propagated to tag metadata to ensure you get the type hinting and primitive behavior you ask for.

If you don't like this style, standard Clojure-style typehints are also supported:

(fn-schema (s/fn [^String x])) ==> (=> Any java.lang.String)

You can directly type hint a symbol as a class, primitive, or simple schema.

See the docstrings of defrecord, fn, and defn for more details about how to use these macros.

A library for data shape definition and validation. A Schema is just Clojure data,
which can be used to document and validate Clojure functions and data.

For example,

(def FooBar {:foo Keyword :bar [Number]}) ;; a schema

(check FooBar {:foo :k :bar [1.0 2.0 3.0]})
==> nil

representing successful validation, but the following all return helpful errors
describing how the provided data fails to measure up to schema FooBar's standards.

(check FooBar {:bar [1.0 2.0 3.0]})
==> {:foo missing-required-key}

(check FooBar {:foo 1 :bar [1.0 2.0 3.0]})
==> {:foo (not (keyword? 1))}

(check FooBar {:foo :k :bar [1.0 2.0 3.0] :baz 1})
==> {:baz disallowed-key}

Schema lets you describe your leaf values using the Any, Keyword, Symbol, Number,
String, and Int definitions below, or (in Clojure) you can use arbitrary Java
classes or primitive casts to describe simple values.

From there, you can build up schemas for complex types using Clojure syntax
(map literals for maps, set literals for sets, vector literals for sequences,
with details described below), plus helpers below that provide optional values,
enumerations, arbitrary predicates, and more.

Assuming you (:require [schema.core :as s :include-macros true]),
Schema also provides macros for defining records with schematized elements
(s/defrecord), and named or anonymous functions (s/fn and s/defn) with
schematized inputs and return values.  In addition to producing better-documented
records and functions, these macros allow you to retrieve the schema associated
with the defined record or function.  Moreover, functions include optional
*validation*, which will throw an error if the inputs or outputs do not
match the provided schemas:

(s/defrecord FooBar
 [foo :- Int
  bar :- String])

(s/defn quux :- Int
 [foobar :- Foobar
  mogrifier :- Number]
 (* mogrifier (+ (:foo foobar) (Long/parseLong (:bar foobar)))))

(quux (FooBar. 10 "5") 2)
==> 30

(fn-schema quux)
==> (=> Int (record user.FooBar {:foo Int, :bar java.lang.String}) java.lang.Number)

(s/with-fn-validation (quux (FooBar. 10.2 "5") 2))
==> Input to quux does not match schema: [(named {:foo (not (integer? 10.2))} foobar) nil]

As you can see, the preferred syntax for providing type hints to schema's defrecord,
fn, and defn macros is to follow each element, argument, or function name with a
:- schema.  Symbols without schemas default to a schema of Any.  In Clojure,
class (e.g., clojure.lang.String) and primitive schemas (long, double) are also
propagated to tag metadata to ensure you get the type hinting and primitive
behavior you ask for.

If you don't like this style, standard Clojure-style typehints are also supported:

(fn-schema (s/fn [^String x]))
==> (=> Any java.lang.String)

You can directly type hint a symbol as a class, primitive, or simple
schema.

See the docstrings of defrecord, fn, and defn for more details about how
to use these macros.
raw docstring

*elide-defprotocol-instrumentation*clj/s

If the s/defprotocol instrumentation strategy is problematic for your platform, set atom to true and instrumentation will not be performed.

Defaults to false.

If the s/defprotocol instrumentation strategy is problematic
for your platform, set atom to true and instrumentation will not
be performed.

Defaults to false.
sourceraw docstring

=>clj/smacro

(=> output-schema & arg-schemas)

Convenience macro for defining function schemas with a single arity; like =>*, but there is no vector around the argument schemas for this arity.

Convenience macro for defining function schemas with a single arity; like =>*, but
there is no vector around the argument schemas for this arity.
sourceraw docstring

=>*clj/smacro

(=>* output-schema & arity-schema-specs)

Produce a function schema from an output schema and a list of arity input schema specs, each of which is a vector of argument schemas, ending with an optional '& more-schema' specification where more-schema must be a sequence schema.

Currently function schemas are purely descriptive; there is no validation except for functions defined directly by s/fn or s/defn

Produce a function schema from an output schema and a list of arity input schema specs,
each of which is a vector of argument schemas, ending with an optional '& more-schema'
specification where more-schema must be a sequence schema.

Currently function schemas are purely descriptive; there is no validation except for
functions defined directly by s/fn or s/defn
sourceraw docstring

Anyclj/s

Any value, including nil.

Any value, including nil.
sourceraw docstring

AnythingSchemaclj/s

source

as-queueclj/s

(as-queue col)
source

atomclj/s

(atom schema)

An atom containing a value matching 'schema'.

An atom containing a value matching 'schema'.
sourceraw docstring

Atomicclj/s

source

Boolclj/s

Boolean true or false

Boolean true or false
sourceraw docstring

Bothclj/s

source

bothclj/sdeprecated

(both & schemas)

A value that must satisfy every schema in schemas.

DEPRECATED: prefer 'conditional' with a single condition instead, or constrained.

When used with coercion, coerces each schema in sequence.

A value that must satisfy every schema in schemas.

DEPRECATED: prefer 'conditional' with a single condition
instead, or `constrained`.

When used with coercion, coerces each schema in sequence.
sourceraw docstring

checkclj/s

(check schema x)

Return nil if x matches schema; otherwise, returns a value that looks like the 'bad' parts of x with ValidationErrors at the leaves describing the failures.

If you will be checking many datums, it is much more efficient to create a 'checker' once and call it on each of them.

Return nil if x matches schema; otherwise, returns a value that looks like the
'bad' parts of x with ValidationErrors at the leaves describing the failures.

If you will be checking many datums, it is much more efficient to create
a 'checker' once and call it on each of them.
sourceraw docstring

checkerclj/s

(checker schema)

Compile an efficient checker for schema, which returns nil for valid values and error descriptions otherwise.

Compile an efficient checker for schema, which returns nil for valid values and
error descriptions otherwise.
sourceraw docstring

cond-preclj/s

(cond-pre & schemas)

A replacement for either that constructs a conditional schema based on the schema spec preconditions of the component schemas.

Given a datum, the preconditions for each schema (which typically check just the outermost class) are tested against the datum in turn. The first schema whose precondition matches is greedily selected, and the datum is validated against that schema. Unlike either, a validation failure is final (and there is no backtracking to try other schemas that might match).

Thus, cond-pre is only suitable for schemas with mutually exclusive preconditions (e.g., s/Int and s/Str). If this doesn't hold (e.g. {:a s/Int} and {:b s/Str}), you must use conditional instead and provide an explicit condition for distinguishing the cases.

EXPERIMENTAL

A replacement for `either` that constructs a conditional schema
based on the schema spec preconditions of the component schemas.

Given a datum, the preconditions for each schema (which typically
check just the outermost class) are tested against the datum in turn.
The first schema whose precondition matches is greedily selected,
and the datum is validated against that schema.  Unlike `either`,
a validation failure is final (and there is no backtracking to try
other schemas that might match).

Thus, `cond-pre` is only suitable for schemas with mutually exclusive
preconditions (e.g., s/Int and s/Str).  If this doesn't hold
(e.g. {:a s/Int} and {:b s/Str}), you must use `conditional` instead
and provide an explicit condition for distinguishing the cases.

EXPERIMENTAL
sourceraw docstring

conditionalclj/s

(conditional & preds-and-schemas)

Define a conditional schema. Takes args like cond, (conditional pred1 schema1 pred2 schema2 ...), and checks the first schemaX where predX (an ordinary Clojure function that returns true or false) returns true on the value. Unlike cond, throws if the value does not match any condition. :else may be used as a final condition in the place of (constantly true). More efficient than either, since only one schema must be checked. An optional final argument can be passed, a symbol to appear in error messages when none of the conditions match.

Define a conditional schema.  Takes args like cond,
(conditional pred1 schema1 pred2 schema2 ...),
and checks the first schemaX where predX (an ordinary Clojure function
that returns true or false) returns true on the value.
Unlike cond, throws if the value does not match any condition.
:else may be used as a final condition in the place of (constantly true).
More efficient than either, since only one schema must be checked.
An optional final argument can be passed, a symbol to appear in
error messages when none of the conditions match.
sourceraw docstring

ConditionalSchemaclj/s

source

CondPreclj/s

source

constrainedclj/s

(constrained s p?)
(constrained s p? pred-name)

A schema with an additional post-condition. Differs from conditional with a single schema, in that the predicate checked after the main schema. This can lead to better error messages, and is often better suited for coercion.

A schema with an additional post-condition.  Differs from `conditional`
with a single schema, in that the predicate checked *after* the main
schema.  This can lead to better error messages, and is often better
suited for coercion.
sourceraw docstring

Constrainedclj/s

source

defclj/smacro

(def & def-args)

Like def, but takes a schema on the var name (with the same format as the output schema of s/defn), requires an initial value, and asserts that the initial value matches the schema on the var name (regardless of the status of with-fn-validation). Due to limitations of add-watch!, cannot enforce validation of subsequent rebindings of var. Throws at compile-time for clj, and client-side load-time for cljs.

Example:

(s/def foo :- long "a long" 2)

Like def, but takes a schema on the var name (with the same format
as the output schema of s/defn), requires an initial value, and
asserts that the initial value matches the schema on the var name
(regardless of the status of with-fn-validation).  Due to
limitations of add-watch!, cannot enforce validation of subsequent
rebindings of var.  Throws at compile-time for clj, and client-side
load-time for cljs.

Example:

(s/def foo :- long "a long" 2)
sourceraw docstring

defmethodclj/smacro

(defmethod multifn dispatch-val & fn-tail)

Like clojure.core/defmethod, except that schema-style typehints can be given on the argument symbols and after the dispatch-val (for the return value).

See (doc s/defn) for details.

Examples:

(s/defmethod mymultifun :a-dispatch-value :- s/Num [x :- s/Int y :- s/Num] (* x y))

;; You can also use meta tags like ^:always-validate by placing them ;; before the multifunction name:

(s/defmethod ^:always-validate mymultifun :a-dispatch-value [x y] (* x y))

Like clojure.core/defmethod, except that schema-style typehints can be given on
the argument symbols and after the dispatch-val (for the return value).

See (doc s/defn) for details.

Examples:

  (s/defmethod mymultifun :a-dispatch-value :- s/Num [x :- s/Int y :- s/Num] (* x y))

  ;; You can also use meta tags like ^:always-validate by placing them
  ;; before the multifunction name:

  (s/defmethod ^:always-validate mymultifun :a-dispatch-value [x y] (* x y))
sourceraw docstring

defnclj/smacro

(defn & defn-args)

Like clojure.core/defn, except that schema-style typehints can be given on the argument symbols and on the function name (for the return value).

You can call s/fn-schema on the defined function to get its schema back, or use with-fn-validation to enable runtime checking of function inputs and outputs.

(s/defn foo :- s/Num [x :- s/Int y :- s/Num] (* x y))

(s/fn-schema foo) ==> (=> java.lang.Number Int java.lang.Number)

(s/with-fn-validation (foo 1 2)) ==> 2

(s/with-fn-validation (foo 1.5 2)) ==> Input to foo does not match schema: [(named (not (integer? 1.5)) x) nil]

See (doc schema.core) for details of the :- syntax for arguments and return schemas.

The overhead for checking if run-time validation should be used is very small -- about 5% of a very small fn call. On top of that, actual validation costs what it costs.

You can also turn on validation unconditionally for this fn only by putting ^:always-validate metadata on the fn name.

Gotchas and limitations:

  • The output schema always goes on the fn name, not the arg vector. This means that all arities must share the same output schema. Schema will automatically propagate primitive hints to the arg vector and class hints to the fn name, so that you get the behavior you expect from Clojure.

  • All primitive schemas will be passed through as type hints to Clojure, despite their legality in a particular position. E.g., (s/defn foo [x :- int]) will fail because Clojure does not allow primitive ints as fn arguments; in such cases, use the boxed Classes instead (e.g., Integer).

  • Schema metadata is only processed on top-level arguments. I.e., you can use destructuring, but you must put schema metadata on the top-level arguments, not the destructured variables.

    Bad: (s/defn foo [{:keys [x :- s/Int]}]) Good: (s/defn foo [{:keys [x]} :- {:x s/Int}])

  • Only a specific subset of rest-arg destructuring is supported:

    • & rest works as expected
    • & [a b] works, with schemas for individual elements parsed out of the binding, or an overall schema on the vector
    • & {} is not supported.
  • Unlike clojure.core/defn, a final attr-map on multi-arity functions is not supported.

Like clojure.core/defn, except that schema-style typehints can be given on
the argument symbols and on the function name (for the return value).

You can call s/fn-schema on the defined function to get its schema back, or
use with-fn-validation to enable runtime checking of function inputs and
outputs.

(s/defn foo :- s/Num
 [x :- s/Int
  y :- s/Num]
 (* x y))

(s/fn-schema foo)
==> (=> java.lang.Number Int java.lang.Number)

(s/with-fn-validation (foo 1 2))
==> 2

(s/with-fn-validation (foo 1.5 2))
==> Input to foo does not match schema: [(named (not (integer? 1.5)) x) nil]

See (doc schema.core) for details of the :- syntax for arguments and return
schemas.

The overhead for checking if run-time validation should be used is very
small -- about 5% of a very small fn call.  On top of that, actual
validation costs what it costs.

You can also turn on validation unconditionally for this fn only by
putting ^:always-validate metadata on the fn name.

Gotchas and limitations:
 - The output schema always goes on the fn name, not the arg vector. This
   means that all arities must share the same output schema. Schema will
   automatically propagate primitive hints to the arg vector and class hints
   to the fn name, so that you get the behavior you expect from Clojure.
 - All primitive schemas will be passed through as type hints to Clojure,
   despite their legality in a particular position.  E.g.,
     (s/defn foo [x :- int])
   will fail because Clojure does not allow primitive ints as fn arguments;
   in such cases, use the boxed Classes instead (e.g., Integer).
 - Schema metadata is only processed on top-level arguments.  I.e., you can
   use destructuring, but you must put schema metadata on the top-level
   arguments, not the destructured variables.

   Bad:  (s/defn foo [{:keys [x :- s/Int]}])
   Good: (s/defn foo [{:keys [x]} :- {:x s/Int}])
 - Only a specific subset of rest-arg destructuring is supported:
   - & rest works as expected
   - & [a b] works, with schemas for individual elements parsed out of the binding,
     or an overall schema on the vector
   - & {} is not supported.
 - Unlike clojure.core/defn, a final attr-map on multi-arity functions
   is not supported.
sourceraw docstring

defprotocolclj/smacro

(defprotocol & name+opts+sigs)

Like clojure.core/defprotocol, except schema-style typehints can be provided for the argument symbols and after method names (for output schemas).

^:always-validate and ^:never-validate metadata can be specified for all methods on the protocol name. If specified on the method name, ignores the protocol name metatdata and uses the method name metadata.

Examples:

(s/defprotocol MyProtocol "Docstring" :extend-via-metadata true (^:always-validate method1 :- s/Int [this a :- s/Bool] [this a :- s/Any, b :- s/Str] "Method doc2") (^:never-validate method2 :- s/Int [this] "Method doc2"))

Gotchas and limitations:

  • Implementation details are used to instrument protocol methods for schema checking. This is tested against a variety of platforms and versions, however if this is problematic for your environment, use elide-defprotocol-instrumentation to disable such instrumentation (either at compile-time or runtime depending on your needs). In ClojureScript, method var metadata will be overwritten unless disabled at compile-time.
  • :schema metadata on protocol method vars is only supported in Clojure.
  • Clojure will never inline protocol methods, as :inline metadata is added to protocol methods designed to defeat potential short-circuiting of schema checks. This also means compile-time errors for arity errors are suppressed (eg., No single method errors).
Like clojure.core/defprotocol, except schema-style typehints can be provided for
the argument symbols and after method names (for output schemas).

^:always-validate and ^:never-validate metadata can be specified for all
methods on the protocol name. If specified on the method name, ignores
the protocol name metatdata and uses the method name metadata.

Examples:

  (s/defprotocol MyProtocol
    "Docstring"
    :extend-via-metadata true
    (^:always-validate method1 :- s/Int
      [this a :- s/Bool]
      [this a :- s/Any, b :- s/Str]
      "Method doc2")
    (^:never-validate method2 :- s/Int
      [this]
      "Method doc2"))

Gotchas and limitations:
- Implementation details are used to instrument protocol methods for schema
  checking. This is tested against a variety of platforms and versions,
  however if this is problematic for your environment, use
  *elide-defprotocol-instrumentation* to disable such instrumentation
  (either at compile-time or runtime depending on your needs).
  In ClojureScript, method var metadata will be overwritten unless disabled
  at compile-time. 
- :schema metadata on protocol method vars is only supported in Clojure.
- Clojure will never inline protocol methods, as :inline metadata is added to protocol
  methods designed to defeat potential short-circuiting of schema checks. This also means
  compile-time errors for arity errors are suppressed (eg., `No single method` errors).
sourceraw docstring

defrecordclj/smacro

(defrecord name field-schema extra-key-schema? extra-validator-fn? & opts+specs)

Define a record with a schema.

In addition to the ordinary behavior of defrecord, this macro produces a schema for the Record, which will automatically be used when validating instances of the Record class:

(m/defrecord FooBar [foo :- Int bar :- String])

(schema.utils/class-schema FooBar) ==> (record user.FooBar {:foo Int, :bar java.lang.String})

(s/check FooBar (FooBar. 1.2 :not-a-string)) ==> {:foo (not (integer? 1.2)), :bar (not (instance? java.lang.String :not-a-string))}

See (doc schema.core) for details of the :- syntax for record elements.

Moreover, optional arguments extra-key-schema? and extra-validator-fn? can be passed to augment the record schema.

  • extra-key-schema is a map schema that defines validation for additional key-value pairs not in the record base (the default is to not allow extra mappings).
  • extra-validator-fn? is an additional predicate that will be used as part of validating the record value.

The remaining opts+specs (i.e., protocol and interface implementations) are passed through directly to defrecord.

Finally, this macro replaces Clojure's map->name constructor with one that is more than an order of magnitude faster (as of Clojure 1.5), and provides a new strict-map->name constructor that throws or drops extra keys not in the record base.

Define a record with a schema.

In addition to the ordinary behavior of defrecord, this macro produces a schema
for the Record, which will automatically be used when validating instances of
the Record class:

(m/defrecord FooBar
 [foo :- Int
  bar :- String])

(schema.utils/class-schema FooBar)
==> (record user.FooBar {:foo Int, :bar java.lang.String})

(s/check FooBar (FooBar. 1.2 :not-a-string))
==> {:foo (not (integer? 1.2)), :bar (not (instance? java.lang.String :not-a-string))}

See (doc schema.core) for details of the :- syntax for record elements.

Moreover, optional arguments extra-key-schema? and extra-validator-fn? can be
passed to augment the record schema.
 - extra-key-schema is a map schema that defines validation for additional
   key-value pairs not in the record base (the default is to not allow extra
    mappings).
 - extra-validator-fn? is an additional predicate that will be used as part
   of validating the record value.

The remaining opts+specs (i.e., protocol and interface implementations) are
passed through directly to defrecord.

Finally, this macro replaces Clojure's map->name constructor with one that is
more than an order of magnitude faster (as of Clojure 1.5), and provides a
new strict-map->name constructor that throws or drops extra keys not in the
record base.
sourceraw docstring

defrecord+clj/smacro

(defrecord+ name
            field-schema
            extra-key-schema?
            extra-validator-fn?
            &
            opts+specs)

DEPRECATED -- canonical version moved to schema.potemkin Like defrecord, but emits a record using potemkin/defrecord+. You must provide your own dependency on potemkin to use this.

DEPRECATED -- canonical version moved to schema.potemkin
Like defrecord, but emits a record using potemkin/defrecord+.  You must provide
your own dependency on potemkin to use this.
sourceraw docstring

defschemaclj/smacro

(defschema name form)
(defschema name docstring form)

Convenience macro to make it clear to reader that body is meant to be used as a schema. The name of the schema is recorded in the metadata.

Convenience macro to make it clear to reader that body is meant to be used as a schema.
The name of the schema is recorded in the metadata.
sourceraw docstring

eitherclj/sdeprecated

(either & schemas)

A value that must satisfy at least one schema in schemas. Note that either does not work properly with coercion

DEPRECATED: prefer conditional or cond-pre

WARNING: either does not work with coercion. It is also slow and gives bad error messages. Please consider using conditional and friends instead; they are more efficient, provide better error messages, and work with coercion.

A value that must satisfy at least one schema in schemas.
Note that `either` does not work properly with coercion

DEPRECATED: prefer `conditional` or `cond-pre`

WARNING: either does not work with coercion.  It is also slow and gives
bad error messages.  Please consider using `conditional` and friends
instead; they are more efficient, provide better error messages,
and work with coercion.
sourceraw docstring

Eitherclj/s

source

enumclj/s

(enum & vs)

A value that must be = to some element of vs.

A value that must be = to some element of vs.
sourceraw docstring

EnumSchemaclj/s

source

eqclj/s

(eq v)

A value that must be (= v).

A value that must be (= v).
sourceraw docstring

EqSchemaclj/s

source

explain-input-schemaclj/s

(explain-input-schema input-schema)
source

explicit-schema-keyclj/s

(explicit-schema-key ks)
source

extend-primitiveclj/smacro

(extend-primitive cast-sym class-sym)
source

find-extra-keys-schemaclj/s

(find-extra-keys-schema map-schema)
source

fnclj/smacro

(fn & fn-args)

s/fn : s/defn :: clojure.core/fn : clojure.core/defn

See (doc s/defn) for details.

Additional gotchas and limitations:

  • Like s/defn, the output schema must go on the fn name. If you don't supply a name, schema will gensym one for you and attach the schema.
  • Unlike s/defn, the function schema is stored in metadata on the fn. The implications of this differ per platform: :clj The resulting function has the same performance characteristics as clojure.core/fn. Additionally, the following invariant holds for all parameters and schema annotations: (let [f (s/fn this ... [...] this)] (assert (identical? f (f ...)))) :cljs Returns a wrapper function that forwards arguments positionally up to 20 arguments, and then via apply beyond 20 arguments. See cljs.core/with-meta and cljs.core.MetaFn.
s/fn : s/defn :: clojure.core/fn : clojure.core/defn

See (doc s/defn) for details.

Additional gotchas and limitations:
 - Like s/defn, the output schema must go on the fn name. If you
   don't supply a name, schema will gensym one for you and attach
   the schema.
 - Unlike s/defn, the function schema is stored in metadata on the
   fn. The implications of this differ per platform:
   :clj   The resulting function has the same performance characteristics
          as clojure.core/fn. Additionally, the following invariant
          holds for all parameters and schema annotations:
            (let [f (s/fn this ... [...] this)]
              (assert (identical? f (f ...))))
   :cljs  Returns a wrapper function that forwards arguments positionally
          up to 20 arguments, and then via `apply` beyond 20 arguments.
          See `cljs.core/with-meta` and `cljs.core.MetaFn`.
sourceraw docstring

fn-schemaclj/s

(fn-schema f)

Produce the schema for a function defined with s/fn or s/defn.

Produce the schema for a function defined with s/fn or s/defn.
sourceraw docstring

fn-validation?clj/s

(fn-validation?)

Get the current global schema validation setting.

Get the current global schema validation setting.
sourceraw docstring

fn-validatorclj/s

A var that can be rebound to a function to customize the behavior of fn validation. When fn validation is on and fn-validator is bound to a function, normal argument and return value checks will be substituted with a call to this function with five arguments:

direction - :input or :output fn-name - a symbol, the function's name schema - the schema for the arglist or the return value checker - a precompiled checker to check a value against the schema value - the actual arglist or return value

The function's return value will be ignored.

A var that can be rebound to a function to customize the behavior
of fn validation. When fn validation is on and `fn-validator` is
bound to a function, normal argument and return value checks will
be substituted with a call to this function with five arguments:

  direction   - :input or :output
  fn-name     - a symbol, the function's name
  schema      - the schema for the arglist or the return value
  checker     - a precompiled checker to check a value against
                the schema
  value       - the actual arglist or return value

The function's return value will be ignored.
sourceraw docstring

FnSchemaclj/s

source

HasPreconditionclj/sprotocol

preconditionclj/s

(precondition this)

Return a predicate representing the Precondition for this schema: the predicate returns true if the precondition is satisfied. (See spec.core for more details)

Return a predicate representing the Precondition for this schema:
the predicate returns true if the precondition is satisfied.
(See spec.core for more details)
source

ifclj/s

(if pred if-schema else-schema)

if the predicate returns truthy, use the if-schema, otherwise use the else-schema

if the predicate returns truthy, use the if-schema, otherwise use the else-schema
sourceraw docstring

Instclj/s

The local representation of #inst ...

The local representation of #inst ...
sourceraw docstring

instance-preconditionclj/s

(instance-precondition s klass)
source

instrument-defprotocol?clj/s

(instrument-defprotocol?)

If true, elide s/defprotocol instrumentation.

Instrumentation is elided for any of the following cases:

  • @elide-defprotocol-instrumentation is true during s/defprotocol macroexpansion
  • @elide-defprotocol-instrumentation is true during s/defprotocol evaluation
If true, elide s/defprotocol instrumentation.

Instrumentation is elided for any of the following cases:
*   @*elide-defprotocol-instrumentation* is true during s/defprotocol macroexpansion
*   @*elide-defprotocol-instrumentation* is true during s/defprotocol evaluation
sourceraw docstring

Intclj/s

Any integral number

Any integral number
sourceraw docstring

Isaclj/s

source

isaclj/s

(isa parent)
(isa h parent)

A value that must be a child of parent.

A value that must be a child of parent.
sourceraw docstring

Keywordclj/s

A keyword

A keyword
sourceraw docstring

letfnclj/smacro

(letfn fnspecs & body)

s/letfn : s/fn :: clojure.core/letfn : clojure.core/fn

Gotchas:

  • s/fn-schema will only work on direct references to the bindings inside the body. It will not work on intermediate calls between bindings.
s/letfn : s/fn :: clojure.core/letfn : clojure.core/fn

Gotchas:
- s/fn-schema will only work on direct references to the bindings
  inside the body. It will not work on intermediate calls between bindings.
sourceraw docstring

make-fn-schemaclj/s

(make-fn-schema output-schema input-schemas)

A function outputting a value in output schema, whose argument vector must match one of input-schemas, each of which should be a sequence schema. Currently function schemas are purely descriptive; they validate against any function, regardless of actual input and output types.

A function outputting a value in output schema, whose argument vector must match one of
input-schemas, each of which should be a sequence schema.
Currently function schemas are purely descriptive; they validate against any function,
regardless of actual input and output types.
sourceraw docstring

map-entryclj/s

(map-entry key-schema val-schema)
source

map-entry-ctorclj/s

(map-entry-ctor [k v :as coll])
source

MapEntryclj/s

source

maybeclj/s

(maybe schema)

A value that must either be nil or satisfy schema

A value that must either be nil or satisfy schema
sourceraw docstring

Maybeclj/s

source

namedclj/s

(named schema name)

A value that must satisfy schema, and has a name for documentation purposes.

A value that must satisfy schema, and has a name for documentation purposes.
sourceraw docstring

NamedSchemaclj/s

source

Numclj/s

Any number

Any number
sourceraw docstring

Oneclj/s

source

oneclj/s

(one schema name)

A single required element of a sequence (not repeated, the implicit default)

A single required element of a sequence (not repeated, the implicit default)
sourceraw docstring

optionalclj/s

(optional schema name)

A single optional element of a sequence (not repeated, the implicit default)

A single optional element of a sequence (not repeated, the implicit default)
sourceraw docstring

optional-keyclj/s

(optional-key k)

An optional key in a map

An optional key in a map
sourceraw docstring

optional-key?clj/s

(optional-key? ks)
source

OptionalKeyclj/s

source

pairclj/s

(pair first-schema first-name second-schema second-name)

A schema for a pair of schemas and their names

A schema for a pair of schemas and their names
sourceraw docstring

parse-sequence-schemaclj/s

(parse-sequence-schema s)
source

predclj/s

(pred p?)
(pred p? pred-name)

A value for which p? returns true (and does not throw). Optional pred-name can be passed for nicer validation errors.

A value for which p? returns true (and does not throw).
Optional pred-name can be passed for nicer validation errors.
sourceraw docstring

Predicateclj/s

source

protocolclj/smacro

(protocol p)

A value that must satsify? protocol p.

Internaly, we must make sure not to capture the value of the protocol at schema creation time, since that's impossible in cljs and breaks later extends in Clojure.

A macro for cljs sake, since satisfies? is a macro in cljs.

A value that must satsify? protocol p.

Internaly, we must make sure not to capture the value of the protocol at
schema creation time, since that's impossible in cljs and breaks later
extends in Clojure.

A macro for cljs sake, since `satisfies?` is a macro in cljs.
sourceraw docstring

Protocolclj/s

source

protocol-nameclj/s

(protocol-name protocol)
source

Queueclj/s

source

queueclj/s

(queue x)

Defines a schema satisfied by instances of clojure.lang.PersistentQueue (clj.core/PersistentQueue in ClojureScript) whose values satisfy x.

Defines a schema satisfied by instances of clojure.lang.PersistentQueue
(clj.core/PersistentQueue in ClojureScript) whose values satisfy x.
sourceraw docstring

queue?clj/s

(queue? x)
source

recordclj/smacro

(record klass schema)
(record klass schema map-constructor)

A Record instance of type klass, whose elements match map schema 'schema'.

The final argument is the map constructor of the record type; if you do not pass one, an attempt is made to find the corresponding function (but this may fail in exotic circumstances).

A Record instance of type klass, whose elements match map schema 'schema'.

The final argument is the map constructor of the record type; if you do
not pass one, an attempt is made to find the corresponding function
(but this may fail in exotic circumstances).
sourceraw docstring

Recordclj/s

source

record*clj/s

(record* klass schema map-constructor)
source

recursiveclj/s

(recursive schema)

Support for (mutually) recursive schemas by passing a var that points to a schema, e.g (recursive #'ExampleRecursiveSchema).

Support for (mutually) recursive schemas by passing a var that points to a schema,
e.g (recursive #'ExampleRecursiveSchema).
sourceraw docstring

Recursiveclj/s

source

Regexclj/s

A regular expression

A regular expression
sourceraw docstring

required-keyclj/s

(required-key k)

A required key in a map

A required key in a map
sourceraw docstring

required-key?clj/s

(required-key? ks)
source

RequiredKeyclj/s

source

Schemaclj/sprotocol

explainclj/s

(explain this)

Expand this schema to a human-readable format suitable for pprinting, also expanding class schematas at the leaves. Example:

user> (s/explain {:a s/Keyword :b [s/Int]} ) {:a Keyword, :b [Int]}

Expand this schema to a human-readable format suitable for pprinting,
also expanding class schematas at the leaves.  Example:

user> (s/explain {:a s/Keyword :b [s/Int]} )
{:a Keyword, :b [Int]}

specclj/s

(spec this)

A spec is a record of some type that expresses the structure of this schema in a declarative and/or imperative way. See schema.spec.* for examples.

A spec is a record of some type that expresses the structure of this schema
in a declarative and/or imperative way.  See schema.spec.* for examples.
source

schema-nameclj/s

(schema-name schema)

Returns the name of a schema attached via schema-with-name (or defschema).

Returns the name of a schema attached via schema-with-name (or defschema).
sourceraw docstring

schema-nsclj/s

(schema-ns schema)

Returns the namespace of a schema attached via defschema.

Returns the namespace of a schema attached via defschema.
sourceraw docstring

schema-with-nameclj/s

(schema-with-name schema name)

Records name in schema's metadata.

Records name in schema's metadata.
sourceraw docstring

schematize-fnclj/s

(schematize-fn f schema)

Attach the schema to fn f at runtime, extractable by fn-schema.

Attach the schema to fn f at runtime, extractable by fn-schema.
sourceraw docstring

set-compile-fn-validation!clj/smacro

(set-compile-fn-validation! on?)
source

set-fn-validation!clj/s

(set-fn-validation! on?)

Globally turn on (or off) schema validation for all s/fn and s/defn instances.

Globally turn on (or off) schema validation for all s/fn and s/defn instances.
sourceraw docstring

set-max-value-length!clj/s

(set-max-value-length! max-length)

Sets the maximum length of value to be output before it is contracted to a prettier name.

Sets the maximum length of value to be output before it is contracted to a prettier name.
sourceraw docstring

specific-key?clj/s

(specific-key? ks)
source

Strclj/s

Satisfied only by String. Is (pred string?) and not js/String in cljs because of keywords.

Satisfied only by String.
Is (pred string?) and not js/String in cljs because of keywords.
sourceraw docstring

Symbolclj/s

A symbol

A symbol
sourceraw docstring

Uuidclj/s

The local representation of #uuid ...

The local representation of #uuid ...
sourceraw docstring

validateclj/s

(validate schema value)

Throw an exception if value does not satisfy schema; otherwise, return value. If you will be validating many datums, it is much more efficient to create a 'validator' once and call it on each of them.

Throw an exception if value does not satisfy schema; otherwise, return value.
If you will be validating many datums, it is much more efficient to create
a 'validator' once and call it on each of them.
sourceraw docstring

validatorclj/s

(validator schema)

Compile an efficient validator for schema.

Compile an efficient validator for schema.
sourceraw docstring

var-nameclj/s

(var-name v)
source

with-fn-validationclj/smacro

(with-fn-validation & body)

Execute body with input and output schema validation turned on for all s/defn and s/fn instances globally (across all threads). After all forms have been executed, resets function validation to its previously set value. Not concurrency-safe.

Execute body with input and output schema validation turned on for
all s/defn and s/fn instances globally (across all threads). After
all forms have been executed, resets function validation to its
previously set value. Not concurrency-safe.
sourceraw docstring

without-fn-validationclj/smacro

(without-fn-validation & body)

Execute body with input and output schema validation turned off for all s/defn and s/fn instances globally (across all threads). After all forms have been executed, resets function validation to its previously set value. Not concurrency-safe.

Execute body with input and output schema validation turned off for
all s/defn and s/fn instances globally (across all threads). After
all forms have been executed, resets function validation to its
previously set value. Not concurrency-safe.
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close