Schema comparison and analysis operations.
Provides high-level operations for comparing and combining Schemallic schemas
after their automatic schema.core -> malli conversion. Accepts both
Schemallic-defined schemas (via s/def) and plain Clojure maps using Schemallic
types. s/def registers wrapped primitive forms, so raw maps and s/def schemas
can convert differently; compare like with like.
Note: Composition operations are for map schemas. extend, merge, and
intersect return schema1 unchanged for non-map inputs; patch replaces
non-map targets with schema2.
Compare like with like: raw maps here, s/def schemas elsewhere:
(require '[schemallic.core :as s]
'[schemallic.ops :as o])
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def UserCopy {:id s/Int :name s/Str :email s/Str})
(def UserMap {:id s/Int :name s/Str :email s/Str})
(o/equal User UserCopy)
(o/extend User {:age s/Int})
(o/merge UserMap {:id s/Str})
Result: true, [:map {:closed true} [:id [:int]] ... [:age [:int]]],
and [:map {:closed true} [:id :string] ...].
Comparison:
equal (strict structural equality comparison)equiv (flexible compatibility checking, allows extra fields)Composition:
extend (add new fields to a schema, ignoring conflicts)merge (combine schemas, with precedence for the second)intersect (find common fields between schemas)patch (recursively update a schema with another, accepts raw data)Introspection:
info (analyze and introspect schemas)Schema comparison and analysis operations.
Provides high-level operations for comparing and combining Schemallic schemas
after their automatic `schema.core` -> `malli` conversion. Accepts both
Schemallic-defined schemas (via s/def) and plain Clojure maps using Schemallic
types. s/def registers wrapped primitive forms, so raw maps and s/def schemas
can convert differently; compare like with like.
**Note:** Composition operations are for map schemas. `extend`, `merge`, and
`intersect` return `schema1` unchanged for non-map inputs; `patch` replaces
non-map targets with `schema2`.
## Quick Start
Compare like with like: raw maps here, s/def schemas elsewhere:
```clojure
(require '[schemallic.core :as s]
'[schemallic.ops :as o])
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def UserCopy {:id s/Int :name s/Str :email s/Str})
(def UserMap {:id s/Int :name s/Str :email s/Str})
(o/equal User UserCopy)
(o/extend User {:age s/Int})
(o/merge UserMap {:id s/Str})
```
**Result:** `true`, `[:map {:closed true} [:id [:int]] ... [:age [:int]]]`,
and `[:map {:closed true} [:id :string] ...]`.
## Functions
Comparison:
- `equal` (strict structural equality comparison)
- `equiv` (flexible compatibility checking, allows extra fields)
Composition:
- `extend` (add new fields to a schema, ignoring conflicts)
- `merge` (combine schemas, with precedence for the second)
- `intersect` (find common fields between schemas)
- `patch` (recursively update a schema with another, accepts raw data)
Introspection:
- `info` (analyze and introspect schemas)(equal schema1 schema2)Inputs: [schema1 :- SObject schema2 :- SObject] Returns: s/Bool
Strict structural equality between schemas.
Compares converted Malli forms for exact match - same fields, types, and structure. Order-independent for maps. Handles closed/open map metadata. Ensures schemas are normalized before comparison for consistent results. s/def registers wrapped primitive forms, so compare like with like.
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def UserCopy {:id s/Int :name s/Str :email s/Str})
(o/equal User UserCopy)
(o/equal User {:id s/Str :name s/Str :email s/Str})
Result: true, false.
Args: schema1, schema2
Returns: Boolean
Inputs: [schema1 :- SObject schema2 :- SObject]
Returns: s/Bool
Strict structural equality between schemas.
Compares converted Malli forms for exact match - same fields, types, and structure.
Order-independent for maps. Handles closed/open map metadata.
Ensures schemas are normalized before comparison for consistent results.
s/def registers wrapped primitive forms, so compare like with like.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def UserCopy {:id s/Int :name s/Str :email s/Str})
(o/equal User UserCopy)
(o/equal User {:id s/Str :name s/Str :email s/Str})
```
**Result:** `true`, `false`.
**Args:** `schema`1, `schema`2
**Returns:** Boolean(equiv schema1 schema2)Inputs: [schema1 :- SObject schema2 :- SObject] Returns: s/Bool
Schema compatibility checking.
Returns true if schema1 is compatible with schema2. Either schema may have extra fields, but shared fields must match. s/def registers wrapped primitive forms, so compare like with like.
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def UserWithAge {:id s/Int :name s/Str :email s/Str :age s/Int})
(s/def BadId {:id s/Str :name s/Str :email s/Str})
(o/equiv User UserWithAge)
(o/equiv UserWithAge User)
(o/equiv User BadId)
Result: true, true, false.
Args: schema1, schema2
Returns: Boolean
Inputs: [schema1 :- SObject schema2 :- SObject]
Returns: s/Bool
Schema compatibility checking.
Returns true if schema1 is compatible with schema2.
Either schema may have extra fields, but shared fields must match.
s/def registers wrapped primitive forms, so compare like with like.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :email s/Str})
(s/def UserWithAge {:id s/Int :name s/Str :email s/Str :age s/Int})
(s/def BadId {:id s/Str :name s/Str :email s/Str})
(o/equiv User UserWithAge)
(o/equiv UserWithAge User)
(o/equiv User BadId)
```
**Result:** `true`, `true`, `false`.
**Args:** `schema`1, `schema`2
**Returns:** Boolean(extend base-schema extension-schema)Inputs: [base-schema :- SObject extension-schema :- SObject] Returns: MObject
Extends a schema with additional fields from another schema.
Adds new keys only -- existing fields are never overwritten. For nested maps, extends recursively. For non-map schemas, returns the base schema unchanged.
(s/def User {:id s/Int :name s/Str})
(o/extend User {:age s/Int})
(o/extend User {:name s/Bool :age s/Int})
Result: [:map {:closed true} [:id [:int]] [:name [:string]] [:age [:int]]]
for both calls. The :name conflict in the second call is ignored.
Args: base-schema, extension-schema
Returns: New schema with added fields
Inputs: [base-schema :- SObject extension-schema :- SObject]
Returns: MObject
Extends a schema with additional fields from another schema.
Adds new keys only -- existing fields are never overwritten.
For nested maps, extends recursively. For non-map schemas,
returns the base schema unchanged.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(o/extend User {:age s/Int})
(o/extend User {:name s/Bool :age s/Int})
```
**Result:** `[:map {:closed true} [:id [:int]] [:name [:string]] [:age [:int]]]`
for both calls. The `:name` conflict in the second call is ignored.
**Args:** `base-schema`, `extension-schema`
**Returns:** New schema with added fields(info schema)Inputs: [schema :- SObject] Returns: SInfoMap
Schema introspection and analysis.
Returns a normalized description of the schema's structure and types.
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/info User)
Result: {:schema {:valid? true, :closed? true}, ...}.
Args: schema
Returns: Map with type info and nested structure
Inputs: [schema :- SObject]
Returns: SInfoMap
Schema introspection and analysis.
Returns a normalized description of the schema's structure and types.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/info User)
```
**Result:** `{:schema {:valid? true, :closed? true}, ...}`.
**Args:** `schema`
**Returns:** Map with type info and nested structure(intersect schema1 schema2)Inputs: [schema1 :- SObject schema2 :- SObject] Returns: MObject
Structural intersection of two schemas.
Keeps only fields shared between both schemas, with types from schema1. Returns schema1 unchanged for non-map inputs.
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/intersect User {:id s/Str :age s/Int})
Result: [:map {:closed true} [:id [:int]]].
Args: schema1, schema2
Returns: New schema with only shared fields
Inputs: [schema1 :- SObject schema2 :- SObject]
Returns: MObject
Structural intersection of two schemas.
Keeps only fields shared between both schemas, with types from schema1.
Returns schema1 unchanged for non-map inputs.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/intersect User {:id s/Str :age s/Int})
```
**Result:** `[:map {:closed true} [:id [:int]]]`.
**Args:** `schema`1, `schema`2
**Returns:** New schema with only shared fields(merge schema1 schema2)Inputs: [schema1 :- SObject schema2 :- SObject] Returns: MObject
Structural merge of two schemas.
Combines fields from both schemas, with schema2 taking precedence on conflicts. Returns schema1 unchanged for non-map inputs.
(s/def User {:id s/Int :name s/Str})
(o/merge User {:id s/Str :age s/Int})
Result: [:map {:closed true} [:id :string] [:name :string] [:age :int]].
Args: schema1, schema2
Returns: New schema with merged fields
Inputs: [schema1 :- SObject schema2 :- SObject]
Returns: MObject
Structural merge of two schemas.
Combines fields from both schemas, with schema2 taking precedence on conflicts.
Returns schema1 unchanged for non-map inputs.
## Examples
```clojure
(s/def User {:id s/Int :name s/Str})
(o/merge User {:id s/Str :age s/Int})
```
**Result:** `[:map {:closed true} [:id :string] [:name :string] [:age :int]]`.
**Args:** `schema`1, `schema`2
**Returns:** New schema with merged fields(patch schema1 schema2)Inputs: [schema1 :- SObject schema2 :- SObject] Returns: MObject
Recursively patches schema1 with the structure and keys from schema2.
Like merge, but works recursively into nested maps and :map-of values. Also accepts raw Clojure data (maps, vectors, sets, scalars) as schema2. Other schema forms are replaced by schema2, including non-map targets.
Patch with another schema:
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/patch User {:id s/Str :age s/Int})
Result: [:map {:closed true} [:id [:string]] [:name [:string]] [:email [:string]] [:age [:int]]].
Patch with raw data:
(o/patch User {:id 42 :role :admin})
Result: [:map {:closed true} [:id [:= 42]] [:name [:string]] [:email [:string]] [:role [:= :admin]]].
Args: schema1, schema2
Returns: New schema with schema1 patched by schema2
Inputs: [schema1 :- SObject schema2 :- SObject]
Returns: MObject
Recursively patches schema1 with the structure and keys from schema2.
Like merge, but works recursively into nested maps and :map-of values.
Also accepts raw Clojure data (maps, vectors, sets, scalars) as schema2.
Other schema forms are replaced by schema2, including non-map targets.
## Examples
Patch with another schema:
```clojure
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/patch User {:id s/Str :age s/Int})
```
**Result:** `[:map {:closed true} [:id [:string]] [:name [:string]] [:email [:string]] [:age [:int]]]`.
Patch with raw data:
```clojure
(o/patch User {:id 42 :role :admin})
```
**Result:** `[:map {:closed true} [:id [:= 42]] [:name [:string]] [:email [:string]] [:role [:= :admin]]]`.
**Args:** `schema`1, `schema`2
**Returns:** New schema with schema1 patched by schema2cljdoc 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 |