# `Sinter.JsonSchema`
[🔗](https://github.com/nshkrdotcom/sinter/blob/v0.3.1/lib/sinter/json_schema.ex#L1)

Unified JSON Schema generation for Sinter.

This module provides the single JSON Schema generation engine that handles
all JSON Schema creation in Sinter. It converts Sinter schemas into standard
JSON Schema format with optional provider-specific optimizations.

## Features

- Standard JSON Schema generation
- Provider-specific optimizations (OpenAI, Anthropic, etc.)
- Reference resolution and flattening
- Constraint mapping
- Metadata preservation

## Usage

    schema = Sinter.Schema.define([
      {:name, :string, [required: true, min_length: 2]},
      {:age, :integer, [optional: true, gt: 0]}
    ])

    # Basic JSON Schema generation
    json_schema = Sinter.JsonSchema.generate(schema)

    # Provider-specific optimization
    openai_schema = Sinter.JsonSchema.generate(schema, optimize_for_provider: :openai)

    # For specific providers
    anthropic_schema = Sinter.JsonSchema.for_provider(schema, :anthropic)

# `draft`

```elixir
@type draft() :: :draft2020_12 | :draft7
```

# `generation_opts`

```elixir
@type generation_opts() :: [
  optimize_for_provider: :openai | :anthropic | :generic,
  draft: draft(),
  flatten: boolean(),
  include_descriptions: boolean(),
  strict: boolean()
]
```

# `for_provider`

```elixir
@spec for_provider(Sinter.Schema.t(), :openai | :anthropic | :generic, keyword()) ::
  map()
```

Generates a JSON Schema optimized for a specific LLM provider.

This is a convenience function that applies provider-specific optimizations
and returns a schema ready for use with that provider's API.

## Examples

    iex> openai_schema = Sinter.JsonSchema.for_provider(schema, :openai)
    iex> anthropic_schema = Sinter.JsonSchema.for_provider(schema, :anthropic)

# `generate`

```elixir
@spec generate(Sinter.Schema.t(), generation_opts()) :: map()
```

Generates a JSON Schema from a Sinter schema.

This is the core JSON Schema generation function that converts Sinter schemas
into standard JSON Schema format.

## Parameters

  * `schema` - A Sinter schema created by `Sinter.Schema.define/2`
  * `opts` - Generation options

## Options

  * `:optimize_for_provider` - Apply provider-specific optimizations
    - `:openai` - Optimize for OpenAI function calling
    - `:anthropic` - Optimize for Anthropic tool use
    - `:generic` - Standard JSON Schema (default)
  * `:flatten` - Resolve all references inline (default: false)
  * `:include_descriptions` - Include field descriptions (default: true)
  * `:strict` - Override schema's strict setting for additionalProperties

## Returns

  * JSON Schema map

## Examples

    iex> schema = Sinter.Schema.define([
    ...>   {:name, :string, [required: true, min_length: 2]},
    ...>   {:age, :integer, [optional: true, gt: 0]}
    ...> ], title: "User Schema")
    iex> Sinter.JsonSchema.generate(schema)
    %{
      "type" => "object",
      "title" => "User Schema",
      "properties" => %{
        "name" => %{"type" => "string", "minLength" => 2},
        "age" => %{"type" => "integer", "exclusiveMinimum" => 0}
      },
      "required" => ["name"],
      "additionalProperties" => false
    }

    # Provider-specific optimization
    iex> Sinter.JsonSchema.generate(schema, optimize_for_provider: :openai)
    %{
      "type" => "object",
      "properties" => %{...},
      "additionalProperties" => false,
      "required" => ["name"]
    }

# `validate_schema`

```elixir
@spec validate_schema(
  map(),
  keyword()
) :: :ok | {:error, [String.t()]}
```

Validates a JSON Schema for correctness and compatibility.

Uses `JSV` to validate against the JSON Schema meta-schema.

## Parameters

  * `json_schema` - The JSON Schema to validate
  * `opts` - Validation options

## Options

  * `:draft` - Override default meta-schema (`:draft2020_12` or `:draft7`)

## Returns

  * `:ok` if schema is valid
  * `{:error, issues}` if problems are found

---

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