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.
(require '[schemallic.core :as s] '[schemallic.ops :as o])
;; Compare like with like: raw maps here, s/def schemas elsewhere (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) ; => true (o/extend User {:age s/Int}) ; => [:map {:closed true} [:id [:int]] ... [:age [:int]]] (o/merge UserMap {:id s/Str}) ; => [:map {:closed true} [:id :string] ...]
Comparison:
Composition:
Introspection:
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
(require '[schemallic.core :as s]
'[schemallic.ops :as o])
;; Compare like with like: raw maps here, s/def schemas elsewhere
(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) ; => true
(o/extend User {:age s/Int}) ; => [:map {:closed true} [:id [:int]] ... [:age [:int]]]
(o/merge UserMap {:id s/Str}) ; => [: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) ; => true (o/equal User {:id s/Str :name s/Str :email s/Str}) ; => 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:
(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) ; => true
(o/equal User {:id s/Str :name s/Str :email s/Str}) ; => false
Args: schema1, schema2
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; 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) ; => true (o/equiv UserWithAge User) ; => true (o/equiv User BadId) ; => 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; shared fields must match.
s/def registers wrapped primitive forms, so compare like with like.
## Examples:
(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) ; => true
(o/equiv UserWithAge User) ; => true
(o/equiv User BadId) ; => false
Args: schema1, schema2
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}) ;; => [:map {:closed true} [:id [:int]] [:name [:string]] [:age [:int]]]
(o/extend User {:name s/Bool :age s/Int}) ; :name conflict ignored ;; => [:map {:closed true} [:id [:int]] [:name [:string]] [:age [:int]]]
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:
(s/def User {:id s/Int :name s/Str})
(o/extend User {:age s/Int})
;; => [:map {:closed true} [:id [:int]] [:name [:string]] [:age [:int]]]
(o/extend User {:name s/Bool :age s/Int}) ; :name conflict ignored
;; => [:map {:closed true} [:id [:int]] [:name [:string]] [:age [:int]]]
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) ;; => {: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:
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/info User)
;; => {: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}) ;; => [: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:
(s/def User {:id s/Int :name s/Str :email s/Str})
(o/intersect User {:id s/Str :age s/Int})
;; => [:map {:closed true} [:id [:int]]]
Args: schema1, schema2
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}) ;; => [: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:
(s/def User {:id s/Int :name s/Str})
(o/merge User {:id s/Str :age s/Int})
;; => [:map {:closed true} [:id :string] [:name :string] [:age :int]]
Args: schema1, schema2
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.
(s/def User {:id s/Int :name s/Str :email s/Str})
;; Patch with another schema (o/patch User {:id s/Str :age s/Int}) ;; => [:map {:closed true} [:id [:string]] [:name [:string]] [:email [:string]] [:age [:int]]]
;; Patch with raw data (o/patch User {:id 42 :role :admin}) ;; => [: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:
(s/def User {:id s/Int :name s/Str :email s/Str})
;; Patch with another schema
(o/patch User {:id s/Str :age s/Int})
;; => [:map {:closed true} [:id [:string]] [:name [:string]] [:email [:string]] [:age [:int]]]
;; Patch with raw data
(o/patch User {:id 42 :role :admin})
;; => [:map {:closed true} [:id [:= 42]] [:name [:string]] [:email [:string]] [:role [:= :admin]]]
Args: schema1, schema2
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 |