Zoi (Zoi v0.5.5)

View Source

Zoi is a schema validation library for Elixir, designed to provide a simple and flexible way to define and validate data.

It allows you to create schemas for various data types, including strings, integers, booleans, and complex objects, with built-in support for validations like minimum and maximum values, regex patterns, and email formats.

user = Zoi.object(%{
  name: Zoi.string() |> Zoi.min(2) |> Zoi.max(100),
  age: Zoi.integer() |> Zoi.min(18) |> Zoi.max(120),
  email: Zoi.email()
})

Zoi.parse(user, %{
  name: "Alice",
  age: 30,
  email: "alice@email.com"
})
# {:ok, %{name: "Alice", age: 30, email: "alice@email.com"}}

Coercion

By default, Zoi will not attempt to infer input data to match the expected type. For example, if you define a schema that expects a string, passing an integer will result in an error.

iex> Zoi.string() |> Zoi.parse(123)
{:error, [%Zoi.Error{message: "invalid type: must be a string"}]}

If you need coercion, you can enable it by passing the :coerce option:

iex> Zoi.string(coerce: true) |> Zoi.parse(123)
{:ok, "123"}

Custom errors

You can customize parsing error messages the primitive types by passing the error option:

iex> schema = Zoi.integer(error: "must be a number")
iex> Zoi.parse(schema, "a")
{:error, [%Zoi.Error{message: "must be a number"}]}

Summary

Functions

Converts a list of errors into a human-readable string format. Each error is displayed on a new line, with its message and path.

Converts a list of errors into a tree structure, where each error is placed at its corresponding path.

Basic Types

Defines a schema that accepts any type of input.

Defines an atom type schema.

Defines a boolean type schema.

Defines a float type schema.

Defines a number type schema.

Defines the numeric type schema.

Defines a string type schema.

Defines a string boolean type schema.

Complex Types

Defines a array type schema.

Defines an enum type schema.

Extends two object type schemas into one. This function merges the fields of two object schemas. If there are overlapping fields, the fields from the second schema will override those from the first.

Defines a keyword list type schema.

Defines a map type schema with Zoi.any() type.

Defines a map type schema.

Defines a object type schema.

Defines a tuple type schema.

Encapsulated Types

Creates a default value for the schema.

Defines an intersection type schema.

Defines a schema that allows nil values.

Makes the schema optional for the Zoi.object/2 type.

Defines a union type schema.

Extensions

Adds a custom validation function to the schema.

Adds a transformation function to the schema.

Formats

Validates that the string is a valid email format.

Validates that the string is a valid IPv4 address.

Validates that the string is a valid IPv6 address.

Defines a URL format validation.

Validates that the string is a valid UUID format.

Parsing

Parse input data against a schema. Accepts optional coerce: true option to enable coercion.

Refinements

Validates that a string ends with a specific suffix.

Validates that the input is greater than a specific value.

Validates that the input is greater than or equal to a value.

Validates that the string has a specific length.

Validates that the input is less than a specific value.

Validates that the input is less than or equal to a value.

Validates that the input matches a given regex pattern.

Validates that a string starts with a specific prefix.

Structured Types

Defines a date type schema.

Defines a DateTime type schema.

Defines a decimal type schema.

Defines a NaiveDateTime type schema.

Defines a time type schema.

Transforms

Converts a string to lowercase.

Converts a schema to a struct of the given module. This is useful for transforming parsed data into a specific struct type.

Converts a string to uppercase.

Trims whitespace from the beginning and end of a string.

Types

input()

@type input() :: any()

options()

@type options() :: keyword()

refinement()

@type refinement() ::
  {module(), atom(), [any()]}
  | (input() -> :ok | {:error, binary()})
  | (input(), Zoi.Context.t() -> :ok | {:error, binary()})

result()

@type result() :: {:ok, any()} | {:error, [Zoi.Error.t() | binary()]}

transform()

@type transform() ::
  {module(), atom(), [any()]}
  | (input() -> {:ok, input()} | {:error, binary()} | input())
  | (input(), Zoi.Context.t() -> {:ok, input()} | {:error, binary()} | input())

Functions

prettify_errors(errors)

@spec prettify_errors([Zoi.Error.t() | binary()]) :: binary()

Converts a list of errors into a human-readable string format. Each error is displayed on a new line, with its message and path.

Example

iex> errors = [
...>   %Zoi.Error{path: ["name"], message: "is required"},
...>   %Zoi.Error{path: ["age"], message: "invalid type: must be an integer"},
...>   %Zoi.Error{path: ["address", "city"], message: "is required"}
...> ]
iex> Zoi.prettify_errors(errors)
"is required, at name\ninvalid type: must be an integer, at age\nis required, at address.city"

iex> errors = [%Zoi.Error{message: "invalid type: must be a string"}]
iex> Zoi.prettify_errors(errors)
"invalid type: must be a string"

treefy_errors(errors)

@spec treefy_errors([Zoi.Error.t()]) :: map()

Converts a list of errors into a tree structure, where each error is placed at its corresponding path.

This is useful for displaying validation errors in a structured way, such as in a form.

Example

iex> errors = [
...>   %Zoi.Error{path: ["name"], message: "is required"},
...>   %Zoi.Error{path: ["age"], message: "invalid type: must be an integer"},
...>   %Zoi.Error{path: ["address", "city"], message: "is required"}
...> ]
iex> Zoi.treefy_errors(errors)
%{
  "name" => ["is required"],
  "age" => ["invalid type: must be an integer"],
  "address" => %{
    "city" => ["is required"]
  }
}

If you use this function on types without path (like Zoi.string()), it will create a top-level :__errors__ key:

iex> errors = [%Zoi.Error{message: "invalid type: must be a string"}]
iex> Zoi.treefy_errors(errors)
%{__errors__: ["invalid type: must be a string"]}

Errors without a path are considered top-level errors and are grouped under :__errors__. This is how Zoi also handles errors when Zoi.object/2 is used with :strict option, where unrecognized keys are added to the :__errors__ key.

Basic Types

any(opts \\ [])

Defines a schema that accepts any type of input.

This is useful when you want to allow any data type without validation.

Example

iex> schema = Zoi.any()
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, 42)
{:ok, 42}
iex> Zoi.parse(schema, %{key: "value"})
{:ok, %{key: "value"}}

atom(opts \\ [])

Defines an atom type schema.

Examples

iex> schema = Zoi.atom()
iex> Zoi.parse(schema, :atom)
{:ok, :atom}
iex> Zoi.parse(schema, "not_an_atom")
{:error, [%Zoi.Error{message: "invalid type: must be an atom"}]}

boolean(opts \\ [])

Defines a boolean type schema.

Example

iex> schema = Zoi.boolean()
iex> Zoi.parse(schema, true)
{:ok, true}

For coercion, you can pass the :coerce option:

iex> Zoi.boolean(coerce: true) |> Zoi.parse("true")
{:ok, true}

float(opts \\ [])

Defines a float type schema.

Example

iex> schema = Zoi.float()
iex> Zoi.parse(schema, 3.14)
{:ok, 3.14}

Built-in validations for floats include:

Zoi.min(0.0)
Zoi.max(100.0)

For coercion, you can pass the :coerce option:

iex> Zoi.float(coerce: true) |> Zoi.parse("3.14")
{:ok, 3.14}

integer(opts \\ [])

Defines a number type schema.

Example

iex> shema = Zoi.integer()
iex> Zoi.parse(shema, 42)
{:ok, 42}

Built-in validations for integers include:

Zoi.min(0)
Zoi.max(100)

number(opts \\ [])

Defines the numeric type schema.

This type is a union of Zoi.integer() and Zoi.float(), allowing you to validate both integers and floats.

Example

iex> schema = Zoi.number()
iex> Zoi.parse(schema, 42)
{:ok, 42}
iex> Zoi.parse(schema, 3.14)
{:ok, 3.14}

string(opts \\ [])

Defines a string type schema.

Example

Zoi provides built-in validations for strings, such as:

Zoi.gt(100)
Zoi.gte(100)
Zoi.lt(2)
Zoi.lte(2)
Zoi.min(2) # alias for `Zoi.gte(2)`
Zoi.max(100) # alias for `Zoi.lte(100)`
Zoi.starts_with("hello")
Zoi.ends_with("world")
Zoi.length(5)
Zoi.regex(~r/^[a-zA-Z]+$/)

Additionally it can perform data transformation:

Zoi.string()
|> Zoi.trim()
|> Zoi.to_downcase()
|> Zoi.to_uppercase()

string_boolean(opts \\ [])

Defines a string boolean type schema.

This type parses "boolish" string values:

# thruthy values: true, "true", "1", "yes", "on", "y", "enabled"
# falsy values: false, "false", "0", "no", "off", "n", "disabled"

Example

iex> schema = Zoi.string_boolean()
iex> Zoi.parse(schema, "true")
{:ok, true}
iex> Zoi.parse(schema, "false")
{:ok, false}
iex> Zoi.parse(schema, "yes")
{:ok, true}
iex> Zoi.parse(schema, "no")
{:ok, false}

You can also specify custom truthy and falsy values using the :truthy and :falsy options:

iex> schema = Zoi.string_boolean(truthy: ["yes", "y"], falsy: ["no", "n"])
iex> Zoi.parse(schema, "yes")
{:ok, true}
iex> Zoi.parse(schema, "no")
{:ok, false}

By default the string boolean type is case insensitive and the input is converted to lowercase during the comparison. You can change this behavior using the :case option:

iex> schema = Zoi.string_boolean(case: "sensitive")
iex> Zoi.parse(schema, "True")
{:error, [%Zoi.Error{message: "invalid type: must be a string boolean"}]}
iex> Zoi.parse(schema, "true")
{:ok, true}

Complex Types

array(elements, opts \\ [])

Defines a array type schema.

Use Zoi.array(elements) to define an array of a specific type:

iex> schema = Zoi.array(Zoi.string())
iex> Zoi.parse(schema, ["hello", "world"])
{:ok, ["hello", "world"]}
iex> Zoi.parse(schema, ["hello", 123])
{:error, [%Zoi.Error{message: "invalid type: must be a string", path: [1]}]}

Built-in validations for integers include:

Zoi.gt(5)
Zoi.gte(5)
Zoi.lt(2)
Zoi.lte(2)
Zoi.min(2) # alias for `Zoi.gte/1`
Zoi.max(5) # alias for `Zoi.lte/1`
Zoi.length(5)

enum(values, opts \\ [])

Defines an enum type schema.

Use Zoi.enum(values) to define a schema that accepts only specific values:

iex> schema = Zoi.enum([:red, :green, :blue])
iex> Zoi.parse(schema, :red)
{:ok, :red}
iex> Zoi.parse(schema, :yellow)
{:error, [%Zoi.Error{message: "invalid option, must be one of: red, green, blue"}]}

You can also specify enum as strings:

iex> schema = Zoi.enum(["red", "green", "blue"])
iex> Zoi.parse(schema, "red")
{:ok, "red"}
iex> Zoi.parse(schema, "yellow")
{:error, [%Zoi.Error{message: "invalid option, must be one of: red, green, blue"}]}

or with key-value pairs:

iex> schema = Zoi.enum([red: "Red", green: "Green", blue: "Blue"])
iex> Zoi.parse(schema, "Red")
{:ok, :red}
iex> Zoi.parse(schema, "Yellow")
{:error, [%Zoi.Error{message: "invalid option, must be one of: Red, Green, Blue"}]}

Integer values can also be used:

iex> schema = Zoi.enum([1, 2, 3])
iex> Zoi.parse(schema, 1)
{:ok, 1}
iex> Zoi.parse(schema, 4)
{:error, [%Zoi.Error{message: "invalid option, must be one of: 1, 2, 3"}]}

And Integers with key-value pairs also is allowed:

iex> schema = Zoi.enum([one: 1, two: 2, three: 3])
iex> Zoi.parse(schema, 1)
{:ok, :one}
iex> Zoi.parse(schema, 4)
{:error, [%Zoi.Error{message: "invalid value for enum"}]}
{:error, [%Zoi.Error{message: "invalid option, must be one of: 1, 2, 3"}]}

extend(schema1, schema2, opts \\ [])

Extends two object type schemas into one. This function merges the fields of two object schemas. If there are overlapping fields, the fields from the second schema will override those from the first.

Example

iex> user = Zoi.object(%{name: Zoi.string()})
iex> role = Zoi.object(%{role: Zoi.enum([:admin,:user])})
iex> user_with_role = Zoi.extend(user, role)
iex> Zoi.parse(user_with_role, %{name: "Alice", role: :admin})
{:ok, %{name: "Alice", role: :admin}}

keyword(fields, opts \\ [])

Defines a keyword list type schema.

iex> schema = Zoi.keyword(name: Zoi.string(), age: Zoi.integer())
iex> Zoi.parse(schema, [name: "Alice", age: 30])
{:ok, [name: "Alice", age: 30]}
iex> Zoi.parse(schema, %{name: "Alice", age: 30})
{:error, [%Zoi.Error{message: "invalid type: must be a keyword list"}]}

By default, unrecognized keys will be removed from the parsed data. If you want to not allow unrecognized keys, use the :strict option:

iex> schema = Zoi.keyword([name: Zoi.string()], strict: true)
iex> Zoi.parse(schema, [name: "Alice", age: 30])
{:error, [%Zoi.Error{message: "unrecognized key: 'age'"}]}

map(opts \\ [])

Defines a map type schema with Zoi.any() type.

This type is the same as creating the following map:

Zoi.map(Zoi.any(), Zoi.any())

map(key, type, opts \\ [])

Defines a map type schema.

Example

iex> schema = Zoi.map(Zoi.string(), Zoi.integer())
iex> Zoi.parse(schema, %{"a" => 1, "b" => 2})
{:ok, %{"a" => 1, "b" => 2}}
iex> Zoi.parse(schema, %{"a" => "1", "b" => 2})
{:error, [%Zoi.Error{message: "invalid type: must be an integer", path: ["a"]}]}

object(fields, opts \\ [])

Defines a object type schema.

Use Zoi.object(fields) to define complex objects with nested schemas:

iex> user_schema = Zoi.object(%{
...> name: Zoi.string() |> Zoi.min(2) |> Zoi.max(100),
...> age: Zoi.integer() |> Zoi.min(18) |> Zoi.max(120),
...> email: Zoi.email()
...> })
iex> Zoi.parse(user_schema, %{name: "Alice", age: 30, email: "alice@email.com"})
{:ok, %{name: "Alice", age: 30, email: "alice@email.com"}}

By default all fields are required, but you can make them optional by using Zoi.optional/1:

iex> user_schema = Zoi.object(%{
...> name: Zoi.string() |> Zoi.optional(),
...> age: Zoi.integer() |> Zoi.optional(),
...> email: Zoi.email() |> Zoi.optional()
...> })
iex> Zoi.parse(user_schema, %{name: "Alice"})
{:ok, %{name: "Alice"}}

By default, unrecognized keys will be removed from the parsed data. If you want to not allow unrecognized keys, use the :strict option:

iex> schema = Zoi.object(%{name: Zoi.string()}, strict: true)
iex> Zoi.parse(schema, %{name: "Alice", age: 30})
{:error, [%Zoi.Error{message: "unrecognized key: 'age'"}]}

String keys and Atom keys

Objects can be declared using string keys too, this would set the expectation that the param data is also using string keys:

iex> schema = Zoi.object(%{"name" => Zoi.string()})
iex> Zoi.parse(schema, %{"name" => "Alice"})
{:ok, %{"name" => "Alice"}}
iex> Zoi.parse(schema, %{name: "Alice"})
{:error, [%Zoi.Error{message: "is required", path: ["name"]}]}

It's possible coerce the keys to atoms using the :coerce option:

iex> schema = Zoi.object(%{name: Zoi.string()}, coerce: true)
iex> Zoi.parse(schema, %{"name" => "Alice"})
{:ok, %{name: "Alice"}}

Which will automatically convert string keys to atom keys.

Nullable vs Optional fields

The Zoi.optional/1 function makes a field optional, meaning it can be omitted from the input data. If the field is not present, it will not be included in the parsed result. The Zoi.nullable/1 function allows a field to be nil, meaning it can be explicitly set to nil in the input data. If the field is set to nil, it will be included in the parsed result as nil.

iex> schema = Zoi.object(%{name: Zoi.string() |> Zoi.optional(), age: Zoi.integer() |> Zoi.nullable()})
iex> Zoi.parse(schema, %{name: "Alice", age: nil})
{:ok, %{name: "Alice", age: nil}}
iex> Zoi.parse(schema, %{name: "Alice"})
{:error, [%Zoi.Error{message: "is required", path: [:age]}]}

Optional vs Default fields

There are two options to define the behaviour of a field being optional and with a default value:

  1. If the field is not present in the input data OR nil, it will be included in the parsed result with the default value.
  2. If the field not present in the input data, it will not be included on the parsed result. If the value is nil, it will be included in the parsed result with the default value.

The order you encapsulate the type matters, to implement the first option, the encapsulation should be Zoi.default(Zoi.optional(type, default_value)):

iex> schema = Zoi.object(%{name: Zoi.default(Zoi.optional(Zoi.string()), "default value")})
iex> Zoi.parse(schema, %{})
{:ok, %{name: "default value"}}
iex> Zoi.parse(schema, %{name: nil})
{:ok, %{name: "default value"}}

The second option is implemented by encapsulating the type as Zoi.optional(Zoi.default(type, default_value)):

iex> schema = Zoi.object(%{name: Zoi.optional(Zoi.default(Zoi.string(), "default value"))})
iex> Zoi.parse(schema, %{})
{:ok, %{}}
iex> Zoi.parse(schema, %{name: nil})
{:ok, %{name: "default value"}}

tuple(fields, opts \\ [])

Defines a tuple type schema.

Use Zoi.tuple(fields) to define a tuple with specific types for each element:

iex> schema = Zoi.tuple({Zoi.string(), Zoi.integer()})
iex> Zoi.parse(schema, {"hello", 42})
{:ok, {"hello", 42}}
iex> Zoi.parse(schema, {"hello", "world"})
{:error, [%Zoi.Error{message: "invalid type: must be an integer", path: [1]}]}

Encapsulated Types

default(inner, value, opts \\ [])

Creates a default value for the schema.

This allows you to specify a default value that will be used if the input is nil or not provided.

Example

iex> schema = Zoi.string() |> Zoi.default("default value")
iex> Zoi.parse(schema, nil)
{:ok, "default value"}

intersection(fields, opts \\ [])

Defines an intersection type schema.

An intersection type allows you to combine multiple schemas into one, requiring that the input data satisfies all of them.

Example

iex> schema = Zoi.intersection([
...>   Zoi.string() |> Zoi.min(2),
...>   Zoi.string() |> Zoi.max(5)
...> ])
iex> Zoi.parse(schema, "helloworld")
{:error, [%Zoi.Error{message: "too big: must have at most 5 characters"}]}
iex> Zoi.parse(schema, "hi")
{:ok, "hi"}

If you define the validation on the intersection itself, it will apply to all types in the intersection:

iex> schema = Zoi.intersection([
...>   Zoi.string(),
...>   Zoi.integer(coerce: true)
...> ]) |> Zoi.min(3)
iex> Zoi.parse(schema, "115")
{:ok, 115}
iex> Zoi.parse(schema, "2")
{:error, [%Zoi.Error{message: "too small: must have at least 3 characters"}]}

nullable(opts \\ [])

Defines a schema that allows nil values.

Examples

iex> schema = Zoi.string() |> Zoi.nullable()
iex> Zoi.parse(schema, nil)
{:ok, nil}
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}

optional(opts \\ [])

Makes the schema optional for the Zoi.object/2 type.

Example

iex> schema = Zoi.object(%{name: Zoi.string() |> Zoi.optional()})
iex> Zoi.parse(schema, %{})
{:ok, %{}}

union(fields, opts \\ [])

Defines a union type schema.

Example

iex> schema = Zoi.union([Zoi.string(), Zoi.integer()])
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, 42)
{:ok, 42}
iex> Zoi.parse(schema, true)
{:error, [%Zoi.Error{message: "invalid type: must be an integer"}]}

This type also allows to define validations for each type in the union:

iex> schema = Zoi.union([
...>   Zoi.string() |> Zoi.min(2),
...>   Zoi.integer() |> Zoi.min(0)
...> ])
iex> Zoi.parse(schema, "h") # fails on string and try to parse as integer
{:error, [%Zoi.Error{message: "invalid type: must be an integer"}]}
iex> Zoi.parse(schema, -1)
{:error, [%Zoi.Error{message: "too small: must be at least 0"}]}

If you define the validation on the union itself, it will apply to all types in the union:

iex> schema = Zoi.union([
...>   Zoi.string(),
...>   Zoi.integer()
...> ]) |> Zoi.min(3)
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, 2)
{:error, [%Zoi.Error{message: "too small: must be at least 3"}]}

Extensions

refine(schema, fun)

@spec refine(schema :: Zoi.Type.t(), fun :: refinement()) :: Zoi.Type.t()

Adds a custom validation function to the schema.

This function will be called with the input data and options, and should return :ok for valid data or {:error, reason} for invalid data.

iex> schema = Zoi.string() |> Zoi.refine(fn value -> 
...>   if String.length(value) > 5, do: :ok, else: {:error, "must be longer than 5 characters"}
...> end)
iex> Zoi.parse(schema, "hello")
{:error, [%Zoi.Error{message: "must be longer than 5 characters"}]}
iex> Zoi.parse(schema, "hello world")
{:ok, "hello world"}

Returning multiple errors

You can use the context when defining the Zoi.refine/2 function to return multiple errors.

iex> schema = Zoi.string() |> Zoi.refine(fn value, ctx ->
...>   if String.length(value) < 5 do
...>     Zoi.Context.add_error(ctx, "must be longer than 5 characters")
...>     |> Zoi.Context.add_error("must be shorter than 10 characters")
...>   end
...> end)
iex> Zoi.parse(schema, "hi")
{:error, [
  %Zoi.Error{message: "must be longer than 5 characters"},
  %Zoi.Error{message: "must be shorter than 10 characters"}
]}

mfa

You can also pass a mfa (module, function, args) to the Zoi.refine/2 function. This is recommended if you are declaring schemas during compile time:

defmodule MySchema do
  use Zoi

  @schema Zoi.string() |> Zoi.refine({__MODULE__, :validate, []})

  def validate(value, opts \\ []) do
    if String.length(value) > 5 do
      :ok
    else 
      {:error, "must be longer than 5 characters"}
    end
  end
end

Since during the module compilation, anonymous functions are not available, you can use the mfa option to pass a module, function and arguments. The opts argument is mandatory, this is where the ctx is passed to the function and you can leverage the Zoi.Context to add extra errors. In general, most cases the :ok or {:error, reason} returns will be enough. Use the context only if you need extra errors or modify the context in some way.

transform(schema, fun)

@spec transform(schema :: Zoi.Type.t(), fun :: transform()) :: Zoi.Type.t()

Adds a transformation function to the schema.

This function will be applied to the input data after parsing but before validations.

Example

iex> schema = Zoi.string() |> Zoi.transform(fn value ->
...>   {:ok, String.trim(value)}
...> end)
iex> Zoi.parse(schema, "  hello world  ")
{:ok, "hello world"}

You can also use mfa (module, function, args) to pass a transformation function:

iex> defmodule MyTransforms do
...>   def trim(value, _opts) do
...>     {:ok, String.trim(value)}
...>   end
...> end
iex> schema = Zoi.string() |> Zoi.transform({MyTransforms, :trim, []})
iex> Zoi.parse(schema, "  hello world  ")
{:ok, "hello world"}

This is useful if you are defining schemas at compile time, where anonymous functions are not available. The opts argument is mandatory, this is where the ctx is passed to the function and you can leverage the Zoi.Context to add extra errors. In general, most cases the {:ok, value} or {:error, reason} returns will be enough. Use the context only if you need extra errors or modify the context in some way.

Using context for validation

You can use the context when defining the Zoi.transform/2 function to return multiple errors.

iex> schema = Zoi.string() |> Zoi.transform(fn value, ctx ->
...>   if String.length(value) < 5 do
...>     Zoi.Context.add_error(ctx, "must be longer than 5 characters")
...>     |> Zoi.Context.add_error("must be shorter than 10 characters")
...>   else
...>     {:ok, String.trim(value)}
...>   end
...> end)
iex> Zoi.parse(schema, "hi")
{:error, [
  %Zoi.Error{message: "must be longer than 5 characters"},
  %Zoi.Error{message: "must be shorter than 10 characters"}
]}

The ctx is a Zoi.Context struct that contains information about the current parsing context, including the path, options, and any errors that have been added so far.

Formats

email()

@spec email() :: Zoi.Type.t()

Validates that the string is a valid email format.

Example

iex> schema = Zoi.email()
iex> Zoi.parse(schema, "test@test.com")
{:ok, "test@test.com"}
iex> Zoi.parse(schema, "invalid-email")
{:error, [%Zoi.Error{message: "invalid email format"}]}

It uses a regex pattern to validate the email format, which checks for a standard email structure including local part, domain, and top-level domain:

~r/^(?!.)(?!.*..)([a-z0-9_'+-.]*)[a-z0-9_+-]@([a-z0-9][a-z0-9-]*.)+[a-z]{2,}$/i

ipv4()

Validates that the string is a valid IPv4 address.

Example

iex> schema = Zoi.ipv4()
iex> Zoi.parse(schema, "127.0.0.1")
{:ok, "127.0.0.1"}

ipv6()

Validates that the string is a valid IPv6 address.

Example

iex> schema = Zoi.ipv6()
iex> Zoi.parse(schema, "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
{:ok, "2001:0db8:85a3:0000:0000:8a2e:0370:7334"}
iex> Zoi.parse(schema, "invalid-ipv6")
{:error, [%Zoi.Error{message: "invalid IPv6 address"}]}

url()

Defines a URL format validation.

Example

iex> schema = Zoi.url()
iex> Zoi.parse(schema, "https://example.com")
{:ok, "https://example.com"}
iex> Zoi.parse(schema, "invalid-url")
{:error, [%Zoi.Error{message: "invalid URL"}]}

uuid(opts \\ [])

@spec uuid(opts :: keyword()) :: Zoi.Type.t()

Validates that the string is a valid UUID format.

You can specify the UUID version using the :version option, which can be one of "v1", "v2", "v3", "v4", "v5", "v6", "v7", or "v8". If no version is specified, it defaults to any valid UUID format.

Example

iex> schema = Zoi.uuid()
iex> Zoi.parse(schema, "550e8400-e29b-41d4-a716-446655440000")
{:ok, "550e8400-e29b-41d4-a716-446655440000"}
iex> Zoi.parse(schema, "invalid-uuid")
{:error, [%Zoi.Error{message: "invalid UUID format"}]}

iex> schema = Zoi.uuid(version: "v8")
iex> Zoi.parse(schema, "6d084cef-a067-8e9e-be6d-7c5aefdfd9b4")
{:ok, "6d084cef-a067-8e9e-be6d-7c5aefdfd9b4"}

Parsing

parse(schema, input, opts \\ [])

@spec parse(schema :: Zoi.Type.t(), input :: input(), opts :: options()) :: result()

Parse input data against a schema. Accepts optional coerce: true option to enable coercion.

Examples

iex> schema = Zoi.string() |> Zoi.min(2) |> Zoi.max(100)
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, "h")
{:error, [%Zoi.Error{message: "too small: must have at least 2 characters"}]}
iex> Zoi.parse(schema, 123, coerce: true)
{:ok, "123"}

Refinements

ends_with(schema, suffix, opts \\ [])

@spec ends_with(schema :: Zoi.Type.t(), suffix :: binary(), opts :: options()) ::
  Zoi.Type.t()

Validates that a string ends with a specific suffix.

Example

iex> schema = Zoi.string() |> Zoi.ends_with("world")
iex> Zoi.parse(schema, "hello world")
{:ok, "hello world"}
iex> Zoi.parse(schema, "hello")
{:error, [%Zoi.Error{message: "invalid string: must end with 'world'"}]}

gt(schema, gt, opts \\ [])

@spec gt(schema :: Zoi.Type.t(), gt :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

Validates that the input is greater than a specific value.

This can be used for strings, integers, floats and numbers.

Example

iex> schema = Zoi.integer() |> Zoi.gt(2)
iex> Zoi.parse(schema, 3)
{:ok, 3}
iex> Zoi.parse(schema, 2)
{:error, [%Zoi.Error{message: "too small: must be greater than 2"}]}

gte(schema, gte, opts \\ [])

@spec gte(schema :: Zoi.Type.t(), min :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

Validates that the input is greater than or equal to a value.

This can be used for strings, integers, floats and numbers.

Example

iex> schema = Zoi.string() |> Zoi.gte(3)
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, "hi")
{:error, [%Zoi.Error{message: "too small: must have at least 3 characters"}]}

length(schema, length, opts \\ [])

@spec length(schema :: Zoi.Type.t(), length :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

Validates that the string has a specific length.

Example

iex> schema = Zoi.string() |> Zoi.length(5)
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, "hi")
{:error, [%Zoi.Error{message: "invalid length: must have 5 characters"}]}

lt(schema, lt, opts \\ [])

@spec lt(schema :: Zoi.Type.t(), lt :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

Validates that the input is less than a specific value.

This can be used for strings, integers, floats and numbers.

Example

iex> schema = Zoi.integer() |> Zoi.lt(10)
iex> Zoi.parse(schema, 5)
{:ok, 5}
iex> Zoi.parse(schema, 10)
{:error, [%Zoi.Error{message: "too big: must be less than 10"}]}

lte(schema, lte, opts \\ [])

@spec lte(schema :: Zoi.Type.t(), lte :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

Validates that the input is less than or equal to a value.

This can be used for strings, integers, floats and numbers.

Example

iex> schema = Zoi.string() |> Zoi.lte(5)
iex> Zoi.parse(schema, "hello")
{:ok, "hello"}
iex> Zoi.parse(schema, "hello world")
{:error, [%Zoi.Error{message: "too big: must have at most 5 characters"}]}

max(schema, max, opts \\ [])

@spec max(schema :: Zoi.Type.t(), max :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

alias for Zoi.lte/2

min(schema, min, opts \\ [])

@spec min(schema :: Zoi.Type.t(), min :: non_neg_integer(), opts :: options()) ::
  Zoi.Type.t()

alias for Zoi.gte/2

regex(schema, regex, opts \\ [])

@spec regex(schema :: Zoi.Type.t(), regex :: Regex.t(), opts :: options()) ::
  Zoi.Type.t()

Validates that the input matches a given regex pattern.

Example

iex> schema = Zoi.string() |> Zoi.regex(~r/^\d+$/)
iex> Zoi.parse(schema, "12345")
{:ok, "12345"}

starts_with(schema, prefix, opts \\ [])

@spec starts_with(schema :: Zoi.Type.t(), prefix :: binary(), opts :: options()) ::
  Zoi.Type.t()

Validates that a string starts with a specific prefix.

Example

iex> schema = Zoi.string() |> Zoi.starts_with("hello")
iex> Zoi.parse(schema, "hello world")
{:ok, "hello world"}
iex> Zoi.parse(schema, "world hello")
{:error, [%Zoi.Error{message: "invalid string: must start with 'hello'"}]}

Structured Types

date(opts \\ [])

Defines a date type schema.

This type is used to validate and parse date values. It will convert the input to a Date structure.

Example

iex> schema = Zoi.date()
iex> Zoi.parse(schema, ~D[2000-01-01])
{:ok, ~D[2000-01-01]}
iex> Zoi.parse(schema, "2000-01-01")
{:error, [%Zoi.Error{message: "invalid type: must be a date"}]}

You can also specify the :coerce option to allow coercion from strings or integers:

iex> schema = Zoi.date(coerce: true)
iex> Zoi.parse(schema, "2000-01-01")
{:ok, ~D[2000-01-01]}
iex> Zoi.parse(schema, 730_485) # 730_485 is the number of days since epoch
{:ok, ~D[2000-01-01]}

datetime(opts \\ [])

Defines a DateTime type schema.

This type is used to validate and parse DateTime values. It will convert the input to a DateTime structure.

Example

iex> schema = Zoi.datetime()
iex> Zoi.parse(schema, ~U[2000-01-01 12:34:56Z])
{:ok, ~U[2000-01-01 12:34:56Z]}
iex> Zoi.parse(schema, "2000-01-01T12:34:56Z")
{:error, [%Zoi.Error{message: "invalid type: must be a datetime"}]}

You can also specify the :coerce option to allow coercion from strings or integers:

iex> schema = Zoi.datetime(coerce: true)
iex> Zoi.parse(schema, "2000-01-01T12:34:56Z")
{:ok, ~U[2000-01-01 12:34:56Z]}
iex> Zoi.parse(schema, 1_464_096_368) # 1_464_096_368 is the Unix timestamp
{:ok, ~U[2016-05-24 13:26:08Z]}

decimal(opts \\ [])

Defines a decimal type schema.

This type is used to validate and parse decimal numbers, which can be useful for financial calculations or precise numeric values. It uses the Decimal library for handling decimal numbers. It will convert the input to a Decimal structure.

Example

iex> schema = Zoi.decimal()
iex> Zoi.parse(schema, Decimal.new("123.45"))
{:ok, Decimal.new("123.45")}
iex> Zoi.parse(schema, "invalid-decimal")
{:error, [%Zoi.Error{message: "invalid type: must be a decimal"}]}

You can also specify the :coerce option to allow coercion from strings or integers:

iex> schema = Zoi.decimal(coerce: true)
iex> Zoi.parse(schema, "123.45")
{:ok, Decimal.new("123.45")}
iex> Zoi.parse(schema, 123)
{:ok, Decimal.new("123")}

naive_datetime(opts \\ [])

Defines a NaiveDateTime type schema.

This type is used to validate and parse NaiveDateTime values. It will convert the input to a NaiveDateTime structure.

Example

iex> schema = Zoi.naive_datetime()
iex> Zoi.parse(schema, ~N[2000-01-01 23:00:07])
{:ok, ~N[2000-01-01 23:00:07]}
iex> Zoi.parse(schema, "2000-01-01T12:34:56")
{:error, [%Zoi.Error{message: "invalid type: must be a naive datetime"}]}

You can also specify the :coerce option to allow coercion from strings or integers:

iex> schema = Zoi.naive_datetime(coerce: true)
iex> Zoi.parse(schema, "2000-01-01T12:34:56")
{:ok, ~N[2000-01-01 12:34:56]}
iex> Zoi.parse(schema, 1) # 1  is the number of days since epoch
{:ok, ~N[0000-01-01 00:00:01]}

time(opts \\ [])

Defines a time type schema.

This type is used to validate and parse time values. It will convert the input to a Time structure.

Example

iex> schema = Zoi.time()
iex> Zoi.parse(schema, ~T[12:34:56])
{:ok, ~T[12:34:56]}
iex> Zoi.parse(schema, "12:34:56")
{:error, [%Zoi.Error{message: "invalid type: must be a time"}]}

You can also specify the :coerce option to allow coercion from strings:

iex> schema = Zoi.time(coerce: true)
iex> Zoi.parse(schema, "12:34:56")
{:ok, ~T[12:34:56]}

Transforms

to_downcase(schema)

Converts a string to lowercase.

Example

iex> schema = Zoi.string() |> Zoi.to_downcase()
iex> Zoi.parse(schema, "Hello World")
{:ok, "hello world"}

to_struct(schema, module)

@spec to_struct(schema :: Zoi.Type.t(), struct :: module()) :: Zoi.Type.t()

Converts a schema to a struct of the given module. This is useful for transforming parsed data into a specific struct type.

Example

defmodule User do
  defstruct [:name, :age]
end

schema = Zoi.object(%{
  name: Zoi.string(),
  age: Zoi.integer()
})
|> Zoi.to_struct(User)

Zoi.parse(schema, %{name: "Alice", age: 30})
#=> {:ok, %User{name: "Alice", age: 30}}

to_upcase(schema)

@spec to_upcase(schema :: Zoi.Type.t()) :: Zoi.Type.t()

Converts a string to uppercase.

Example

iex> schema = Zoi.string() |> Zoi.to_upcase()
iex> Zoi.parse(schema, "Hello World")
{:ok, "HELLO WORLD"}

trim(schema)

@spec trim(schema :: Zoi.Type.t()) :: Zoi.Type.t()

Trims whitespace from the beginning and end of a string.

Example

iex> schema = Zoi.string() |> Zoi.trim()
iex> Zoi.parse(schema, "  hello world  ")
{:ok, "hello world"}