DynamicSchema

zio.blocks.schema.DynamicSchema
See theDynamicSchema companion object
final case class DynamicSchema(reflect: Reflect[NoBinding, _])

A type-erased schema representation that can validate DynamicValue instances at runtime.

DynamicSchema wraps an unbound Reflect structure (one with no runtime binding), enabling schema-based validation and manipulation without compile-time type information. This is useful for scenarios where schemas are loaded dynamically, such as from configuration files, databases, or network services.

Key capabilities:

  • '''Validation''': Check whether a DynamicValue conforms to this schema's structure and constraints using check or conforms
  • '''Schema Conversion''': Convert to a Schema[DynamicValue] that only accepts conforming values via toSchema
  • '''Metadata Access''': Access documentation, type names, modifiers, defaults, and examples
  • '''Structure Navigation''': Navigate the schema structure using DynamicOptic paths

Value parameters

reflect

The underlying unbound reflect structure representing the schema

Attributes

See also

Schema.toDynamicSchema for creating a DynamicSchema from a typed Schema

DynamicValue for the runtime value representation

Example
import zio.blocks.chunk.Chunk
case class Person(name: String, age: Int)
// Create a DynamicSchema from an existing Schema
val personSchema: Schema[Person] = Schema.derived[Person]
val dynamicSchema: DynamicSchema = personSchema.toDynamicSchema
// Validate a DynamicValue against the schema
val value = DynamicValue.Record(Chunk(
 "name" -> DynamicValue.Primitive(PrimitiveValue.String("Alice")),
 "age" -> DynamicValue.Primitive(PrimitiveValue.Int(30))
))
dynamicSchema.check(value) // None if valid, Some(SchemaError) if invalid
dynamicSchema.conforms(value) // true if valid
Companion
object
Source
DynamicSchema.scala
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def check(value: DynamicValue): Option[SchemaError]

Validates a DynamicValue against this schema.

Validates a DynamicValue against this schema.

Performs recursive structural validation, checking that:

  • Record fields match expected names and types (extra fields are rejected)
  • Variant case names are valid and case values match their schemas
  • Sequence elements conform to the element schema
  • Map keys and values conform to their respective schemas
  • Primitive values have matching types and pass any validation constraints

Value parameters

value

The dynamic value to validate

Attributes

Returns

None if the value conforms to this schema, or Some(SchemaError) describing the first validation failure encountered

Source
DynamicSchema.scala
def conforms(value: DynamicValue): Boolean

Tests whether a DynamicValue conforms to this schema.

Tests whether a DynamicValue conforms to this schema.

Value parameters

value

The dynamic value to test

Attributes

Returns

true if the value passes all validation checks, false otherwise

Source
DynamicSchema.scala

Returns a copy of this schema with the specified default value.

Returns a copy of this schema with the specified default value.

Attributes

Source
DynamicSchema.scala
def doc: Doc

Returns the documentation associated with this schema.

Returns the documentation associated with this schema.

Attributes

Source
DynamicSchema.scala
def doc(value: String): DynamicSchema

Returns a copy of this schema with updated documentation.

Returns a copy of this schema with updated documentation.

Attributes

Source
DynamicSchema.scala

Returns the example values for this schema, if any are defined.

Returns the example values for this schema, if any are defined.

Attributes

Source
DynamicSchema.scala

Returns a copy of this schema with the specified example values.

Returns a copy of this schema with the specified example values.

Attributes

Source
DynamicSchema.scala
def get(optic: DynamicOptic): Option[Reflect[NoBinding, _]]

Navigates to a nested schema element using a DynamicOptic path.

Navigates to a nested schema element using a DynamicOptic path.

Value parameters

optic

The path to navigate (e.g., DynamicOptic.root.field("name"))

Attributes

Returns

The nested reflect structure if the path is valid, None otherwise

Source
DynamicSchema.scala

Returns the default value for this schema, if one is defined.

Returns the default value for this schema, if one is defined.

Attributes

Source
DynamicSchema.scala

Returns a copy of this schema with an additional modifier.

Returns a copy of this schema with an additional modifier.

Attributes

Source
DynamicSchema.scala
def modifiers: Seq[Reflect]

Returns the modifiers attached to this schema.

Returns the modifiers attached to this schema.

Attributes

Source
DynamicSchema.scala
def rebind[A](resolver: BindingResolver): Schema[A]

Rebinds this unbound schema using bindings from a BindingResolver.

Rebinds this unbound schema using bindings from a BindingResolver.

This converts the structural schema information (types, fields, cases, validations) into a fully operational Schema[A] by attaching runtime bindings (constructors, deconstructors, matchers) from the resolver.

The resolver must provide bindings for all types referenced in this schema:

  • Record types need Binding.Record entries
  • Variant types need Binding.Variant entries
  • Primitive types need Binding.Primitive entries (provided by BindingResolver.defaults)
  • Sequence types need Binding.Seq entries (provided by BindingResolver.defaults)
  • Map types need Binding.Map entries (provided by BindingResolver.defaults)
  • Wrapper types need Binding.Wrapper entries

Value parameters

resolver

The BindingResolver providing bindings for all types in this schema

Attributes

Returns

A bound Schema that can construct and deconstruct values

Throws
RebindException

If any required binding is missing from the resolver

Example
import zio.blocks.schema.binding._
case class Person(name: String, age: Int)
// Usually loaded at runtime, for example decoded from JSON
val dynamicSchema: DynamicSchema = Schema.derived[Person].toDynamicSchema
val resolver = BindingResolver.empty.bind(Binding.of[Person]) ++ BindingResolver.defaults
val schema: Schema[Person] = dynamicSchema.rebind[Person](resolver)
Source
DynamicSchema.scala

Derives a JSON Schema from this schema.

Derives a JSON Schema from this schema.

Note: Currently returns the generic Schema[DynamicValue] JSON Schema, which represents the DynamicValue ADT structure rather than this specific schema's structure. A future enhancement could derive structure-specific JSON Schema directly from the unbound reflect.

Attributes

Source
DynamicSchema.scala

Creates a Schema[DynamicValue] that only accepts values conforming to this schema.

Creates a Schema[DynamicValue] that only accepts values conforming to this schema.

The returned schema wraps the base Schema[DynamicValue] with a validation layer that rejects any DynamicValue that doesn't conform to this schema's structure.

Attributes

Returns

A schema that validates incoming dynamic values against this schema's structure

Source
DynamicSchema.scala
def typeId: TypeId[_ <: AnyKind]

Returns the type id of the schema's root type.

Returns the type id of the schema's root type.

Attributes

Source
DynamicSchema.scala

Inherited methods

def productElementNames: Iterator[String]

An iterator over the names of all the elements of this product.

An iterator over the names of all the elements of this product.

Attributes

Inherited from:
Product
Source
Product.scala
def productIterator: Iterator[Any]

An iterator over all the elements of this product.

An iterator over all the elements of this product.

Attributes

Returns

in the default implementation, an Iterator[Any]

Inherited from:
Product
Source
Product.scala