Exdantic.Type behaviour (exdantic v0.0.2)
View SourceBehaviour and macros for defining custom types.
This module provides the behaviour and utility functions for creating custom types in Exdantic schemas with validation and coercion capabilities.
Summary
Functions
Provides functionality for defining custom types in Exdantic schemas.
Types
Callbacks
@callback coerce_rule() :: coerce_rule()
@callback custom_rules() :: [atom()]
@callback json_schema() :: map()
@callback type_definition() :: Exdantic.Types.type_definition()
Functions
Provides functionality for defining custom types in Exdantic schemas.
When you use Exdantic.Type
, your module gets:
- The
Exdantic.Type
behaviour - Import of
Exdantic.Types
functions - Type aliases for coercion functions
- A default implementation of validation with coercion support
Examples
defmodule MyApp.Types.Email do
use Exdantic.Type
def type_definition do
{:type, :string, [format: ~r/^[^@]+@[^@]+.[^@]+$/]}
end
def json_schema do
%{"type" => "string", "format" => "email"}
end
def validate(value) do
case type_definition() |> Exdantic.Validator.validate(value) do
{:ok, validated} -> {:ok, validated}
{:error, _} -> {:error, "invalid email format"}
end
end
end