Exdantic.Validator (exdantic v0.0.2)
View SourceValidates values against type definitions and schemas.
This module provides the core validation logic for Exdantic schemas, handling field validation, constraints, and error reporting.
Summary
Functions
Validates a value against a type definition.
Validates data against a schema module, checking for required fields, field-level validations, and strict mode constraints if enabled.
Types
@type validation_result() :: {:ok, term()} | {:error, Exdantic.Error.t() | [Exdantic.Error.t()]}
Functions
@spec validate(Exdantic.Types.type_definition() | module(), term()) :: validation_result()
@spec validate(Exdantic.Types.type_definition() | module(), term(), validation_path()) :: validation_result()
Validates a value against a type definition.
Parameters
type
- The type definition or schema module to validate againstvalue
- The value to validatepath
- Current validation path for error messages (defaults to[]
)
Returns
{:ok, validated_value}
on success{:error, errors}
on validation failures
Examples
iex> Exdantic.Validator.validate({:type, :string, []}, "hello")
{:ok, "hello"}
iex> Exdantic.Validator.validate({:type, :integer, []}, "not a number")
{:error, %Exdantic.Error{...}}
@spec validate_schema(module(), map(), validation_path()) :: validation_result()
Validates data against a schema module, checking for required fields, field-level validations, and strict mode constraints if enabled.
Parameters
schema
- Schema module to validate againstdata
- Data to validate (map)path
- Current validation path for error messages (defaults to[]
)
Returns
{:ok, validated_data}
on success{:error, errors}
on validation failures
Examples
iex> defmodule TestSchema do
...> use Exdantic
...> schema do
...> field :name, :string
...> end
...> end
iex> Exdantic.Validator.validate_schema(TestSchema, %{name: "John"})
{:ok, %{name: "John"}}