Exdantic (exdantic v0.0.2)

View Source

Exdantic is a schema definition and validation library for Elixir.

It provides a DSL for defining schemas with rich metadata, validation rules, and JSON Schema generation capabilities.

For validating non-dictionary types at the root level (similar to Pydantic's RootModel), see Exdantic.RootSchema.

Struct Pattern Support

Exdantic now supports generating structs alongside validation schemas:

defmodule UserSchema do
  use Exdantic, define_struct: true

  schema "User account information" do
    field :name, :string do
      required()
      min_length(2)
    end

    field :age, :integer do
      optional()
      gt(0)
    end
  end
end

The schema can then be used for validation and returns struct instances:

# Returns {:ok, %UserSchema{name: "John", age: 30}}
UserSchema.validate(%{name: "John", age: 30})

# Serialize struct back to map
{:ok, map} = UserSchema.dump(user_struct)

Examples

defmodule UserSchema do
  use Exdantic

  schema "User registration data" do
    field :name, :string do
      required()
      min_length(2)
    end

    field :age, :integer do
      optional()
      gt(0)
      lt(150)
    end

    field :email, Types.Email do
      required()
    end

    config do
      title("User Schema")
      strict(true)
    end
  end
end

The schema can then be used for validation and JSON Schema generation:

# Validation (returns map by default)
{:ok, user} = UserSchema.validate(%{
  name: "John Doe",
  email: "john@example.com",
  age: 30
})

# JSON Schema generation
json_schema = UserSchema.json_schema()

Summary

Functions

Phase 6 Enhancement: Enhanced schema information with complete feature analysis.

Configures a module to be an Exdantic schema.

Functions

__before_compile__(env)

(macro)
@spec __before_compile__(Macro.Env.t()) :: Macro.t()

Phase 6 Enhancement: Enhanced schema information with complete feature analysis.

Examples

iex> UserSchema.__enhanced_schema_info__()
%{
  exdantic_version: "Phase 6",
  phase_6_enhanced: true,
  compatibility: %{...},
  performance_profile: %{...},
  llm_optimization: %{...}
}

__using__(opts)

(macro)
@spec __using__(keyword()) :: Macro.t()

Configures a module to be an Exdantic schema.

Options

  • :define_struct - Whether to generate a struct for validated data. When true, validation returns struct instances instead of maps. Defaults to false for backwards compatibility.

Examples

# Traditional map-based validation
defmodule UserMapSchema do
  use Exdantic

  schema do
    field :name, :string
  end
end

# Struct-based validation
defmodule UserStructSchema do
  use Exdantic, define_struct: true

  schema do
    field :name, :string
  end
end