schemallic.malli integrates Schemallic schemas with the malli ecosystem.
It provides validation, coercion, encoding/decoding, introspection, and
integration with tools like reitit. All functions work with schemas
defined by s/def and with plain maps built from Schemallic types.
(require '[schemallic.core :as s]
'[schemallic.malli :as m]
'[schemallic.gen :as g])
(s/def User {:id s/Int :name s/Str})
Validation:
(m/validate User {:id 1 :name "John"})
(m/explain User {:id "invalid"})
These return true and an error map.
Coercion from external data:
(m/coerce User {:id "42" :name "Bob"})
Returns {:id 42 :name "Bob"}.
Generated data:
(m/validate User (g/gen-data User))
Introspection:
(m/form User)
(m/type User)
These return [:map {:closed true} [:id :int] [:name :string]] and :map.
Encoding and decoding:
(m/encode User {:id 42 :name "Bob"})
(m/decode User {:id "42" :name "Bob"})
Both return {:id "42" :name "Bob"}.
Reusable functions for high-throughput paths:
(def encode-user (m/encoder User))
(def decode-user (m/decoder User))
(def coerce-user (m/coercer User))
JSON Schema output:
(m/json-schema User)
Validation:
validate (boolean)explain (error details)humanize (readable errors)Serialization:
encode, encoderdecode, decodercoerce, coercerIntrospection:
formtypejson-schema`schemallic.malli` integrates Schemallic schemas with the `malli` ecosystem.
It provides validation, coercion, encoding/decoding, introspection, and
integration with tools like `reitit`. All functions work with schemas
defined by `s/def` and with plain maps built from Schemallic types.
## Quick Start
```clojure
(require '[schemallic.core :as s]
'[schemallic.malli :as m]
'[schemallic.gen :as g])
(s/def User {:id s/Int :name s/Str})
```
Validation:
```clojure
(m/validate User {:id 1 :name "John"})
(m/explain User {:id "invalid"})
```
These return `true` and an error map.
Coercion from external data:
```clojure
(m/coerce User {:id "42" :name "Bob"})
```
Returns `{:id 42 :name "Bob"}`.
Generated data:
```clojure
(m/validate User (g/gen-data User))
```
Introspection:
```clojure
(m/form User)
(m/type User)
```
These return `[:map {:closed true} [:id :int] [:name :string]]` and `:map`.
Encoding and decoding:
```clojure
(m/encode User {:id 42 :name "Bob"})
(m/decode User {:id "42" :name "Bob"})
```
Both return `{:id "42" :name "Bob"}`.
Reusable functions for high-throughput paths:
```clojure
(def encode-user (m/encoder User))
(def decode-user (m/decoder User))
(def coerce-user (m/coercer User))
```
JSON Schema output:
```clojure
(m/json-schema User)
```
## Functions
Validation:
- `validate` (boolean)
- `explain` (error details)
- `humanize` (readable errors)
Serialization:
- `encode`, `encoder`
- `decode`, `decoder`
- `coerce`, `coercer`
Introspection:
- `form`
- `type`
- `json-schema`
(coerce schema value)(coerce schema value reg)Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)]) Returns: s/Any
Coerce (decode + validate) a value to match a Schemallic schema.
Like malli's coerce: transforms AND validates constraints, throws on failure.
(s/def User {:id s/Int :name s/Str :age ^{:min 0} s/Int})
(m/coerce User {:id "42" :name "Bob" :age "25"})
(m/coerce User {:id "invalid" :name "Bob" :age "25"})
Result: {:id 42 :name "Bob" :age 25}; the invalid input throws ExceptionInfo.
Args: schema, value, optional registry
Returns: Coerced and validated value
Throws: ExceptionInfo if validation fails
Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)])
Returns: s/Any
Coerce (decode + validate) a value to match a Schemallic schema.
Like `malli`'s coerce: transforms AND validates constraints, throws on failure.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :age ^{:min 0} s/Int})
(m/coerce User {:id "42" :name "Bob" :age "25"})
(m/coerce User {:id "invalid" :name "Bob" :age "25"})
```
**Result:** `{:id 42 :name "Bob" :age 25}`; the invalid input throws ExceptionInfo.
**Args:** `schema`, `value`, *optional* `registry`
**Returns:** Coerced and validated value
**Throws:** ExceptionInfo if validation fails(coercer schema)(coercer schema reg)Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)]) Returns: (s/maybe s/Any)
Returns a reusable coercer function for the schema.
Like malli's coercer: creates a function that decodes AND validates, throws on failure.
Create once at startup, call per-request for performance.
(def coerce-user (m/coercer User))
(coerce-user {:id "42" :name "Bob" :age "25"})
(coerce-user {:id "invalid" :name "Bob"})
Result: {:id 42 :name "Bob" :age 25}; the invalid input throws ExceptionInfo.
Args: schema, optional registry
Returns: Function [value] -> coerced-and-validated-value
Throws: ExceptionInfo if validation fails
Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)])
Returns: (s/maybe s/Any)
Returns a reusable coercer function for the schema.
Like `malli`'s coercer: creates a function that decodes AND validates, throws on failure.
Create once at startup, call per-request for performance.
## Examples
```clojure
(def coerce-user (m/coercer User))
(coerce-user {:id "42" :name "Bob" :age "25"})
(coerce-user {:id "invalid" :name "Bob"})
```
**Result:** `{:id 42 :name "Bob" :age 25}`; the invalid input throws ExceptionInfo.
**Args:** `schema`, *optional* `registry`
**Returns:** Function [value] -> coerced-and-validated-value
**Throws:** ExceptionInfo if validation fails(composite-registry & registries)Creates a composite Malli registry.
Combines registries, where earlier registries take precedence. Useful for
adding custom types on top of default-registry.
(require '[malli.core :as mc]
'[schemallic.malli :as m])
(def pos-int-type (mc/-simple-schema {:type :pos-int :pred pos-int?}))
(def reg (m/composite-registry {:pos-int pos-int-type} m/default-registry))
Args: & registries
Returns: Malli registry
Creates a composite Malli registry.
Combines registries, where earlier registries take precedence. Useful for
adding custom types on top of `default-registry`.
## Example
```clojure
(require '[malli.core :as mc]
'[schemallic.malli :as m])
(def pos-int-type (mc/-simple-schema {:type :pos-int :pred pos-int?}))
(def reg (m/composite-registry {:pos-int pos-int-type} m/default-registry))
```
**Args:** `& registries`
**Returns:** Malli registry(decode schema value)(decode schema value reg)Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)]) Returns: s/Any
Decode (transform) a value to match a Schemallic schema using Malli transformers. Transforms string inputs to appropriate types (int, double, boolean, uuid, inst, keyword, etc.), applies default values, and handles collections. Constraints are not validated.
(s/def User {:id s/Int :name s/Str :age s/Int})
(m/decode User {:id "42" :name "Bob" :age "25"})
(s/def Event {:id s/Uuid :timestamp s/Inst})
(m/decode Event {:id "550e8400-e29b-41d4-a716-446655440000" :timestamp "2024-01-01T00:00:00Z"})
Result: {:id 42 :name "Bob" :age 25}, then {:id #uuid "550e8400-e29b-41d4-a716-446655440000", :timestamp #object[java.time.Instant "2024-01-01T00:00:00Z"]}.
Args: schema, value, optional registry
Returns: Decoded value (constraints not validated)
Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)])
Returns: s/Any
Decode (transform) a value to match a Schemallic schema using Malli transformers.
Transforms string inputs to appropriate types (int, double, boolean, uuid, inst, keyword, etc.),
applies default values, and handles collections. Constraints are *not* validated.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :age s/Int})
(m/decode User {:id "42" :name "Bob" :age "25"})
(s/def Event {:id s/Uuid :timestamp s/Inst})
(m/decode Event {:id "550e8400-e29b-41d4-a716-446655440000" :timestamp "2024-01-01T00:00:00Z"})
```
**Result:** `{:id 42 :name "Bob" :age 25}`, then `{:id #uuid "550e8400-e29b-41d4-a716-446655440000", :timestamp #object[java.time.Instant "2024-01-01T00:00:00Z"]}`.
**Args:** `schema`, `value`, *optional* `registry`
**Returns:** Decoded value (constraints *not* validated)(decoder schema)(decoder schema reg)Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)]) Returns: (s/maybe s/Any)
Returns a reusable decoder function for the schema. Create once at startup, call per-request for performance.
(def decode-user (m/decoder User))
(decode-user {:id "42" :name "Bob"})
Result: {:id 42 :name "Bob"}.
Args: schema, optional registry
Returns: Function [value] -> decoded-value
Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)])
Returns: (s/maybe s/Any)
Returns a reusable decoder function for the schema.
Create once at startup, call per-request for performance.
## Examples
```clojure
(def decode-user (m/decoder User))
(decode-user {:id "42" :name "Bob"})
```
**Result:** `{:id 42 :name "Bob"}`.
**Args:** `schema`, *optional* `registry`
**Returns:** Function [value] -> decoded-valueDefault Schemallic Malli registry.
Includes Schemallic custom types (for example :inst, :number) on top of Malli's
default registry. Pass it when using raw Malli/reitit coercion APIs.
(require '[schemallic.malli :as m]
'[reitit.coercion.malli :as malli-coercion])
(malli-coercion/create
{:options {:registry m/default-registry}})
Default Schemallic Malli registry.
Includes Schemallic custom types (for example `:inst`, `:number`) on top of Malli's
default registry. Pass it when using raw Malli/reitit coercion APIs.
## Example
```clojure
(require '[schemallic.malli :as m]
'[reitit.coercion.malli :as malli-coercion])
(malli-coercion/create
{:options {:registry m/default-registry}})
```(encode schema value)(encode schema value reg)Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)]) Returns: s/Any
Encode (transform) a value for external representation (JSON, query params, etc.). Inverse of decode. Constraints are not validated.
(s/def User {:id s/Int :name s/Str})
(m/encode User {:id 42 :name "Bob"})
Result: {:id "42" :name "Bob"}.
Args: schema, value, optional registry
Returns: Encoded value
Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)])
Returns: s/Any
Encode (transform) a value for external representation (JSON, query params, etc.).
Inverse of decode. Constraints are *not* validated.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(m/encode User {:id 42 :name "Bob"})
```
**Result:** `{:id "42" :name "Bob"}`.
**Args:** `schema`, `value`, *optional* `registry`
**Returns:** Encoded value(encoder schema)(encoder schema reg)Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)]) Returns: (s/maybe s/Any)
Returns a reusable encoder function for the schema. Create once at startup, call per-request for performance.
(def encode-user (m/encoder User))
(encode-user {:id 42 :name "Bob"})
Result: {:id "42" :name "Bob"}.
Args: schema, optional registry
Returns: Function [value] -> encoded-value
Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)])
Returns: (s/maybe s/Any)
Returns a reusable encoder function for the schema.
Create once at startup, call per-request for performance.
## Examples
```clojure
(def encode-user (m/encoder User))
(encode-user {:id 42 :name "Bob"})
```
**Result:** `{:id "42" :name "Bob"}`.
**Args:** `schema`, *optional* `registry`
**Returns:** Function [value] -> encoded-value(explain schema value)(explain schema value reg)Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)]) Returns: (s/maybe #:s{Keyword s/Any})
Returns nil if value is valid, or a detailed error map if not.
(s/def User {:id s/Int :name s/Str})
(m/explain User {:id 1 :name "John"})
(m/explain User {:id "x" :name "John"})
Result: nil for the valid value; for the invalid value, an error map shaped like {:schema [...] :value {...} :errors [...]}.
Args: schema, value, optional registry
Returns: nil if valid, error map if invalid
Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)])
Returns: (s/maybe #:s{Keyword s/Any})
Returns nil if value is valid, or a detailed error map if not.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(m/explain User {:id 1 :name "John"})
(m/explain User {:id "x" :name "John"})
```
**Result:** `nil` for the valid value; for the invalid value, an error map shaped like `{:schema [...] :value {...} :errors [...]}`.
**Args:** `schema`, `value`, *optional* `registry`
**Returns:** nil if valid, error map if invalid(form schema)(form schema reg)Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)]) Returns: [s/Any]
Returns the Malli form representation of a schema.
(s/def User {:id s/Int :name s/Str})
(m/form User)
Result: [:map {:closed true} [:id :int] [:name :string]].
Args: schema, optional registry
Returns: Malli form vector
Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)])
Returns: [s/Any]
Returns the Malli form representation of a schema.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(m/form User)
```
**Result:** `[:map {:closed true} [:id :int] [:name :string]]`.
**Args:** `schema`, *optional* `registry`
**Returns:** Malli form vector(humanize schema value)(humanize schema value reg)Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)]) Returns: (s/maybe #:s{Any [s/Str]})
Returns human-readable error messages as a path -> messages map. Uses the same message generator as valued schemas (s/def :-).
(s/def User {:id s/Int :name s/Str})
(m/humanize User {:id 1 :name "John"})
(m/humanize User {:id "x" :name "John"})
(s/def PositiveAge {:age ^{:positive true} s/Int})
(m/humanize PositiveAge {:age -5})
Result: nil for the valid value, {:id ["should be an integer"]} for the invalid one, and {:age ["must be positive"]} for the constrained age.
Args: schema, value, optional registry
Returns: nil if valid, map of entry path -> [message] values if invalid
Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)])
Returns: (s/maybe #:s{Any [s/Str]})
Returns human-readable error messages as a path -> messages map.
Uses the same message generator as valued schemas (s/def :-).
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(m/humanize User {:id 1 :name "John"})
(m/humanize User {:id "x" :name "John"})
(s/def PositiveAge {:age ^{:positive true} s/Int})
(m/humanize PositiveAge {:age -5})
```
**Result:** `nil` for the valid value, `{:id ["should be an integer"]}` for the invalid one, and `{:age ["must be positive"]}` for the constrained age.
**Args:** `schema`, `value`, *optional* `registry`
**Returns:** nil if valid, map of entry path -> [message] values if invalid(json-schema schema)(json-schema schema reg)Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)]) Returns: s/Any
Generate JSON Schema from a Schemallic schema. Useful for API documentation, client generation, etc.
(s/def User {:id s/Int :name s/Str :email ^{:re #"^.*@.*$"} s/Str})
(m/json-schema User)
Result: {"type" "object", "properties" {"id" {"type" "integer"}, ...}, "required" ["id" "name" "email"]}.
Args: schema, optional registry
Returns: JSON Schema map
Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)])
Returns: s/Any
Generate JSON Schema from a Schemallic schema.
Useful for API documentation, client generation, etc.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :email ^{:re #"^.*@.*$"} s/Str})
(m/json-schema User)
```
**Result:** `{"type" "object", "properties" {"id" {"type" "integer"}, ...}, "required" ["id" "name" "email"]}`.
**Args:** `schema`, *optional* `registry`
**Returns:** JSON Schema map(type schema)(type schema reg)Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)]) Returns: s/Keyword
Returns the Malli type of a schema.
(s/def User {:id s/Int :name s/Str})
(m/type User)
(m/type s/Int)
Result: :map and :int.
Args: schema, optional registry
Returns: Malli type keyword
Inputs: ([schema :- SObject] [schema :- SObject reg :- (s/maybe s/Any)])
Returns: s/Keyword
Returns the Malli type of a schema.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(m/type User)
(m/type s/Int)
```
**Result:** `:map` and `:int`.
**Args:** `schema`, *optional* `registry`
**Returns:** Malli type keyword(validate schema value)(validate schema value reg)Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)]) Returns: s/Bool
Validates a value against a schema. Returns true if valid, false otherwise.
(s/def User {:id s/Int :name s/Str})
(m/validate User {:id 1 :name "John"})
(m/validate User {:id "x" :name "John"})
Result: true, false.
Args: schema, value, optional registry
Returns: Boolean
Inputs: ([schema :- SObject value :- s/Any] [schema :- SObject value :- s/Any reg :- (s/maybe s/Any)])
Returns: s/Bool
Validates a value against a schema. Returns true if valid, false otherwise.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(m/validate User {:id 1 :name "John"})
(m/validate User {:id "x" :name "John"})
```
**Result:** `true`, `false`.
**Args:** `schema`, `value`, *optional* `registry`
**Returns:** Booleancljdoc 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 |