View Source Elixact.Schema (elixact v0.1.2)
Schema DSL for defining data schemas with validation rules and metadata.
Summary
Functions
Adds an enumeration constraint, limiting values to a predefined set.
Defines configuration settings for the schema.
Sets the description for the schema configuration.
Sets a default value for the field and marks it as optional. The default value will be used if the field is omitted from input data.
Sets a description for the field.
Sets a single example value for the field.
Sets multiple example values for the field.
Defines a field in the schema with a name, type, and optional constraints.
Adds a format constraint to a string field.
Adds a greater than constraint to a numeric field.
Adds a greater than or equal to constraint to a numeric field.
Adds a less than constraint to a numeric field.
Adds a less than or equal to constraint to a numeric field.
Adds a maximum items constraint to an array field.
Adds a maximum length constraint to a string field.
Adds a minimum items constraint to an array field.
Adds a minimum length constraint to a string field.
Marks the field as optional. An optional field may be omitted from the input data during validation.
Marks the field as required (this is the default behavior). A required field must be present in the input data during validation.
Defines a new schema with optional description.
Sets whether the schema should enforce strict validation. When strict is true, unknown fields will cause validation to fail.
Sets the title for the schema configuration.
Functions
Adds an enumeration constraint, limiting values to a predefined set.
Parameters
values
- List of allowed values
Examples
field :status, :string do
choices(["pending", "active", "completed"])
end
field :priority, :integer do
choices([1, 2, 3])
end
Defines configuration settings for the schema.
Configuration options can include:
- title - Schema title
- config_description - Schema description
- strict - Whether to enforce strict validation
Examples
config do
title("User Schema")
config_description("Validates user registration data")
strict(true)
end
Sets the description for the schema configuration.
Parameters
text
- String description of the schema
Examples
config do
config_description("Validates user data for registration")
end
Sets a default value for the field and marks it as optional. The default value will be used if the field is omitted from input data.
Parameters
value
- The default value to use when the field is not provided
Examples
field :status, :string do
default("pending")
end
field :active, :boolean do
default(true)
end
Sets a description for the field.
Parameters
text
- String description of the field's purpose or usage
Examples
field :age, :integer do
description("User's age in years")
end
Sets a single example value for the field.
Parameters
value
- An example value that would be valid for this field
Examples
field :age, :integer do
example(25)
end
Sets multiple example values for the field.
Parameters
values
- List of example values that would be valid for this field
Examples
field :status, :string do
examples(["pending", "active", "completed"])
end
Defines a field in the schema with a name, type, and optional constraints.
Parameters
name
- Atom representing the field nametype
- The field's type, which can be:- A built-in type (
:string
,:integer
,:float
,:boolean
,:any
) - An array type (
{:array, type}
) - A map type (
{:map, {key_type, value_type}}
) - A union type (
{:union, [type1, type2, ...]}
) - A reference to another schema (atom)
- A built-in type (
opts
- Optional block containing field constraints and metadata
Examples
# Simple field
field :name, :string
# Field with constraints
field :age, :integer do
description("User's age in years")
gt(0)
lt(150)
end
# Array field
field :tags, {:array, :string} do
min_items(1)
max_items(10)
end
# Map field
field :metadata, {:map, {:string, :any}}
# Reference to another schema
field :address, Address
Adds a format constraint to a string field.
Parameters
value
- The format pattern (regular expression)
Examples
field :email, :string do
format(~r/^[^ @]+@[^ @]+.[^ @]+$/)
end
Adds a greater than constraint to a numeric field.
Parameters
value
- The minimum value (exclusive)
Examples
field :age, :integer do
gt(0)
end
field :score, :float do
gt(0.0)
end
Adds a greater than or equal to constraint to a numeric field.
Parameters
value
- The minimum value (inclusive)
Examples
field :age, :integer do
gteq(18)
end
Adds a less than constraint to a numeric field.
Parameters
value
- The maximum value (exclusive)
Examples
field :age, :integer do
lt(100)
end
field :temperature, :float do
lt(100.0)
end
Adds a less than or equal to constraint to a numeric field.
Parameters
value
- The maximum value (inclusive)
Examples
field :rating, :float do
lteq(5.0)
end
Adds a maximum items constraint to an array field.
Parameters
value
- The maximum number of items allowed (must be a non-negative integer)
Examples
field :tags, {:array, :string} do
max_items(10)
end
Adds a maximum length constraint to a string field.
Parameters
value
- The maximum length allowed (must be a non-negative integer)
Examples
field :username, :string do
max_length(20)
end
Adds a minimum items constraint to an array field.
Parameters
value
- The minimum number of items required (must be a non-negative integer)
Examples
field :tags, {:array, :string} do
min_items(1)
end
Adds a minimum length constraint to a string field.
Parameters
value
- The minimum length required (must be a non-negative integer)
Examples
field :username, :string do
min_length(3)
end
Marks the field as optional. An optional field may be omitted from the input data during validation.
Examples
field :middle_name, :string do
optional()
end
Marks the field as required (this is the default behavior). A required field must be present in the input data during validation.
Examples
field :email, :string do
required()
end
Defines a new schema with optional description.
Parameters
description
- Optional string describing the schemado
- Block containing field definitions
Examples
schema "User data validation schema" do
field :name, :string
field :age, :integer
end
Sets whether the schema should enforce strict validation. When strict is true, unknown fields will cause validation to fail.
Parameters
bool
- Boolean indicating if strict validation should be enabled
Examples
config do
strict(true)
end
Sets the title for the schema configuration.
Parameters
text
- String title for the schema
Examples
config do
title("User Schema")
end