Random schema and data generation for testing with Schemallic.
This module provides utilities for generating random schemas and test data, useful for property-based testing, fuzzing, and development workflows.
Generated keys, values, and structures are highly random - expect anything from plain keywords to optional keys, regex patterns, enums, uuids, instants, nested maps, and more. Plain strings are sampled alphanumeric text, not human words. Unseeded examples show representative shapes; values vary per run.
gen-data works with both Schemallic-defined schemas (via s/def) and plain Clojure maps using Schemallic types directly. gen-schema and gen-edn take no schema input.
(require '[schemallic.core :as s] '[schemallic.gen :as g])
;; Generate random schemas (g/gen-schema) ; 5 keys, depth 3 (default) (g/gen-schema 3 2) ; 3 keys, depth 2
;; Generate random EDN data (g/gen-edn) ; 5 keys, depth 3 (default) (g/gen-edn 4 1) ; 4 keys, depth 1
;; Generate data from existing schemas - works with s/def schemas or direct maps (s/def User {:id s/Int :name s/Str}) (g/gen-data User 1) ; seeded: {:id -8522515, :name "n2IQ6opic1Pj8"} (g/gen-data {:id s/Int :name s/Str} 1) ; direct map, same shape
Generation:
Random schema and data generation for testing with Schemallic.
This module provides utilities for generating random schemas and test data,
useful for property-based testing, fuzzing, and development workflows.
Generated keys, values, and structures are highly random - expect anything
from plain keywords to optional keys, regex patterns, enums, uuids, instants,
nested maps, and more. Plain strings are sampled alphanumeric text, not
human words. Unseeded examples show representative shapes; values vary per run.
gen-data works with both Schemallic-defined schemas (via s/def) and plain
Clojure maps using Schemallic types directly. gen-schema and gen-edn take
no schema input.
## Quick Start
(require '[schemallic.core :as s]
'[schemallic.gen :as g])
;; Generate random schemas
(g/gen-schema) ; 5 keys, depth 3 (default)
(g/gen-schema 3 2) ; 3 keys, depth 2
;; Generate random EDN data
(g/gen-edn) ; 5 keys, depth 3 (default)
(g/gen-edn 4 1) ; 4 keys, depth 1
;; Generate data from existing schemas - works with s/def schemas or direct maps
(s/def User {:id s/Int :name s/Str})
(g/gen-data User 1) ; seeded: {:id -8522515, :name "n2IQ6opic1Pj8"}
(g/gen-data {:id s/Int :name s/Str} 1) ; direct map, same shape
## Functions
Generation:
- gen-schema ; Generate random schema structures
- gen-edn ; Generate arbitrary EDN data
- gen-data ; Generate data matching a specific schema(gen-data)(gen-data schema)(gen-data schema x)(gen-data schema x rand-seed)(gen-data schema x rand-seed reg)Inputs: ([] [schema :- SObject] [schema :- SObject x :- (s/cond-pre (s/maybe #:s{Keyword s/Any}) (s/maybe s/Int))] [schema :- SObject x :- (s/cond-pre (s/maybe #:s{Keyword s/Any}) (s/maybe s/Int)) rand-seed :- (s/maybe s/Int)] [schema :- SObject x :- (s/cond-pre (s/maybe #:s{Keyword s/Any}) (s/maybe s/Int)) rand-seed :- (s/maybe s/Int) reg :- (s/maybe s/Any)]) Returns: s/Any
Generate a single random value that conforms to a Schemallic schema.
This is a thin wrapper around random-data, provided for consistency
with other generators such as gen-schema and gen-edn.
With no args, generates from any?; Malli may have no generator for the
resulting form and throw. Pass a schema for reliable generation.
;; Generate data for a predefined schema (g/gen-data User) ;; e.g. => {:id -8522515, :name "n2IQ6opic1Pj8"}
;; Generate data with fixed values (g/gen-data User {:id 99} 1) ;; => {:id 99, :name "n2IQ6opic1Pj8"}
;; Generate data deterministically (same value every run) (g/gen-data User 1) ;; => {:id -8522515, :name "n2IQ6opic1Pj8"}
Args:
Returns: Random data conforming to the schema, with the seed data included.
Inputs: ([] [schema :- SObject] [schema :- SObject x :- (s/cond-pre (s/maybe #:s{Keyword s/Any}) (s/maybe s/Int))] [schema :- SObject x :- (s/cond-pre (s/maybe #:s{Keyword s/Any}) (s/maybe s/Int)) rand-seed :- (s/maybe s/Int)] [schema :- SObject x :- (s/cond-pre (s/maybe #:s{Keyword s/Any}) (s/maybe s/Int)) rand-seed :- (s/maybe s/Int) reg :- (s/maybe s/Any)])
Returns: s/Any
Generate a single random value that conforms to a Schemallic schema.
This is a thin wrapper around `random-data`, provided for consistency
with other generators such as `gen-schema` and `gen-edn`.
With no args, generates from `any?`; Malli may have no generator for the
resulting form and throw. Pass a schema for reliable generation.
## Examples:
;; Generate data for a predefined schema
(g/gen-data User)
;; e.g. => {:id -8522515, :name "n2IQ6opic1Pj8"}
;; Generate data with fixed values
(g/gen-data User {:id 99} 1)
;; => {:id 99, :name "n2IQ6opic1Pj8"}
;; Generate data deterministically (same value every run)
(g/gen-data User 1)
;; => {:id -8522515, :name "n2IQ6opic1Pj8"}
Args:
- schema: The base Schemallic schema (or direct map) to generate data for.
- seed: An optional map of seed data to include in the generated data,
or a numeric seed that makes generation deterministic.
- rand-seed: An optional numeric seed for deterministic generation.
- registry: An optional registry for resolving custom types.
Returns: Random data conforming to the schema, with the seed data included.(gen-edn)(gen-edn num-keys)(gen-edn num-keys depth)Inputs: ([] [num-keys :- s/Int] [num-keys :- s/Int depth :- s/Int]) Returns: #:s{Keyword s/Any}
Generate a random EDN map with keyword keys and depth-safe nested values.
Can be called with 0, 1, or 2 arguments:
;; Default EDN generation (g/gen-edn) ;; e.g. => {:kx5a0c 57, :g4u4 "dwb08g6", :okrba9 {:befhv9x true}, ...}
;; Custom number of keys and depth (g/gen-edn 2 1) ;; e.g. => {:d2t3mn7 {:x94c #inst "2026-01-03T04:50:25.325-00:00"}, :rm0zg :ylpd8ls}
Args:
Returns: A random EDN map.
Inputs: ([] [num-keys :- s/Int] [num-keys :- s/Int depth :- s/Int])
Returns: #:s{Keyword s/Any}
Generate a random EDN map with keyword keys and depth-safe nested values.
Can be called with 0, 1, or 2 arguments:
- 0 args: defaults to 5 keys, depth 3
- 1 arg: number of keys, default depth 3
- 2 args: number of keys and depth
## Examples:
;; Default EDN generation
(g/gen-edn)
;; e.g. => {:kx5a0c 57, :g4u4 "dwb08g6", :okrba9 {:befhv9x true}, ...}
;; Custom number of keys and depth
(g/gen-edn 2 1)
;; e.g. => {:d2t3mn7 {:x94c #inst "2026-01-03T04:50:25.325-00:00"},
:rm0zg :ylpd8ls}
Args:
- num-keys: Number of keys in the EDN map (default: 5)
- depth: Maximum nesting depth (default: 3)
Returns: A random EDN map.(gen-schema)(gen-schema num-keys)(gen-schema num-keys depth)Inputs: ([] [num-keys :- s/Int] [num-keys :- s/Int depth :- s/Int]) Returns: s/Schema
Generates a random schema map with a specified number of keys and depth. Each key can be optionally wrapped with s/optional-key, and values are recursively generated using random-schema and random-value constructors.
Can be called with 0, 1, or 2 arguments:
;; Default schema generation (g/gen-schema) ;; e.g. => {:e0u0vz8 s/Int, :l3r6d5v s/Str, #schema.core.OptionalKey{:k :sdvup} s/Any, ...} ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ;; from (s/optional-key :sdvup)
;; Custom number of keys and depth (g/gen-schema 3 1) ;; e.g. => {:a6ss37y {:baa82lb s/Int}, :j5qc6eg s/Str, :rdedw s/Bool}
Args:
Returns: A random schema map.
Inputs: ([] [num-keys :- s/Int] [num-keys :- s/Int depth :- s/Int])
Returns: s/Schema
Generates a random schema map with a specified number of keys and depth.
Each key can be optionally wrapped with s/optional-key, and values are
recursively generated using random-schema and random-value constructors.
Can be called with 0, 1, or 2 arguments:
- 0 args: defaults to 5 keys, depth 3
- 1 arg: number of keys, default depth 3
- 2 args: number of keys and depth
## Examples:
;; Default schema generation
(g/gen-schema)
;; e.g. => {:e0u0vz8 s/Int, :l3r6d5v s/Str, #schema.core.OptionalKey{:k :sdvup} s/Any, ...}
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;; from (s/optional-key :sdvup)
;; Custom number of keys and depth
(g/gen-schema 3 1)
;; e.g. => {:a6ss37y {:baa82lb s/Int}, :j5qc6eg s/Str, :rdedw s/Bool}
Args:
- num-keys: Number of keys in the schema (default: 5)
- depth: Maximum nesting depth (default: 3)
Returns: A random schema map.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 |