Exdantic.JsonSchema.EnhancedResolver (exdantic v0.0.2)

View Source

Enhanced JSON Schema resolution with full computed field and model validator metadata.

This module extends the basic resolver functionality to handle:

  • Enhanced runtime schemas with model validators and computed fields
  • Dynamic schema references with runtime resolution
  • Cross-schema dependency resolution for complex validation pipelines
  • LLM provider-specific optimizations with enhanced metadata

Phase 6 Enhancement: Complete Integration

  • Seamlessly integrates all Exdantic features (struct patterns, model validators, computed fields)
  • Provides unified JSON Schema generation for all schema types
  • Optimizes for various LLM providers with enhanced metadata
  • Maintains backward compatibility with existing resolvers

Summary

Functions

Creates a comprehensive validation and schema generation report.

Optimizes schemas specifically for DSPy and structured LLM output patterns.

Resolves JSON Schema with full enhanced schema support.

Validates that a schema is compatible with enhanced validation pipeline.

Types

enhanced_resolution_options()

@type enhanced_resolution_options() :: [
  include_model_validators: boolean(),
  include_computed_fields: boolean(),
  optimize_for_provider: :openai | :anthropic | :generic,
  resolve_runtime_refs: boolean(),
  max_depth: non_neg_integer(),
  flatten_for_llm: boolean()
]

Functions

comprehensive_analysis(schema_or_spec, sample_data \\ nil, opts \\ [])

@spec comprehensive_analysis(
  module()
  | Exdantic.Runtime.DynamicSchema.t()
  | Exdantic.Runtime.EnhancedSchema.t(),
  map() | nil,
  keyword()
) :: map()

Creates a comprehensive validation and schema generation report.

Useful for debugging, documentation, and understanding complex schema interactions.

Parameters

  • schema_or_spec - Schema to analyze
  • sample_data - Optional sample data for validation testing
  • opts - Analysis options

Returns

  • Comprehensive report with validation results, schema analysis, and metadata

Examples

iex> defmodule AnalysisTestSchema do
...>   use Exdantic
...>   schema do
...>     field :name, :string, required: true
...>   end
...> end
iex> report = Exdantic.JsonSchema.EnhancedResolver.comprehensive_analysis(AnalysisTestSchema)
iex> report.schema_type
:compiled_schema
iex> is_map(report.performance_metrics)
true

optimize_for_dspy(schema_or_spec, dspy_opts \\ [])

Optimizes schemas specifically for DSPy and structured LLM output patterns.

DSPy requires specific JSON Schema patterns for reliable structured output. This function ensures schemas work optimally with DSPy's validation patterns.

Parameters

  • schema_or_spec - Schema to optimize
  • dspy_opts - DSPy-specific options

DSPy Options

  • :signature_mode - Generate schema for DSPy signature patterns (default: false)
  • :strict_types - Enforce strict type constraints (default: true)
  • :remove_computed_fields - Remove computed fields for input validation (default: false)
  • :field_descriptions - Include field descriptions for better LLM understanding (default: true)

Returns

  • DSPy-optimized JSON Schema

Examples

iex> defmodule DSPyTestSchema do
...>   use Exdantic
...>   schema do
...>     field :input, :string, required: true
...>   end
...> end
iex> dspy_schema = Exdantic.JsonSchema.EnhancedResolver.optimize_for_dspy(DSPyTestSchema)
iex> dspy_schema["x-dspy-optimized"]
true

resolve_enhanced(schema_or_spec, opts \\ [])

Resolves JSON Schema with full enhanced schema support.

Handles all Exdantic schema types:

  • Compile-time schemas (with struct support, model validators, computed fields)
  • Runtime DynamicSchema instances
  • Runtime EnhancedSchema instances with full features
  • Mixed references between schema types

Parameters

  • schema_or_spec - Schema module, runtime schema, or type specification
  • opts - Enhanced resolution options

Options

  • :include_model_validators - Include model validator metadata (default: true)
  • :include_computed_fields - Include computed field metadata (default: true)
  • :optimize_for_provider - Optimize for specific LLM provider (default: :generic)
  • :resolve_runtime_refs - Resolve references to runtime schemas (default: true)
  • :max_depth - Maximum resolution depth (default: 10)
  • :flatten_for_llm - Flatten complex structures for LLM consumption (default: false)

Returns

  • Enhanced JSON Schema with full metadata

Examples

# Basic usage with a module
iex> defmodule TestSchema do
...>   use Exdantic
...>   schema do
...>     field :name, :string, required: true
...>   end
...> end
iex> schema = Exdantic.JsonSchema.EnhancedResolver.resolve_enhanced(TestSchema)
iex> schema["type"]
"object"
iex> schema["x-exdantic-enhanced"]
true

validate_schema_compatibility(schema_or_spec, opts \\ [])

@spec validate_schema_compatibility(
  module()
  | Exdantic.Runtime.DynamicSchema.t()
  | Exdantic.Runtime.EnhancedSchema.t(),
  keyword()
) :: :ok | {:error, [String.t()]}

Validates that a schema is compatible with enhanced validation pipeline.

Checks for common issues that might cause problems with the full validation pipeline including model validators and computed fields.

Parameters

  • schema_or_spec - Schema to validate
  • opts - Validation options

Returns

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

Examples

iex> defmodule CompatibilityTestSchema do
...>   use Exdantic
...>   schema do
...>     field :name, :string, required: true
...>   end
...> end
iex> Exdantic.JsonSchema.EnhancedResolver.validate_schema_compatibility(CompatibilityTestSchema)
:ok