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
Members list
Value members
Concrete methods
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
-
Noneif the value conforms to this schema, orSome(SchemaError)describing the first validation failure encountered - Source
- DynamicSchema.scala
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
-
trueif the value passes all validation checks,falseotherwise - 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
Returns the documentation associated with this schema.
Returns a copy of this schema with updated documentation.
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
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,
Noneotherwise - Source
- DynamicSchema.scala
Returns the default value for this schema, if one is defined.
Returns a copy of this schema with an additional modifier.
Returns the modifiers attached to this schema.
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.Recordentries - Variant types need
Binding.Variantentries - Primitive types need
Binding.Primitiveentries (provided byBindingResolver.defaults) - Sequence types need
Binding.Seqentries (provided byBindingResolver.defaults) - Map types need
Binding.Mapentries (provided byBindingResolver.defaults) - Wrapper types need
Binding.Wrapperentries
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
Returns the type id of the schema's root type.
Inherited methods
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
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