xema v0.11.0 Xema.Builder View Source

This module contains some convenience functions to generate schemas.

Examples

iex> import Xema.Builder
...> schema = Xema.new integer(minimum: 1)
...> Xema.valid?(schema, 6)
true
...> Xema.valid?(schema, 0)
false

Link to this section Summary

Functions

Returns a tuple of :any and the given keyword list.

Returns a tuple of :atom and the given keyword list.

Returns a tuple of :boolean and the given keyword list.

Specifies a field. This function will be used inside xema/0.

Returns a tuple of :float and the given keyword list.

Returns a tuple of :integer and the given keyword list.

Returns a tuple of :keyword and the given keyword list.

Returns a tuple of :list and the given keyword list.

Returns a tuple of :map and the given keyword list.

Returns a tuple of :number and the given keyword list.

Returns the tuple {:ref, ref}.

Sets the list of required fields. Specifies a field. This function will be used inside xema/0.

Returns a tuple of :string and the given keyword list.

Returns :struct.

Returns the tuple {:struct, module: module}.

Returns a tuple of :tuple and the given keyword list.

Creates a schema.

Creates a schema with the given name.

Link to this section Functions

Returns a tuple of :any and the given keyword list.

Examples

iex> Xema.Builder.any(key: 42)
{:any, [key: 42]}
Link to this function

any(keywords)

View Source
any(keyword()) :: {:any, keyword()}

Returns a tuple of :atom and the given keyword list.

Examples

iex> Xema.Builder.atom(key: 42)
{:atom, [key: 42]}
Link to this function

atom(keywords)

View Source
atom(keyword()) :: {:atom, keyword()}
Link to this function

boolean()

View Source
boolean() :: :boolean

Returns a tuple of :boolean and the given keyword list.

Examples

iex> Xema.Builder.boolean(key: 42)
{:boolean, [key: 42]}
Link to this function

boolean(keywords)

View Source
boolean(keyword()) :: {:boolean, keyword()}
Link to this function

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

View Source
field(atom(), Xema.Schema.type() | module(), keyword()) ::
  {:xema, Xema.t()} | {:module, module()} | {:type, atom()}

Specifies a field. This function will be used inside xema/0.

Arguments:

  • name: the name of the field.
  • type: the type of the field. The type can also be a struct or another schema.
  • opts: the rules for the field.

Examples

iex> defmodule User do
...>   use Xema
...>
...>   xema do
...>     field :name, :string, min_length: 1
...>   end
...> end
...>
iex> %{"name" => "Tim"} |> User.cast!() |> Map.from_struct()
%{name: "Tim"}

For more examples see "Examples: Struct".

Link to this function

float()

View Source
float() :: :float

Returns a tuple of :float and the given keyword list.

Examples

iex> Xema.Builder.float(key: 42)
{:float, [key: 42]}
Link to this function

float(keywords)

View Source
float(keyword()) :: {:float, keyword()}
Link to this function

integer()

View Source
integer() :: :integer

Returns a tuple of :integer and the given keyword list.

Examples

iex> Xema.Builder.integer(key: 42)
{:integer, [key: 42]}
Link to this function

integer(keywords)

View Source
integer(keyword()) :: {:integer, keyword()}
Link to this function

keyword()

View Source
keyword() :: :keyword

Returns a tuple of :keyword and the given keyword list.

Examples

iex> Xema.Builder.keyword(key: 42)
{:keyword, [key: 42]}
Link to this function

keyword(keywords)

View Source
keyword(keyword()) :: {:keyword, keyword()}

Returns a tuple of :list and the given keyword list.

Examples

iex> Xema.Builder.list(key: 42)
{:list, [key: 42]}
Link to this function

list(keywords)

View Source
list(keyword()) :: {:list, keyword()}

Returns a tuple of :map and the given keyword list.

Examples

iex> Xema.Builder.map(key: 42)
{:map, [key: 42]}
Link to this function

map(keywords)

View Source
map(keyword()) :: {:map, keyword()}
Link to this function

number()

View Source
number() :: :number

Returns a tuple of :number and the given keyword list.

Examples

iex> Xema.Builder.number(key: 42)
{:number, [key: 42]}
Link to this function

number(keywords)

View Source
number(keyword()) :: {:number, keyword()}

Returns the tuple {:ref, ref}.

Link to this function

required(fields)

View Source
required([atom()]) :: term()

Sets the list of required fields. Specifies a field. This function will be used inside xema/0.

Examples

iex> defmodule Person do
...>   use Xema
...>
...>   xema do
...>     field :name, :string, min_length: 1
...>     required [:name]
...>   end
...> end
...>
iex> %{"name" => "Tim"} |> Person.cast!() |> Map.from_struct()
%{name: "Tim"}
Link to this function

string()

View Source
string() :: :string

Returns a tuple of :string and the given keyword list.

Examples

iex> Xema.Builder.string(key: 42)
{:string, [key: 42]}
Link to this function

string(keywords)

View Source
string(keyword()) :: {:string, keyword()}
Link to this function

strux()

View Source
strux() :: :struct

Returns :struct.

Link to this function

strux(keywords)

View Source
strux(keyword()) :: {:struct, keyword()}
strux(atom()) :: {:struct, [{:module, module()}]}

Returns the tuple {:struct, module: module}.

Link to this function

tuple()

View Source
tuple() :: :tuple

Returns a tuple of :tuple and the given keyword list.

Examples

iex> Xema.Builder.tuple(key: 42)
{:tuple, [key: 42]}
Link to this function

tuple(keywords)

View Source
tuple(keyword()) :: {:tuple, keyword()}

Creates a schema.

Link to this macro

xema(name, list)

View Source (macro)

Creates a schema with the given name.