Exdantic.TypeAdapter (exdantic v0.0.2)
View SourceRuntime type validation and serialization without a schema.
This module provides the equivalent of Pydantic's TypeAdapter functionality, allowing validation and serialization of values against type specifications without requiring a full schema definition.
Supports the DSPy pattern: TypeAdapter(type(value)).validate_python(value)
Summary
Functions
Creates a TypeAdapter instance for reuse with the same type specification.
Serializes a value according to a type specification.
Generates JSON Schema for a type specification.
Validates a value against a type specification.
Types
Functions
@spec create( type_spec(), keyword() ) :: Exdantic.TypeAdapter.Instance.t()
Creates a TypeAdapter instance for reuse with the same type specification.
Parameters
type_spec- The type specification to create an adapter foropts- Configuration options for the adapter
Returns
- TypeAdapter struct
Examples
iex> adapter = Exdantic.TypeAdapter.create({:array, :string})
%Exdantic.TypeAdapter.Instance{...}
iex> Exdantic.TypeAdapter.Instance.validate(adapter, ["a", "b", "c"])
{:ok, ["a", "b", "c"]}
@spec dump(type_spec(), term(), dump_options()) :: {:ok, term()} | {:error, String.t()}
Serializes a value according to a type specification.
Parameters
type_spec- The type specification to serialize according tovalue- The value to serializeopts- Serialization options
Options
:exclude_none- Exclude nil values (default: false):exclude_defaults- Exclude default values (default: false)
Returns
{:ok, serialized_value}on success{:error, reason}on serialization failure
Examples
iex> Exdantic.TypeAdapter.dump(:string, "hello")
{:ok, "hello"}
iex> Exdantic.TypeAdapter.dump({:map, {:string, :any}}, %{name: "John", age: 30})
{:ok, %{"name" => "John", "age" => 30}}
Generates JSON Schema for a type specification.
Parameters
type_spec- The type specification to generate schema foropts- JSON Schema generation options
Options
:title- Schema title:description- Schema description:resolve_refs- Resolve all references inline (default: false)
Returns
- JSON Schema map
Examples
iex> Exdantic.TypeAdapter.json_schema(:string)
%{"type" => "string"}
iex> Exdantic.TypeAdapter.json_schema({:array, :integer})
%{"type" => "array", "items" => %{"type" => "integer"}}
iex> Exdantic.TypeAdapter.json_schema({:union, [:string, :integer]})
%{"oneOf" => [%{"type" => "string"}, %{"type" => "integer"}]}
@spec validate(type_spec(), term(), validation_options()) :: {:ok, term()} | {:error, [Exdantic.Error.t()]}
Validates a value against a type specification.
Parameters
type_spec- The type specification to validate againstvalue- The value to validateopts- Validation options
Options
:coerce- Enable type coercion (default: false):strict- Enable strict validation (default: false):path- Validation path for error reporting (default: [])
Returns
{:ok, validated_value}on success{:error, errors}on validation failure
Examples
iex> Exdantic.TypeAdapter.validate(:string, "hello")
{:ok, "hello"}
iex> Exdantic.TypeAdapter.validate(:integer, "123", coerce: true)
{:ok, 123}
iex> Exdantic.TypeAdapter.validate({:array, :string}, ["a", "b", "c"])
{:ok, ["a", "b", "c"]}
iex> Exdantic.TypeAdapter.validate(:integer, "not a number")
{:error, [%Exdantic.Error{...}]}