Exdantic.Runtime.EnhancedSchema (exdantic v0.0.2)
View SourceEnhanced runtime schema with model validators and computed fields support.
This module extends DynamicSchema with support for:
- Model validators with both named and anonymous functions
- Computed fields with both named and anonymous functions
- Full validation pipeline execution
- JSON Schema generation with enhanced metadata
Summary
Functions
Adds a computed field to an existing enhanced schema.
Adds a model validator to an existing enhanced schema.
Creates an enhanced runtime schema with model validators and computed fields.
Returns information about the enhanced schema.
Processes computed field specifications and converts anonymous functions to named references.
Processes model validators and converts anonymous functions to named references.
Generates JSON Schema for an enhanced runtime schema.
Validates data against an enhanced runtime schema.
Types
@type computed_field_spec() :: {atom(), Exdantic.Types.type_definition(), validator_spec()}
@type t() :: %Exdantic.Runtime.EnhancedSchema{ base_schema: Exdantic.Runtime.DynamicSchema.t(), computed_fields: [computed_field_spec()], metadata: map(), model_validators: [validator_spec()], runtime_functions: %{required(atom()) => function()} }
Functions
@spec add_computed_field(t(), atom(), term(), validator_spec()) :: t()
Adds a computed field to an existing enhanced schema.
Parameters
enhanced_schema
- An EnhancedSchema structfield_name
- Name of the computed fieldfield_type
- Type specification for the fieldcomputation
- Computation function or {module, function} tuple
Returns
- Updated EnhancedSchema struct
@spec add_model_validator(t(), validator_spec()) :: t()
Adds a model validator to an existing enhanced schema.
Parameters
enhanced_schema
- An EnhancedSchema structvalidator
- Validator function or {module, function} tuple
Returns
- Updated EnhancedSchema struct
Creates an enhanced runtime schema with model validators and computed fields.
Parameters
field_definitions
- List of field definitionsopts
- Enhanced schema options
Options
:model_validators
- List of model validator functions or {module, function} tuples:computed_fields
- List of computed field specifications:title
,:description
,:strict
- Standard schema options:name
- Schema name for references
Examples
iex> fields = [{:name, :string, [required: true]}, {:age, :integer, [optional: true]}]
iex> validators = [fn data -> {:ok, %{data | name: String.trim(data.name)}} end]
iex> computed = [{:display_name, :string, fn data -> {:ok, String.upcase(data.name)} end}]
iex> schema = Exdantic.Runtime.EnhancedSchema.create(fields,
...> model_validators: validators,
...> computed_fields: computed
...> )
%Exdantic.Runtime.EnhancedSchema{...}
Returns information about the enhanced schema.
Parameters
enhanced_schema
- An EnhancedSchema struct
Returns
- Map with enhanced schema information
@spec process_computed_fields([computed_field_spec()], %{ required(atom()) => function() }) :: {[computed_field_spec()], %{required(atom()) => function()}}
Processes computed field specifications and converts anonymous functions to named references.
Parameters
computed_fields
- List of computed field specificationsinitial_functions
- Map of existing runtime functions to extend
Returns
- Tuple of
{processed_fields, updated_functions}
where updated_functions contains both initial and any new anonymous functions converted to named references
Examples
iex> fields = [%{name: :full_name, function: fn x -> x.first <> " " <> x.last end}]
iex> {processed, functions} = process_computed_fields(fields, %{})
{[%{name: :full_name, function: {:runtime, :generated_name}}], %{generated_name: #Function<...>}}
@spec process_model_validators([validator_spec()]) :: {[validator_spec()], %{required(atom()) => function()}}
Processes model validators and converts anonymous functions to named references.
Parameters
validators
- List of validator specifications (module/function tuples or functions)
Returns
- Tuple of
{processed_validators, runtime_functions}
where runtime_functions contains any anonymous functions converted to named references
Examples
iex> validators = [{MyModule, :my_validator}, fn x -> x.valid end]
iex> {processed, functions} = process_model_validators(validators)
{[{MyModule, :my_validator}, {:runtime, :generated_name}], %{generated_name: #Function<...>}}
Generates JSON Schema for an enhanced runtime schema.
Parameters
enhanced_schema
- An EnhancedSchema structopts
- JSON Schema generation options
Returns
- JSON Schema map including computed field metadata
Examples
iex> json_schema = Exdantic.Runtime.EnhancedSchema.to_json_schema(schema)
%{
"type" => "object",
"properties" => %{
"name" => %{"type" => "string"},
"display_name" => %{"type" => "string", "readOnly" => true}
}
}
@spec validate(map(), t(), keyword()) :: {:ok, map()} | {:error, [Exdantic.Error.t()]}
Validates data against an enhanced runtime schema.
Parameters
data
- The data to validate (map)enhanced_schema
- An EnhancedSchema structopts
- Validation options
Returns
{:ok, validated_data}
on success (includes computed fields){:error, errors}
on validation failure
Examples
iex> data = %{name: " John ", age: 30}
iex> Exdantic.Runtime.EnhancedSchema.validate(data, schema)
{:ok, %{name: "John", age: 30, display_name: "JOHN"}}