Types Reference

View Source

Peri provides a comprehensive set of built-in types for schema validation.

Basic Types

TypeDescriptionExample
:anyAccepts any value:any
:atomValidates atoms:atom
:stringValidates binary strings:string
:integerValidates integers:integer
:floatValidates floats:float
:booleanValidates booleans:boolean
:mapValidates maps (no content validation):map
:pidValidates process identifiers:pid

Time Types

TypeDescriptionExample
:dateValidates %Date{}:date
:timeValidates %Time{}:time
:datetimeValidates %DateTime{}:datetime
:naive_datetimeValidates %NaiveDateTime{}:naive_datetime
:durationValidates %Duration{}:duration

Collection Types

TypeDescriptionExample
{:list, type}List of elements of specified type{:list, :string}
{:map, type}Map with values of specified type{:map, :integer}
{:map, key_type, value_type}Map with typed keys and values{:map, :atom, :string}
{:tuple, types}Tuple with elements of specified types{:tuple, [:float, :float]}

String Constraints

TypeDescriptionExample
{:string, {:regex, regex}}String matching regex pattern{:string, {:regex, ~r/^\w+$/}}
{:string, {:eq, value}}String equal to value{:string, {:eq, "exact"}}
{:string, {:min, length}}String with minimum length{:string, {:min, 3}}
{:string, {:max, length}}String with maximum length{:string, {:max, 50}}

Integer Constraints

TypeDescriptionExample
{:integer, {:eq, value}}Integer equal to value{:integer, {:eq, 42}}
{:integer, {:neq, value}}Integer not equal to value{:integer, {:neq, 0}}
{:integer, {:gt, value}}Integer greater than value{:integer, {:gt, 0}}
{:integer, {:gte, value}}Integer greater than or equal{:integer, {:gte, 18}}
{:integer, {:lt, value}}Integer less than value{:integer, {:lt, 100}}
{:integer, {:lte, value}}Integer less than or equal{:integer, {:lte, 99}}
{:integer, {:range, {min, max}}}Integer within range (inclusive){:integer, {:range, {18, 65}}}

Choice Types

TypeDescriptionExample
{:enum, choices}Value must be one of choices{:enum, [:admin, :user, :guest]}
{:literal, value}Value must exactly match{:literal, :active}
{:either, {type1, type2}}Value matches either type{:either, {:string, :integer}}
{:oneof, types}Value matches one of types{:oneof, [:string, :integer, :atom]}

Modifiers

TypeDescriptionExample
{:required, type}Field is required{:required, :string}
{type, {:default, value}}Default value if missing{:string, {:default, "unknown"}}
{type, {:default, fun}}Default from function{:integer, {:default, &System.system_time/0}}
{type, {:default, {m, f}}}Default from MFA{:string, {:default, {MyMod, :get_default}}}
{type, {:transform, fun}}Transform value{:string, {:transform, &String.upcase/1}}
{type, {:transform, {m, f}}}Transform with MFA{:string, {:transform, {MyMod, :clean}}}

Custom Validation

TypeDescriptionExample
{:custom, callback}Custom validation function{:custom, &validate_email/1}
{:custom, {mod, fun}}Custom validation MFA{:custom, {MyMod, :validate}}
{:custom, {mod, fun, args}}Custom validation with args{:custom, {MyMod, :validate, []}}

Examples

Simple User Schema

%{
  name: {:required, :string},
  age: {:integer, {:gte, 0}},
  email: {:required, {:string, {:regex, ~r/@/}}},
  role: {:enum, [:admin, :user]}
}

Complex Nested Schema

%{
  user: %{
    profile: {:required, %{
      name: {:required, :string},
      bio: {:string, {:max, 500}}
    }},
    preferences: {:map, :string, :boolean},
    tags: {:list, :string}
  }
}