Exdantic.Runtime.DynamicSchema (exdantic v0.0.2)

View Source

Represents 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

t()

@type t() :: %Exdantic.Runtime.DynamicSchema{
  config: %{
    optional(:title) => String.t(),
    optional(:description) => String.t(),
    optional(:strict) => boolean()
  },
  fields: %{required(atom()) => Exdantic.FieldMeta.t()},
  metadata: map(),
  name: String.t()
}

Functions

add_field(schema, field_name, field_meta)

@spec add_field(t(), atom(), Exdantic.FieldMeta.t()) :: t()

Adds a new field to the schema.

Parameters

  • schema - The DynamicSchema instance
  • field_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{...}

field_names(dynamic_schema)

@spec field_names(t()) :: [atom()]

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]

get_field(dynamic_schema, field_name)

@spec get_field(t(), atom()) :: {:ok, Exdantic.FieldMeta.t()} | :error

Gets the field definition for a specific field name.

Parameters

  • schema - The DynamicSchema instance
  • field_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

new(name, fields, config, metadata \\ %{})

@spec new(String.t(), map(), map(), map()) :: t()

Creates a new DynamicSchema instance.

Parameters

  • name - Unique identifier for the schema
  • fields - Map of field name to FieldMeta
  • config - Schema configuration options
  • metadata - 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{...}

optional_fields(dynamic_schema)

@spec optional_fields(t()) :: [atom()]

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]

remove_field(schema, field_name)

@spec remove_field(t(), atom()) :: t()

Removes a field from the schema.

Parameters

  • schema - The DynamicSchema instance
  • field_name - The field name to remove (atom)

Returns

  • Updated DynamicSchema instance

Examples

iex> updated = Exdantic.Runtime.DynamicSchema.remove_field(schema, :bio)
%Exdantic.Runtime.DynamicSchema{...}

required_fields(dynamic_schema)

@spec required_fields(t()) :: [atom()]

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]

strict?(dynamic_schema)

@spec strict?(t()) :: boolean()

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

summary(schema)

@spec summary(t()) :: map()

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
}

update_config(schema, new_config)

@spec update_config(t(), map()) :: t()

Updates the schema configuration.

Parameters

  • schema - The DynamicSchema instance
  • new_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, ...}}