# `Exdantic.TypeAdapter`
[🔗](https://github.com/nshkrdotcom/exdantic/blob/v0.1.0/lib/exdantic/type_adapter.ex#L1)

Runtime 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)`

# `dump_options`

```elixir
@type dump_options() :: [exclude_none: boolean(), exclude_defaults: boolean()]
```

# `type_spec`

```elixir
@type type_spec() :: Exdantic.Types.type_definition() | atom() | module() | term()
```

# `validation_options`

```elixir
@type validation_options() :: [
  coerce: boolean(),
  strict: boolean(),
  path: [atom() | String.t()]
]
```

# `create`

```elixir
@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 for
  * `opts` - 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"]}

# `dump`

```elixir
@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 to
  * `value` - The value to serialize
  * `opts` - 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}}

# `json_schema`

```elixir
@spec json_schema(
  type_spec(),
  keyword()
) :: map()
```

Generates JSON Schema for a type specification.

## Parameters
  * `type_spec` - The type specification to generate schema for
  * `opts` - 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"}]}

# `validate`

```elixir
@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 against
  * `value` - The value to validate
  * `opts` - 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{...}]}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
