View Source Xema.Builder (xema v0.16.0)

This module contains some convenience functions to generate schemas. Beside the type-functions the module contains the combinator-functions all_of/2, any_of/2 and one_of/2.

examples

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 with the given type (default :any) and the given schemas tagged by the keyword :all_of. This function provides a shortcut for something like integer(all_of: [...]) or any(all_of: [...]).

Returns :any.

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

Returns a tuple with the given type (default :any) and the given schemas tagged by the keyword :any_of. This function provides a shortcut for something like integer(any_of: [...]) or any(any_of: [...]).

Returns :atom.

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

Returns :boolean.

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

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

Returns :float.

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

Returns :integer.

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

Returns :keyword.

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

Returns :list.

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

Returns :map.

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

Returns :number.

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

Returns a tuple with the given type (default :any) and the given schemas tagged by the keyword :one_of. This function provides a shortcut for something like integer(one_of: [...]) or any(one_of: [...]).

Returns the tuple {:ref, ref}.

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

Returns :string.

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

Returns :struct.

Returns a tuple of :struct and the given keyword list when the function gets a keyword list.

Returns :tuple.

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

Creates a schema.

Creates a schema with the given name.

Creates a schema and defines the corresponding struct.

Link to this section Functions

Link to this function

all_of(type \\ :any, schemas)

View Source
@spec all_of(type, [schema]) :: {type, [{:all_of, [schema]}]}
when type: Xema.Schema.type(),
     schema: Xema.Schema.t() | Xema.Schema.type() | tuple() | atom() | keyword()

Returns a tuple with the given type (default :any) and the given schemas tagged by the keyword :all_of. This function provides a shortcut for something like integer(all_of: [...]) or any(all_of: [...]).

examples

Examples

iex> Xema.Builder.all_of([:integer, :string])
{:any, all_of: [:integer, :string] }

iex> Xema.Builder.all_of(:integer, [[minimum: 10], [maximum: 5]])
{:integer, all_of: [[minimum: 10], [maximum: 5]]}
defmodule MySchema do
  use Xema

  xema do
    all_of [
      list(items: integer(minimum: 1, maximum: 66)),
      list(items: integer(minimum: 33, maximum: 100))
    ]
  end
end

MySchema.valid?([20, 30]) #=> false
MySchema.valid?([40, 50]) #=> true
MySchema.valid?([60, 70]) #=> false
MySchema.valid?([10, 90]) #=> false
@spec any() :: :any

Returns :any.

@spec any(keyword()) :: {:any, keyword()}

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

examples

Examples

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

any_of(type \\ :any, schemas)

View Source
@spec any_of(type, [schema]) :: {type, [{:any_of, [schema]}]}
when type: Xema.Schema.type(),
     schema: Xema.Schema.t() | Xema.Schema.type() | tuple() | atom() | keyword()

Returns a tuple with the given type (default :any) and the given schemas tagged by the keyword :any_of. This function provides a shortcut for something like integer(any_of: [...]) or any(any_of: [...]).

examples

Examples

iex> Xema.Builder.any_of([:integer, :string])
{:any, any_of: [:integer, :string] }

iex> Xema.Builder.any_of(:integer, [[minimum: 10], [maximum: 5]])
{:integer, any_of: [[minimum: 10], [maximum: 5]]}
defmodule MySchema do
  use Xema

  xema do
    any_of [
      list(items: integer(minimum: 1, maximum: 66)),
      list(items: integer(minimum: 33, maximum: 100))
    ]
  end
end

MySchema.valid?([20, 30]) #=> true
MySchema.valid?([40, 50]) #=> true
MySchema.valid?([60, 70]) #=> true
MySchema.valid?([10, 90]) #=> false
@spec atom() :: :atom

Returns :atom.

@spec atom(keyword()) :: {:atom, keyword()}

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

examples

Examples

iex> Xema.Builder.atom(key: 42)
{:atom, key: 42}
@spec boolean() :: :boolean

Returns :boolean.

@spec boolean(keyword()) :: {:boolean, keyword()}

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

examples

Examples

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

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

View Source
@spec field(atom(), Xema.Schema.type() | module(), keyword()) ::
  {atom() | [atom()], keyword()}

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

Examples

iex> defmodule User do
...>   use Xema
...>
...>   xema_struct 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".

@spec float() :: :float

Returns :float.

@spec float(keyword()) :: {:float, keyword()}

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

examples

Examples

iex> Xema.Builder.float(key: 42)
{:float, key: 42}
@spec integer() :: :integer

Returns :integer.

@spec integer(keyword()) :: {:integer, keyword()}

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

examples

Examples

iex> Xema.Builder.integer(key: 42)
{:integer, key: 42}
@spec keyword() :: :keyword

Returns :keyword.

@spec keyword(keyword()) :: {:keyword, keyword()}

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

examples

Examples

iex> Xema.Builder.keyword(key: 42)
{:keyword, key: 42}
@spec list() :: :list

Returns :list.

@spec list(keyword()) :: {:list, keyword()}

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

examples

Examples

iex> Xema.Builder.list(key: 42)
{:list, key: 42}
@spec map() :: :map

Returns :map.

@spec map(keyword()) :: {:map, keyword()}

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

examples

Examples

iex> Xema.Builder.map(key: 42)
{:map, key: 42}
@spec number() :: :number

Returns :number.

@spec number(keyword()) :: {:number, keyword()}

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

examples

Examples

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

one_of(type \\ :any, schemas)

View Source
@spec one_of(type, [schema]) :: {type, [{:one_of, [schema]}]}
when type: Xema.Schema.type(),
     schema: Xema.Schema.t() | Xema.Schema.type() | tuple() | atom() | keyword()

Returns a tuple with the given type (default :any) and the given schemas tagged by the keyword :one_of. This function provides a shortcut for something like integer(one_of: [...]) or any(one_of: [...]).

examples

Examples

iex> Xema.Builder.one_of([:integer, :string])
{:any, one_of: [:integer, :string] }

iex> Xema.Builder.one_of(:integer, [[minimum: 10], [maximum: 5]])
{:integer, one_of: [[minimum: 10], [maximum: 5]]}
defmodule MySchema do
  use Xema

  xema do
    one_of [
      list(items: integer(minimum: 1, maximum: 66)),
      list(items: integer(minimum: 33, maximum: 100))
    ]
  end
end

MySchema.valid?([20, 30]) #=> true
MySchema.valid?([40, 50]) #=> false
MySchema.valid?([60, 70]) #=> true
MySchema.valid?([10, 90]) #=> false

Returns the tuple {:ref, ref}.

@spec required([atom()]) :: term()

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

examples

Examples

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

Returns :string.

@spec string(keyword()) :: {:string, keyword()}

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

examples

Examples

iex> Xema.Builder.string(key: 42)
{:string, key: 42}
@spec strux() :: :struct

Returns :struct.

@spec strux(keyword()) :: {:struct, keyword()}
@spec strux(atom()) :: {:struct, [{:module, module()}]}

Returns a tuple of :struct and the given keyword list when the function gets a keyword list.

Returns the tuple {:struct, module: module} when the function gets an atom.

@spec tuple() :: :tuple

Returns :tuple.

@spec tuple(keyword()) :: {:tuple, keyword()}

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

examples

Examples

iex> Xema.Builder.tuple(key: 42)
{:tuple, key: 42}

Creates a schema.

Link to this macro

xema(name, list)

View Source (macro)

Creates a schema with the given name.

Link to this macro

xema_struct(list)

View Source (macro)

Creates a schema and defines the corresponding struct.