Exdantic.Runtime.DynamicSchema (exdantic v0.0.2)
View SourceRepresents a schema created at runtime with field definitions and configuration.
This struct holds all the information needed to validate data against a dynamically created schema, including field metadata, configuration options, and runtime metadata.
Summary
Functions
Adds a new field to the schema.
Lists all field names in the schema.
Gets the field definition for a specific field name.
Creates a new DynamicSchema instance.
Gets the optional field names from the schema.
Removes a field from the schema.
Gets the required field names from the schema.
Checks if the schema is configured for strict validation.
Returns a summary of the schema structure.
Updates the schema configuration.
Types
Functions
@spec add_field(t(), atom(), Exdantic.FieldMeta.t()) :: t()
Adds a new field to the schema.
Parameters
schema
- The DynamicSchema instancefield_name
- The field name (atom)field_meta
- The FieldMeta definition
Returns
- Updated DynamicSchema instance
Examples
iex> field_meta = %Exdantic.FieldMeta{name: :bio, type: {:type, :string, []}, required: false}
iex> updated = Exdantic.Runtime.DynamicSchema.add_field(schema, :bio, field_meta)
%Exdantic.Runtime.DynamicSchema{...}
Lists all field names in the schema.
Parameters
schema
- The DynamicSchema instance
Returns
- List of field names (atoms)
Examples
iex> Exdantic.Runtime.DynamicSchema.field_names(schema)
[:name, :age, :email]
@spec get_field(t(), atom()) :: {:ok, Exdantic.FieldMeta.t()} | :error
Gets the field definition for a specific field name.
Parameters
schema
- The DynamicSchema instancefield_name
- The field name (atom)
Returns
{:ok, field_meta}
if field exists:error
if field not found
Examples
iex> Exdantic.Runtime.DynamicSchema.get_field(schema, :name)
{:ok, %Exdantic.FieldMeta{...}}
iex> Exdantic.Runtime.DynamicSchema.get_field(schema, :nonexistent)
:error
Creates a new DynamicSchema instance.
Parameters
name
- Unique identifier for the schemafields
- Map of field name to FieldMetaconfig
- Schema configuration optionsmetadata
- Optional runtime metadata
Examples
iex> fields = %{name: %Exdantic.FieldMeta{...}}
iex> config = %{title: "User Schema", strict: true}
iex> Exdantic.Runtime.DynamicSchema.new("UserSchema", fields, config)
%Exdantic.Runtime.DynamicSchema{...}
Gets the optional field names from the schema.
Parameters
schema
- The DynamicSchema instance
Returns
- List of optional field names (atoms)
Examples
iex> Exdantic.Runtime.DynamicSchema.optional_fields(schema)
[:age, :bio]
Removes a field from the schema.
Parameters
schema
- The DynamicSchema instancefield_name
- The field name to remove (atom)
Returns
- Updated DynamicSchema instance
Examples
iex> updated = Exdantic.Runtime.DynamicSchema.remove_field(schema, :bio)
%Exdantic.Runtime.DynamicSchema{...}
Gets the required field names from the schema.
Parameters
schema
- The DynamicSchema instance
Returns
- List of required field names (atoms)
Examples
iex> Exdantic.Runtime.DynamicSchema.required_fields(schema)
[:name, :email]
Checks if the schema is configured for strict validation.
Parameters
schema
- The DynamicSchema instance
Returns
true
if strict mode is enabled,false
otherwise
Examples
iex> Exdantic.Runtime.DynamicSchema.strict?(schema)
true
Returns a summary of the schema structure.
Parameters
schema
- The DynamicSchema instance
Returns
- Map with schema summary information
Examples
iex> Exdantic.Runtime.DynamicSchema.summary(schema)
%{
name: "UserSchema",
field_count: 3,
required_count: 2,
optional_count: 1,
strict: true
}
Updates the schema configuration.
Parameters
schema
- The DynamicSchema instancenew_config
- Configuration options to merge
Returns
- Updated DynamicSchema instance
Examples
iex> updated = Exdantic.Runtime.DynamicSchema.update_config(schema, %{strict: true})
%Exdantic.Runtime.DynamicSchema{config: %{strict: true, ...}}