Liking cljdoc? Tell your friends :D

schemallic.malli

malli ecosystem integration and validation for Schemallic schemas.

This module bridges Schemallic schemas with the malli ecosystem, providing validation, coercion, encoding/decoding, and integration with tools like reitit.

All functions work with both Schemallic-defined schemas (via s/def) and plain Clojure maps using Schemallic types directly.

Quick Start

(require '[schemallic.core :as s] '[schemallic.malli :as m] '[schemallic.gen :as g])

(s/def User {:id s/Int :name s/Str})

;; Validate data against schemas (m/validate User {:id 1 :name "John"}) ; => true (m/explain User {:id "invalid"}) ; => error details

;; Coerce external data (e.g. from HTTP params, JSON) (m/coerce User {:id "42" :name "Bob"}) ; => {:id 42 :name "Bob"}

;; Works great with schemallic.gen for property-based testing (m/validate User (g/gen-data User)) ; => true

;; Introspect schemas (m/form User) ; => [:map {:closed true} [:id :int] [:name :string]] (m/type User) ; => :map

;; Encode/decode for external representation (JSON, query params) (m/encode User {:id 42 :name "Bob"}) ; => {:id "42" :name "Bob"} (m/decode User {:id "42" :name "Bob"}) ; => {: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))

;; Generate JSON Schema for API docs (m/json-schema User) ;; => {"type" "object", "properties" {...}, "required" [...], "additionalProperties" false}

Functions

Validation:

  • validate ; Boolean validation (true/false)
  • explain ; Detailed error explanations (nil if valid)
  • humanize ; Human-readable error messages

Serialization:

  • encode ; Transform internal -> external (JSON, query params)
  • encoder ; Reusable encoder function factory
  • decode ; Transform only (no validation)
  • decoder ; Reusable decoder function factory
  • coerce ; Decode + validate, throws on failure
  • coercer ; Reusable coercer function factory

Introspection:

  • form ; Malli form representation
  • type ; Get Malli type information
  • json-schema ; Generate JSON Schema map
`malli` ecosystem integration and validation for Schemallic schemas.

This module bridges Schemallic schemas with the `malli` ecosystem, providing
validation, coercion, encoding/decoding, and integration with tools like `reitit`.

All functions work with both Schemallic-defined schemas (via s/def) and plain
Clojure maps using Schemallic types directly.

## Quick Start

(require '[schemallic.core :as s]
         '[schemallic.malli :as m]
         '[schemallic.gen :as g])

(s/def User {:id s/Int :name s/Str})

;; Validate data against schemas
(m/validate User {:id 1 :name "John"})   ; => true
(m/explain User {:id "invalid"})          ; => error details

;; Coerce external data (e.g. from HTTP params, JSON)
(m/coerce User {:id "42" :name "Bob"})  ; => {:id 42 :name "Bob"}

;; Works great with schemallic.gen for property-based testing
(m/validate User (g/gen-data User))        ; => true

;; Introspect schemas
(m/form User)   ; => [:map {:closed true} [:id :int] [:name :string]]
(m/type User)   ; => :map

;; Encode/decode for external representation (JSON, query params)
(m/encode User {:id 42 :name "Bob"})       ; => {:id "42" :name "Bob"}
(m/decode User {:id "42" :name "Bob"})   ; => {: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))

;; Generate JSON Schema for API docs
(m/json-schema User)
;; => {"type" "object", "properties" {...}, "required" [...], "additionalProperties" false}

## Functions

Validation:
- validate       ; Boolean validation (true/false)
- explain        ; Detailed error explanations (nil if valid)
- humanize       ; Human-readable error messages

Serialization:
- encode         ; Transform internal -> external (JSON, query params)
- encoder        ; Reusable encoder function factory
- decode         ; Transform only (no validation)
- decoder        ; Reusable decoder function factory
- coerce         ; Decode + validate, throws on failure
- coercer        ; Reusable coercer function factory

Introspection:
- form           ; Malli form representation
- type           ; Get Malli type information
- json-schema    ; Generate JSON Schema map
raw docstring

coerceclj

(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.

Examples:

(s/def User {:id s/Int :name s/Str :age ^{:min 0} s/Int}) (m/coerce User {:id "42" :name "Bob" :age "25"}) ;; => {:id 42 :name "Bob" :age 25}

(m/coerce User {:id "invalid" :name "Bob" :age "25"}) ;; => 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:

  (s/def User {:id s/Int :name s/Str :age ^{:min 0} s/Int})
  (m/coerce User {:id "42" :name "Bob" :age "25"})
  ;; => {:id 42 :name "Bob" :age 25}

  (m/coerce User {:id "invalid" :name "Bob" :age "25"})
  ;; => throws ExceptionInfo

  Args: schema, value, (optional) registry
  Returns: Coerced and validated value
  Throws: ExceptionInfo if validation fails
sourceraw docstring

coercerclj

(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.

Examples:

(def coerce-user (m/coercer User)) (coerce-user {:id "42" :name "Bob" :age "25"}) ;; => {:id 42 :name "Bob" :age 25}

(coerce-user {:id "invalid" :name "Bob"}) ;; => throws ExceptionInfo

Args: schema, (optional) registry Returns: Function [value] -> coerced-and-validated-value

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:

  (def coerce-user (m/coercer User))
  (coerce-user {:id "42" :name "Bob" :age "25"})
  ;; => {:id 42 :name "Bob" :age 25}

  (coerce-user {:id "invalid" :name "Bob"})
  ;; => throws ExceptionInfo

  Args: schema, (optional) registry
  Returns: Function [value] -> coerced-and-validated-value
sourceraw docstring

decodeclj

(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. Does NOT validate constraints.

Examples:

(s/def User {:id s/Int :name s/Str :age s/Int}) (m/decode User {:id "42" :name "Bob" :age "25"}) ;; => {: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"}) ;; => {: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. Does NOT validate constraints.

  ## Examples:

  (s/def User {:id s/Int :name s/Str :age s/Int})
  (m/decode User {:id "42" :name "Bob" :age "25"})
  ;; => {: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"})
   ;; => {: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)
sourceraw docstring

decoderclj

(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.

Examples:

(def decode-user (m/decoder User)) (decode-user {:id "42" :name "Bob"}) ;; => {: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:

  (def decode-user (m/decoder User))
  (decode-user {:id "42" :name "Bob"})  ;; => {:id 42 :name "Bob"}

  Args: schema, (optional) registry
  Returns: Function [value] -> decoded-value
sourceraw docstring

encodeclj

(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. Does NOT validate constraints.

Examples:

(s/def User {:id s/Int :name s/Str}) (m/encode User {:id 42 :name "Bob"}) ;; => {: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. Does NOT validate constraints.

  ## Examples:

  (s/def User {:id s/Int :name s/Str})
  (m/encode User {:id 42 :name "Bob"})
  ;; => {:id "42" :name "Bob"}

  Args: schema, value, (optional) registry
  Returns: Encoded value
sourceraw docstring

encoderclj

(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.

Examples:

(def encode-user (m/encoder User)) (encode-user {:id 42 :name "Bob"}) ;; => {: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:

  (def encode-user (m/encoder User))
  (encode-user {:id 42 :name "Bob"})  ;; => {:id "42" :name "Bob"}

  Args: schema, (optional) registry
  Returns: Function [value] -> encoded-value
sourceraw docstring

explainclj

(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.

Examples:

(s/def User {:id s/Int :name s/Str}) (m/explain User {:id 1 :name "John"}) ; => nil (m/explain User {:id "x" :name "John"}) ;; => {: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:

 (s/def User {:id s/Int :name s/Str})
 (m/explain User {:id 1 :name "John"})    ; => nil
 (m/explain User {:id "x" :name "John"})
 ;; => {:schema [...], :value {...}, :errors [...]}

 Args: schema, value, (optional) registry
 Returns: nil if valid, error map if invalid
sourceraw docstring

formclj

(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.

Examples:

(s/def User {:id s/Int :name s/Str}) (m/form User) ;; => [: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:

 (s/def User {:id s/Int :name s/Str})
 (m/form User)
 ;; => [:map {:closed true} [:id :int] [:name :string]]

 Args: schema, (optional) registry
 Returns: Malli form vector
sourceraw docstring

humanizeclj

(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 :-).

Examples:

(s/def User {:id s/Int :name s/Str}) (m/humanize User {:id 1 :name "John"}) ; => nil (m/humanize User {:id "x" :name "John"}) ;; => {:id ["should be an integer"]}

(s/def PositiveAge {:age ^{:positive true} s/Int}) (m/humanize PositiveAge {:age -5}) ;; => {:age ["must be positive"]}

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:

  (s/def User {:id s/Int :name s/Str})
  (m/humanize User {:id 1 :name "John"})    ; => nil
  (m/humanize User {:id "x" :name "John"})
  ;; => {:id ["should be an integer"]}

  (s/def PositiveAge {:age ^{:positive true} s/Int})
  (m/humanize PositiveAge {:age -5})
  ;; => {:age ["must be positive"]}

  Args: schema, value, (optional) registry
  Returns: nil if valid, map of entry path -> [message] values if invalid
sourceraw docstring

json-schemaclj

(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.

Examples:

(s/def User {:id s/Int :name s/Str :email ^{:re #"^.@.$"} s/Str}) (m/json-schema User) ;; => {"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:

  (s/def User {:id s/Int :name s/Str :email ^{:re #"^.*@.*$"} s/Str})
  (m/json-schema User)
  ;; => {"type" "object", "properties" {"id" {"type" "integer"}, ...}, "required" ["id" "name" "email"]}

  Args: schema, (optional) registry
  Returns: JSON Schema map
sourceraw docstring

typeclj

(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.

Examples:

(s/def User {:id s/Int :name s/Str}) (m/type User) ; => :map (m/type s/Int) ; => :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:

 (s/def User {:id s/Int :name s/Str})
 (m/type User)   ; => :map
 (m/type s/Int)  ; => :int

 Args: schema, (optional) registry
 Returns: Malli type keyword
sourceraw docstring

validateclj

(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.

Examples:

(s/def User {:id s/Int :name s/Str}) (m/validate User {:id 1 :name "John"}) ; => true (m/validate User {:id "x" :name "John"}) ; => 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:

 (s/def User {:id s/Int :name s/Str})
 (m/validate User {:id 1 :name "John"})   ; => true
 (m/validate User {:id "x" :name "John"}) ; => false

 Args: schema, value, (optional) registry
 Returns: Boolean
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close