View Source Skema.Schema (Skema v1.0.0)

Schema definition macros for Skema.

Provides defschema macro for defining structured schemas with type annotations, validation rules, and automatic struct generation.

Features

  • Type-safe struct generation with @type annotations
  • Automatic field validation and casting
  • Support for default values (static or function-based)
  • Required field enforcement via @enforce_keys
  • Nested schema support
  • Custom casting and validation functions

Basic Usage

defmodule User do
  use Skema.Schema

  defschema do
    field :name, :string, required: true
    field :email, :string, required: true
    field :age, :integer, default: 0
    field :created_at, :naive_datetime, default: &NaiveDateTime.utc_now/0
  end
end

Advanced Usage

defmodule BlogPost do
  use Skema.Schema

  defschema do
    field :title, :string, required: true, length: [min: 5, max: 100]
    field :content, :string, required: true
    field :status, :string, default: "draft", in: ~w(draft published archived)
    field :tags, {:array, :string}, default: []
    field :author, User, required: true
    field :published_at, :naive_datetime,
      default: fn -> if status == "published", do: NaiveDateTime.utc_now() end
  end
end

Nested Schemas

defmodule Company do
  use Skema.Schema

  defschema Address do
    field :street, :string, required: true
    field :city, :string, required: true
    field :country, :string, default: "US"
  end

  defschema do
    field :name, :string, required: true
    field :address, Address, required: true
    field :employees, {:array, User}, default: []
  end
end

Summary

Functions

Defines a typed struct with validation capabilities.

Defines a field in a typed struct.

Functions

Link to this macro

defschema(module \\ nil, list)

View Source (macro)

Defines a typed struct with validation capabilities.

Inside a defschema block, each field is defined through the field/3 macro.

Options per field

  • type - The field type (built-in Elixir types, custom types, or other schemas)
  • required - If true, the field is required and cannot be nil
  • default - Default value (static value or zero-arity function)
  • cast_func - Custom casting function
  • from - Source field name if different from target field name
  • as - Target field name if different from source field name
  • into - Transformation function for Skema.transform/2
  • Validation options: length, number, format, in, not_in, func, each

Examples

Basic Schema

defmodule Person do
  use Skema.Schema

  defschema do
    field :name, :string, required: true
    field :age, :integer, number: [min: 0, max: 150]
    field :email, :string, format: ~r/@/
  end
end

Schema in Submodule

defmodule MyModule do
  use Skema.Schema

  defschema User do
    field :username, :string, required: true
    field :role, :string, default: "user", in: ~w(user admin)
  end

  defschema Post do
    field :title, :string, required: true
    field :author, User, required: true
    field :tags, {:array, :string}, default: []
  end
end

Dynamic Defaults

defschema Document do
  field :title, :string, required: true
  field :created_at, :naive_datetime, default: &NaiveDateTime.utc_now/0
  field :uuid, :string, default: fn -> Ecto.UUID.generate() end
end
Link to this macro

field(name, type, opts \\ [])

View Source (macro)

Defines a field in a typed struct.

Examples

# Basic field
field :name, :string

# Required field
field :email, :string, required: true

# Field with default value
field :status, :string, default: "active"

# Field with validation
field :age, :integer, number: [min: 0, max: 150]

# Field with custom casting
field :tags, {:array, :string}, cast_func: &parse_comma_separated/1

Options

  • default - Sets the default value for the field (can be a value or function)
  • required - If set to true, enforces the field and makes its type non-nullable
  • cast_func - Custom function for casting the field value
  • from - Use value from a different source field name
  • as - Output the field with a different name
  • into - Transform function for post-processing
  • Validation options: length, number, format, in, not_in, func, each