schemallic = schema + malli + pydantic
Schemallic unifies schema.core contracts, malli machinery, and pydantic-like validations — plus much more.
Schemallic is a cross-ecosystem schema toolkit for Clojure: it keeps the schema.core
syntax you already know, adds pydantic-style inline constraints, and runs validation,
coercion and schema ops through malli. It generates random schemas and seeded data,
compares and combines schemas, and offers optional reitit integration.
A semantic bridge between schema.core and malli.
(require '[schemallic.core :as s]
'[schemallic.malli :as m]
'[schemallic.ops :as o]
'[schemallic.gen :as g])
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def CreateUserRequest {:name s/Str :email s/Str})
(s/def current-user :- User {:id 1 :name "Alice" :email "alice@example.com"})
(s/def max-retries :- s/Int 3)
(s/defn create-user :- User
[req :- CreateUserRequest]
{:id (rand-int 1000)
:name (:name req)
:email (:email req)})
Once schemas are defined here, the rest of the ecosystem operates on them.
schemallic.malli validates data and introspects schemas:
(m/validate User {:id 1 :name "Alice" :email "alice@example.com"})
(m/explain User {:id "bad"})
(m/form User)
These return true, an error map, and [:map {:closed true} [:id :int] ...].
schemallic.ops compares and combines schemas:
(o/equal User CreateUserRequest)
(o/extend User {:age s/Int})
(o/merge User {:id s/Str})
These return false, then Malli map schemas.
schemallic.gen generates random schemas and test data:
(g/gen-data User 42)
(g/gen-data User {:id 99} 42)
(g/gen-schema 3 2)
schemallic.core provides schema wrappers, combinators, and macros (def, defn, =>).
Schema:
Int (integer)Str (string)Num (number: int, float, ratio)Bool (true or false)Any (any value)Keyword (keyword)Symbol (symbol)Inst (java.util.Date / #inst)Uuid (java.util.UUID / #uuid)pred (arbitrary predicate)eq (exact value equality)maybe (nil or schema)enum (one of values)cond-pre (conditional on type)constrained (schema with post-condition)conditional (explicit predicate/schema pairs)either (union, deprecated)both (intersection, deprecated)recursive (self-referential schema)optional-key (optional map key)=> (function schema)Schemallic:
def (schema, validated var, or regular var)defn (validated function with args and return)Numeric:
:gt :lt :ge :le :min :max :multiple-of :positive :negative :even :odd:max-digits :min-digits :decimal-places :divisor-of :finite
String::min-length :max-length :pattern :re :starts-with :ends-with :contains:not-contains :lower-case :upper-case :non-blank :blank :format
Collection::min-count :max-count :non-empty :empty :distinct:unique-items :unique :sorted :sorted-desc :contains
Generic::pred :fnUsage:
^{:min 0 :max 100} s/Int
{:age ^{:min 18} s/Int}
`schemallic` = `schema` + `malli` + `pydantic`
Schemallic unifies `schema.core` contracts, `malli` machinery, and `pydantic`-like validations — plus *much more*.
Schemallic is a cross-ecosystem *schema* toolkit for Clojure: it keeps the `schema.core`
syntax you already know, adds `pydantic`-style inline constraints, and runs validation,
coercion and schema ops through `malli`. It generates random schemas and seeded data,
compares and combines schemas, and offers *optional* `reitit` integration.
A *semantic bridge* between `schema.core` and `malli`.
## Quick Start
```clojure
(require '[schemallic.core :as s]
'[schemallic.malli :as m]
'[schemallic.ops :as o]
'[schemallic.gen :as g])
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def CreateUserRequest {:name s/Str :email s/Str})
(s/def current-user :- User {:id 1 :name "Alice" :email "alice@example.com"})
(s/def max-retries :- s/Int 3)
(s/defn create-user :- User
[req :- CreateUserRequest]
{:id (rand-int 1000)
:name (:name req)
:email (:email req)})
```
Once schemas are defined here, the rest of the ecosystem operates on them.
`schemallic.malli` validates data and introspects schemas:
```clojure
(m/validate User {:id 1 :name "Alice" :email "alice@example.com"})
(m/explain User {:id "bad"})
(m/form User)
```
These return `true`, an error map, and `[:map {:closed true} [:id :int] ...]`.
`schemallic.ops` compares and combines schemas:
```clojure
(o/equal User CreateUserRequest)
(o/extend User {:age s/Int})
(o/merge User {:id s/Str})
```
These return `false`, then Malli map schemas.
`schemallic.gen` generates random schemas and test data:
```clojure
(g/gen-data User 42)
(g/gen-data User {:id 99} 42)
(g/gen-schema 3 2)
```
`schemallic.core` provides schema wrappers, combinators, and macros (`def`, `defn`, `=>`).
## Wrappers
Schema:
- `Int` (integer)
- `Str` (string)
- `Num` (number: int, float, ratio)
- `Bool` (true or false)
- `Any` (any value)
- `Keyword` (keyword)
- `Symbol` (symbol)
- `Inst` (`java.util.Date` / `#inst`)
- `Uuid` (`java.util.UUID` / `#uuid`)
- `pred` (arbitrary predicate)
- `eq` (exact value equality)
- `maybe` (nil or schema)
- `enum` (one of values)
- `cond-pre` (conditional on type)
- `constrained` (schema with post-condition)
- `conditional` (explicit predicate/schema pairs)
- `either` (union, deprecated)
- `both` (intersection, deprecated)
- `recursive` (self-referential schema)
- `optional-key` (optional map key)
- `=>` (function schema)
## Macros
Schemallic:
- `def` (schema, validated var, or regular var)
- `defn` (validated function with args and return)
## Props
Numeric:
- `:gt :lt :ge :le :min :max :multiple-of :positive :negative :even :odd`
- `:max-digits :min-digits :decimal-places :divisor-of :finite`
String:
- `:min-length :max-length :pattern :re :starts-with :ends-with :contains`
- `:not-contains :lower-case :upper-case :non-blank :blank :format`
Collection:
- `:min-count :max-count :non-empty :empty :distinct`
- `:unique-items :unique :sorted :sorted-desc :contains`
Generic:
- `:pred :fn`
Usage:
```clojure
^{:min 0 :max 100} s/Int
{:age ^{:min 18} s/Int}
```(=> & args)Defines a function schema. Produces a Malli [:-> ...] schema directly.
(s/=> s/Int s/Int s/Int)
(s/=> s/Str s/Str)
These produce [:-> :int :int :int] and [:-> :string :string].
(s/=> (=> s/Str s/Str)
(=> s/Str s/Str s/Int))
Produces [:function [:-> :string :string] [:-> :string :int :string]].
s/def(s/def AddFn (s/=> s/Int s/Int s/Int))
(s/def ParseFn (s/=> (=> s/Str s/Str)
(=> s/Str s/Str s/Int)))
Args: in single-arity mode, first arg is the return type and the rest are arg types.
In multi-arity mode, each arg is an (=> ReturnType ArgType ...) form.
Defines a function schema. Produces a Malli `[:-> ...]` schema directly.
## Single arity
```clojure
(s/=> s/Int s/Int s/Int)
(s/=> s/Str s/Str)
```
These produce `[:-> :int :int :int]` and `[:-> :string :string]`.
## Multi arity
```clojure
(s/=> (=> s/Str s/Str)
(=> s/Str s/Str s/Int))
```
Produces `[:function [:-> :string :string] [:-> :string :int :string]]`.
## With `s/def`
```clojure
(s/def AddFn (s/=> s/Int s/Int s/Int))
(s/def ParseFn (s/=> (=> s/Str s/Str)
(=> s/Str s/Str s/Int)))
```
**Args:** in single-arity mode, first arg is the return type and the rest are arg types.
In multi-arity mode, each arg is an `(=> ReturnType ArgType ...)` form.Any value, including nil. Use as a wildcard or escape hatch.
(s/def Metadata {s/Keyword s/Any})
(m/validate Metadata {:foo 1 :bar "x" :baz nil})
Result: true.
Any value, including nil. Use as a wildcard or escape hatch.
## Examples
```clojure
(s/def Metadata {s/Keyword s/Any})
(m/validate Metadata {:foo 1 :bar "x" :baz nil})
```
**Result:** `true`.Boolean true or false. Does not coerce truthy/falsy values.
(s/def IsActive s/Bool)
(m/validate IsActive true)
(m/validate IsActive false)
(m/validate IsActive 1)
(m/validate IsActive nil)
Result: true, true, false, false.
Boolean true or false. Does not coerce truthy/falsy values. ## Examples ```clojure (s/def IsActive s/Bool) (m/validate IsActive true) (m/validate IsActive false) (m/validate IsActive 1) (m/validate IsActive nil) ``` **Result:** `true`, `true`, `false`, `false`.
A value that must satisfy every schema in schemas.
Deprecated in schema.core, but fully supported by Schemallic.
Prefer constrained for new code.
(s/def PosInt (s/both s/Int (s/pred pos?)))
(m/validate PosInt 5)
(m/validate PosInt -1)
Result: true, false.
A value that must satisfy every schema in schemas. Deprecated in `schema.core`, but fully supported by Schemallic. Prefer `constrained` for new code. ## Examples ```clojure (s/def PosInt (s/both s/Int (s/pred pos?))) (m/validate PosInt 5) (m/validate PosInt -1) ``` **Result:** `true`, `false`.
Conditional schema with explicit predicate/schema pairs.
Converts each pair to [:and [:fn pred] schema] under [:or ...]; a value
passes when any branch matches. No greedy first-match behavior.
(s/def NumOrStr (s/cond-pre integer? s/Int string? s/Str))
(m/validate NumOrStr 42)
(m/validate NumOrStr "hi")
Result: true, true.
Conditional schema with explicit predicate/schema pairs. Converts each pair to `[:and [:fn pred] schema]` under `[:or ...]`; a value passes when any branch matches. No greedy first-match behavior. ## Examples ```clojure (s/def NumOrStr (s/cond-pre integer? s/Int string? s/Str)) (m/validate NumOrStr 42) (m/validate NumOrStr "hi") ``` **Result:** `true`, `true`.
Conditional schema with explicit predicates, like cond for schemas.
Takes pairs of (pred schema) and converts to [:or ...] of
[:and [:fn pred] schema] branches. No match returns false unless :else exists.
(s/def IdOrName (s/conditional integer? s/Int string? s/Str))
(m/validate IdOrName 1)
(m/validate IdOrName "bob")
(s/def Shape (s/conditional #(= :circle (:type %)) Circle
#(= :rect (:type %)) Rect
:else s/Any))
Result: true, true.
Conditional schema with explicit predicates, like `cond` for schemas.
Takes pairs of `(pred schema)` and converts to `[:or ...]` of
`[:and [:fn pred] schema]` branches. No match returns false unless `:else` exists.
## Examples
```clojure
(s/def IdOrName (s/conditional integer? s/Int string? s/Str))
(m/validate IdOrName 1)
(m/validate IdOrName "bob")
(s/def Shape (s/conditional #(= :circle (:type %)) Circle
#(= :rect (:type %)) Rect
:else s/Any))
```
**Result:** `true`, `true`.A schema with an additional post-condition predicate.
Validates the value against the base schema first, then runs the predicate.
Produces better error messages than wrapping with pred alone and works well
with coercion since the predicate runs after coercion.
(s/def PositiveInt (s/constrained s/Int pos?))
(m/validate PositiveInt 5)
(m/validate PositiveInt -1)
Result: true, false.
A schema with an additional post-condition predicate. Validates the value against the base schema first, then runs the predicate. Produces better error messages than wrapping with `pred` alone and works well with coercion since the predicate runs after coercion. ## Examples ```clojure (s/def PositiveInt (s/constrained s/Int pos?)) (m/validate PositiveInt 5) (m/validate PositiveInt -1) ``` **Result:** `true`, `false`.
(def & args)Define a named schema OR a validated variable with schema.core type annotations.
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def Post {:author User :content s/Str :created s/Inst})
Schema definitions are reusable types. Use ^{:closed true} to reject extra
keys and ^{:closed false} or plain maps to allow them:
(s/def StrictUser ^{:closed true} {:id s/Int :name s/Str})
(s/def FlexibleUser ^{:closed false} {:id s/Int :name s/Str})
(s/def username :- s/Str "john")
(s/def config :- {:port s/Int} {:port 3000})
The value is validated at definition time. Metadata and docstrings can be attached to the symbol:
(s/def ^{:doc {:summary "A username"}} username :- s/Str "john")
(s/def username "The user's name" :- s/Str "john")
(s/def ^{:doc {:summary "A username"}} username "The user's name" :- s/Str "john")
(s/def UserProfile
"A validated user profile"
^{:author "team" :version "1.0"}
{:id s/Int :name s/Str :email s/Str})
(s/def UserProfile
"A validated user profile"
(with-meta {:id s/Int :name s/Str :email s/Str}
{:author "team" :version "1.0"}))
(s/def UserProfile
(with-meta {:id s/Int :name s/Str :email s/Str}
{:author "team" :version "1.0"}))
(s/def User {:id s/Int
:name s/Str
:email ^{:required true :doc "Primary email"} s/Str})
(s/def User {:id s/Int
:name ^{:min-length 1 :max-length 50} s/Str
:age ^{:min 0 :max 150} s/Int
:email ^{:pattern "^.*@.*$"} s/Str
:tags ^{:distinct true :min-count 1} [s/Str]})
Schema definitions:
^{} metadata on the schema map^{} metadata to Malli entry propertiesValidated variables:
^{} or with-meta metadata on the var:doc var metadataPlain (name value) forms emit a regular def with no validation. Use
(s/def x :- s/Int 42) for validation; (s/def x 42) does not validate.
Schema definitions put metadata on the schema VALUE and convert it to Malli
properties, such as ^{:closed true} becoming [:map {:closed true} ...].
^{:doc "..."} is copied to var metadata; other keys such as :author and
:version are dropped.
Validated variables put metadata on the SYMBOL, so it survives as Clojure var metadata. It does not become a Malli property because the schema is separate.
Use schema-value metadata for Malli properties and symbol metadata for Clojure tooling. To get both, define a schema with metadata and reference it in a validated variable.
Reusable schemas:
(def name schema)
(def name ^{:closed true} schema)
(def name "docstring" schema)
(def name "docstring" ^{:closed true} schema)
(def name "docstring" (with-meta schema meta))
(def name (with-meta schema meta))
(def name ^{:doc "..."} schema)
Validated variables:
(def name :- schema value)
(def name :- schema "docstring" value)
(def name "docstring" :- schema value)
(def ^{:doc {:any :map}} name :- schema value)
(def ^{:doc {:any :map}} name "docstring" :- schema value)
(def (with-meta name {:doc "desc"}) :- schema value)
(def (with-meta name {:doc {:summary "s"}}) "docstring" :- schema value)
Define a named schema OR a validated variable with `schema.core` type annotations.
## Schema definitions
```clojure
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def Post {:author User :content s/Str :created s/Inst})
```
Schema definitions are reusable types. Use `^{:closed true}` to reject extra
keys and `^{:closed false}` or plain maps to allow them:
```clojure
(s/def StrictUser ^{:closed true} {:id s/Int :name s/Str})
(s/def FlexibleUser ^{:closed false} {:id s/Int :name s/Str})
```
## Validated variables
```clojure
(s/def username :- s/Str "john")
(s/def config :- {:port s/Int} {:port 3000})
```
The value is validated at definition time. Metadata and docstrings can be
attached to the symbol:
```clojure
(s/def ^{:doc {:summary "A username"}} username :- s/Str "john")
(s/def username "The user's name" :- s/Str "john")
(s/def ^{:doc {:summary "A username"}} username "The user's name" :- s/Str "john")
```
## Schema metadata and docstrings
```clojure
(s/def UserProfile
"A validated user profile"
^{:author "team" :version "1.0"}
{:id s/Int :name s/Str :email s/Str})
(s/def UserProfile
"A validated user profile"
(with-meta {:id s/Int :name s/Str :email s/Str}
{:author "team" :version "1.0"}))
(s/def UserProfile
(with-meta {:id s/Int :name s/Str :email s/Str}
{:author "team" :version "1.0"}))
(s/def User {:id s/Int
:name s/Str
:email ^{:required true :doc "Primary email"} s/Str})
```
## Constraint props
```clojure
(s/def User {:id s/Int
:name ^{:min-length 1 :max-length 50} s/Str
:age ^{:min 0 :max 150} s/Int
:email ^{:pattern "^.*@.*$"} s/Str
:tags ^{:distinct true :min-count 1} [s/Str]})
```
## Validation behavior
Schema definitions:
- Register the schema for nested and recursive references
- Apply closed/open map semantics from `^{}` metadata on the schema map
- Convert entry-level `^{}` metadata to Malli entry properties
Validated variables:
- Validate the initial value at definition time
- Throw ExceptionInfo with Malli explanations on failure
- Preserve reader `^{}` or `with-meta` metadata on the var
- Preserve docstrings as `:doc` var metadata
Plain `(name value)` forms emit a regular `def` with no validation. Use
`(s/def x :- s/Int 42)` for validation; `(s/def x 42)` does not validate.
## Metadata placement
Schema definitions put metadata on the schema VALUE and convert it to Malli
properties, such as `^{:closed true}` becoming `[:map {:closed true} ...]`.
`^{:doc "..."}` is copied to var metadata; other keys such as `:author` and
`:version` are dropped.
Validated variables put metadata on the SYMBOL, so it survives as Clojure var
metadata. It does not become a Malli property because the schema is separate.
Use schema-value metadata for Malli properties and symbol metadata for Clojure
tooling. To get both, define a schema with metadata and reference it in a
validated variable.
## Syntax
Reusable schemas:
```clojure
(def name schema)
(def name ^{:closed true} schema)
(def name "docstring" schema)
(def name "docstring" ^{:closed true} schema)
(def name "docstring" (with-meta schema meta))
(def name (with-meta schema meta))
(def name ^{:doc "..."} schema)
```
Validated variables:
```clojure
(def name :- schema value)
(def name :- schema "docstring" value)
(def name "docstring" :- schema value)
(def ^{:doc {:any :map}} name :- schema value)
(def ^{:doc {:any :map}} name "docstring" :- schema value)
(def (with-meta name {:doc "desc"}) :- schema value)
(def (with-meta name {:doc {:summary "s"}}) "docstring" :- schema value)
```(defn fn-name & args)Define a function with schema.core type annotations using malli validation.
Like schema.core/defn, but always validates. Uses malli under the hood for
faster validation and richer error messages.
(s/defn add-numbers :- s/Num
[x :- s/Num y :- s/Num]
(+ x y))
(s/defn create-user :- {:id s/Int :name s/Str :email s/Str}
[name :- s/Str email :- s/Str]
{:id (rand-int 1000) :name name :email email})
All arities share the same return schema:
(s/defn greet :- s/Str
([name :- s/Str]
(str "Hello " name))
([first :- s/Str last :- s/Str]
(str "Hello " first " " last)))
(s/defn greet-user :- s/Str
[{:keys [name age]} :- {:name s/Str :age s/Int}]
(str "Hello " name ", age " age))
(s/defn sum-coords :- s/Int
[[x y z] :- [s/Int s/Int s/Int]]
(+ x y z))
(s/defn format-user :- s/Str
[{:keys [id name]} :- {:id s/Int :name s/Str}
prefix :- s/Str]
(str prefix " #" id " " name))
(s/defn person-address :- s/Str
[{:keys [name] {:keys [street city]} :address} :- {:name s/Str :address {:street s/Str :city s/Str}}]
(str name " lives at " street ", " city))
(s/defn sum-nested :- s/Int
[[x [y z]] :- [s/Int [s/Int s/Int]]]
(+ x y z))
Rest arguments support map, vector, and mixed destructuring:
(s/defn greet-with-opts :- s/Str
[name :- s/Str & {:keys [greeting]} :- {:greeting s/Str}]
(str (or greeting "Hello") ", " name))
(s/defn sum-rest :- s/Int
[x :- s/Int & [y z] :- [s/Int s/Int]]
(+ x (or y 0) (or z 0)))
(s/defn log-event :- s/Str
[event :- s/Keyword & {:keys [level msg] :as opts} :- {:level s/Str :msg s/Str}]
(str (name event) " [" level "] " msg))
:or defaults mark keys with defaults as optional, and :syms works too:
(s/defn greet :- s/Str
[{:keys [name greeting] :or {greeting "Hello"}} :- {:name s/Str :greeting s/Str}]
(str greeting ", " name))
(s/defn get-user :- s/Str
[{user-name 'name user-age 'age} :- {'name s/Str 'age s/Int}]
(str user-name " is " user-age))
(s/def UserInput
{:name ^{:min-length 1 :max-length 50} s/Str
:age ^{:min 0 :max 150} s/Int
:tags ^{:distinct true} [s/Str]})
(s/defn create-user :- {:id s/Int :name s/Str :email s/Str :age s/Int :tags [s/Str]}
[input :- UserInput]
{:id (rand-int 1000)
:name (:name input)
:email "user@example.com"
:age (:age input)
:tags (:tags input)})
Argument validation failed or Return value validation failedAttaches :malli/schema to the var, as malli.dev/start! expects:
(:malli/schema (meta #'add-numbers))
Returns [:-> :number :number :number].
:or-derived optionality is processed recursively through nested destructuring(s/defn fn-name :- ReturnSchema
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
(s/defn fn-name :- ReturnSchema
"docstring"
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
(s/defn ^{:doc {:any :map}} fn-name :- ReturnSchema
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
(s/defn ^{:doc {:any :map}} fn-name :- ReturnSchema
"docstring"
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
Define a function with `schema.core` type annotations using `malli` validation.
Like `schema.core/defn`, but always validates. Uses `malli` under the hood for
faster validation and richer error messages.
## Basic usage
```clojure
(s/defn add-numbers :- s/Num
[x :- s/Num y :- s/Num]
(+ x y))
(s/defn create-user :- {:id s/Int :name s/Str :email s/Str}
[name :- s/Str email :- s/Str]
{:id (rand-int 1000) :name name :email email})
```
## Multi arity
All arities share the same return schema:
```clojure
(s/defn greet :- s/Str
([name :- s/Str]
(str "Hello " name))
([first :- s/Str last :- s/Str]
(str "Hello " first " " last)))
```
## Destructuring
```clojure
(s/defn greet-user :- s/Str
[{:keys [name age]} :- {:name s/Str :age s/Int}]
(str "Hello " name ", age " age))
(s/defn sum-coords :- s/Int
[[x y z] :- [s/Int s/Int s/Int]]
(+ x y z))
(s/defn format-user :- s/Str
[{:keys [id name]} :- {:id s/Int :name s/Str}
prefix :- s/Str]
(str prefix " #" id " " name))
(s/defn person-address :- s/Str
[{:keys [name] {:keys [street city]} :address} :- {:name s/Str :address {:street s/Str :city s/Str}}]
(str name " lives at " street ", " city))
(s/defn sum-nested :- s/Int
[[x [y z]] :- [s/Int [s/Int s/Int]]]
(+ x y z))
```
Rest arguments support map, vector, and mixed destructuring:
```clojure
(s/defn greet-with-opts :- s/Str
[name :- s/Str & {:keys [greeting]} :- {:greeting s/Str}]
(str (or greeting "Hello") ", " name))
(s/defn sum-rest :- s/Int
[x :- s/Int & [y z] :- [s/Int s/Int]]
(+ x (or y 0) (or z 0)))
(s/defn log-event :- s/Str
[event :- s/Keyword & {:keys [level msg] :as opts} :- {:level s/Str :msg s/Str}]
(str (name event) " [" level "] " msg))
```
`:or` defaults mark keys with defaults as optional, and `:syms` works too:
```clojure
(s/defn greet :- s/Str
[{:keys [name greeting] :or {greeting "Hello"}} :- {:name s/Str :greeting s/Str}]
(str greeting ", " name))
(s/defn get-user :- s/Str
[{user-name 'name user-age 'age} :- {'name s/Str 'age s/Int}]
(str user-name " is " user-age))
```
## Constraint props
```clojure
(s/def UserInput
{:name ^{:min-length 1 :max-length 50} s/Str
:age ^{:min 0 :max 150} s/Int
:tags ^{:distinct true} [s/Str]})
(s/defn create-user :- {:id s/Int :name s/Str :email s/Str :age s/Int :tags [s/Str]}
[input :- UserInput]
{:id (rand-int 1000)
:name (:name input)
:email "user@example.com"
:age (:age input)
:tags (:tags input)})
```
## Validation behavior
- Arguments are validated on function entry
- Return value is validated before exit
- Map, vector, mixed, and nested destructuring are validated recursively against their schemas
- Rest args are converted to maps or vectors before validation; nil rest args are skipped
- Failures throw ExceptionInfo with `Argument validation failed` or `Return value validation failed`
## Malli metadata
Attaches `:malli/schema` to the var, as `malli.dev/start!` expects:
```clojure
(:malli/schema (meta #'add-numbers))
```
Returns `[:-> :number :number :number]`.
## Limitations
- Return schema must be on the function name, not the arg vector
- `:or`-derived optionality is processed recursively through nested destructuring
- No final attr-map support on multi-arity functions
## Syntax
```clojure
(s/defn fn-name :- ReturnSchema
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
(s/defn fn-name :- ReturnSchema
"docstring"
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
(s/defn ^{:doc {:any :map}} fn-name :- ReturnSchema
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
(s/defn ^{:doc {:any :map}} fn-name :- ReturnSchema
"docstring"
[arg1 :- Arg1Schema arg2 :- Arg2Schema]
(function-body))
```A value that must satisfy at least one schema in schemas.
Deprecated in schema.core, but fully supported by Schemallic.
Prefer conditional or cond-pre for new code.
(s/def NumOrStr (s/either s/Int s/Str))
(m/validate NumOrStr 42)
(m/validate NumOrStr "hi")
Result: true, true.
A value that must satisfy at least one schema in schemas. Deprecated in `schema.core`, but fully supported by Schemallic. Prefer `conditional` or `cond-pre` for new code. ## Examples ```clojure (s/def NumOrStr (s/either s/Int s/Str)) (m/validate NumOrStr 42) (m/validate NumOrStr "hi") ``` **Result:** `true`, `true`.
A value that must be equal to one of the given options.
(s/def Role (s/enum :admin :user :guest))
(m/validate Role :admin)
(m/validate Role :guest)
(m/validate Role :owner)
(s/def Direction (s/enum "north" "south" "east" "west"))
Result: true, true, false.
A value that must be equal to one of the given options. ## Examples ```clojure (s/def Role (s/enum :admin :user :guest)) (m/validate Role :admin) (m/validate Role :guest) (m/validate Role :owner) (s/def Direction (s/enum "north" "south" "east" "west")) ``` **Result:** `true`, `true`, `false`.
A schema that only accepts a single specific value.
(s/def AdminRole (s/eq :admin))
(m/validate AdminRole :admin)
(m/validate AdminRole :user)
(s/def Zero (s/eq 0))
(m/validate Zero 0)
(m/validate Zero 1)
Result: true, false, then true, false.
A schema that only accepts a single specific value. ## Examples ```clojure (s/def AdminRole (s/eq :admin)) (m/validate AdminRole :admin) (m/validate AdminRole :user) (s/def Zero (s/eq 0)) (m/validate Zero 0) (m/validate Zero 1) ``` **Result:** `true`, `false`, then `true`, `false`.
A java.util.Date / #inst timestamp.
(s/def CreatedAt s/Inst)
(m/validate CreatedAt #inst "2024-01-01")
(m/validate CreatedAt "2024-01-01")
Result: true, false.
A java.util.Date / #inst timestamp. ## Examples ```clojure (s/def CreatedAt s/Inst) (m/validate CreatedAt #inst "2024-01-01") (m/validate CreatedAt "2024-01-01") ``` **Result:** `true`, `false`.
Any integral number (Long, Integer, BigInteger, etc.).
(s/def UserId s/Int)
(m/validate UserId 42)
(m/validate UserId 3.14)
(m/validate UserId "42")
Result: true, false, false.
Any integral number (Long, Integer, BigInteger, etc.). ## Examples ```clojure (s/def UserId s/Int) (m/validate UserId 42) (m/validate UserId 3.14) (m/validate UserId "42") ``` **Result:** `true`, `false`, `false`.
A Clojure keyword.
(s/def Status s/Keyword)
(m/validate Status :active)
(m/validate Status "active")
(m/validate Status :a/b)
Result: true, false, true. Namespaced keywords are valid.
A Clojure keyword. ## Examples ```clojure (s/def Status s/Keyword) (m/validate Status :active) (m/validate Status "active") (m/validate Status :a/b) ``` **Result:** `true`, `false`, `true`. Namespaced keywords are valid.
A value that must either be nil or satisfy the given schema.
(s/def OptionalName (s/maybe s/Str))
(m/validate OptionalName "Alice")
(m/validate OptionalName nil)
(m/validate OptionalName 42)
Result: true, true, false.
A value that must either be nil or satisfy the given schema. ## Examples ```clojure (s/def OptionalName (s/maybe s/Str)) (m/validate OptionalName "Alice") (m/validate OptionalName nil) (m/validate OptionalName 42) ``` **Result:** `true`, `true`, `false`.
Any number (Long, Double, BigDecimal, etc.).
(s/def Price s/Num)
(m/validate Price 9.99)
(m/validate Price 42)
(m/validate Price "9")
Result: true, true, false.
Any number (Long, Double, BigDecimal, etc.). ## Examples ```clojure (s/def Price s/Num) (m/validate Price 9.99) (m/validate Price 42) (m/validate Price "9") ``` **Result:** `true`, `true`, `false`.
Marks a map key as optional. Absent keys are valid, present ones are validated.
(s/def User {:id s/Int :name s/Str (s/optional-key :age) s/Int})
(m/validate User {:id 1 :name "Alice"})
(m/validate User {:id 1 :name "Alice" :age 30})
Result: true, true.
Marks a map key as optional. Absent keys are valid, present ones are validated.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str (s/optional-key :age) s/Int})
(m/validate User {:id 1 :name "Alice"})
(m/validate User {:id 1 :name "Alice" :age 30})
```
**Result:** `true`, `true`.Schema defined by an arbitrary predicate function.
(s/def PositiveInt (s/pred pos? 'pos?))
(m/validate PositiveInt 5)
(m/validate PositiveInt -1)
(m/validate PositiveInt 0)
Result: true, false, false.
Schema defined by an arbitrary predicate function. ## Examples ```clojure (s/def PositiveInt (s/pred pos? 'pos?)) (m/validate PositiveInt 5) (m/validate PositiveInt -1) (m/validate PositiveInt 0) ``` **Result:** `true`, `false`, `false`.
Support for mutually recursive schemas by passing a var that points to a
schema, e.g. (s/recursive #'ExampleRecursiveSchema).
Converts to a self-contained Malli [:schema {:registry ...} key] form whose
self-references resolve locally via [:ref key].
(declare Tree)
(s/def Tree {:value s/Int :children [(s/recursive #'Tree)]})
(m/validate Tree {:value 1 :children [{:value 2 :children []}]})
Result: true.
Support for mutually recursive schemas by passing a var that points to a
schema, e.g. `(s/recursive #'ExampleRecursiveSchema)`.
Converts to a self-contained Malli `[:schema {:registry ...} key]` form whose
self-references resolve locally via `[:ref key]`.
## Examples
```clojure
(declare Tree)
(s/def Tree {:value s/Int :children [(s/recursive #'Tree)]})
(m/validate Tree {:value 1 :children [{:value 2 :children []}]})
```
**Result:** `true`.A string value.
(s/def Username s/Str)
(m/validate Username "alice")
(m/validate Username 42)
(m/validate Username nil)
Result: true, false, false.
A string value. ## Examples ```clojure (s/def Username s/Str) (m/validate Username "alice") (m/validate Username 42) (m/validate Username nil) ``` **Result:** `true`, `false`, `false`.
A Clojure symbol.
(s/def FnName s/Symbol)
(m/validate FnName 'my-fn)
(m/validate FnName :my-fn)
Result: true, false.
A Clojure symbol. ## Examples ```clojure (s/def FnName s/Symbol) (m/validate FnName 'my-fn) (m/validate FnName :my-fn) ``` **Result:** `true`, `false`.
A java.util.UUID / #uuid value.
(s/def TraceId s/Uuid)
(m/validate TraceId #uuid "550e8400-e29b-41d4-a716-446655440000")
(m/validate TraceId "550e8400-e29b-41d4-a716-446655440000")
Result: true, false.
A java.util.UUID / #uuid value. ## Examples ```clojure (s/def TraceId s/Uuid) (m/validate TraceId #uuid "550e8400-e29b-41d4-a716-446655440000") (m/validate TraceId "550e8400-e29b-41d4-a716-446655440000") ``` **Result:** `true`, `false`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |