Exdantic.Validator (exdantic v0.0.2)

View Source

Validates 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

validation_path()

@type validation_path() :: [atom() | String.t() | integer()]

validation_result()

@type validation_result() ::
  {:ok, term()} | {:error, Exdantic.Error.t() | [Exdantic.Error.t()]}

Functions

validate(type, value)

validate(type, value, path)

Validates a value against a type definition.

Parameters

  • type - The type definition or schema module to validate against
  • value - The value to validate
  • path - 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{...}}

validate_schema(schema, data, path \\ [])

@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 against
  • data - 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"}}