View Source Xema.Builder (xema v0.17.4)
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
iex> import Xema.Builder
...> schema = Xema.new integer(minimum: 1)
...> Xema.valid?(schema, 6)
true
...> Xema.valid?(schema, 0)
false
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.
Functions
@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
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.
Returns a tuple of :any
and the given keyword list.
Examples
iex> Xema.Builder.any(key: 42)
{:any, key: 42}
@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
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.
Returns a tuple of :atom
and the given keyword list.
Examples
iex> Xema.Builder.atom(key: 42)
{:atom, key: 42}
@spec boolean() :: :boolean
Returns :boolean.
Returns a tuple of :boolean
and the given keyword list.
Examples
iex> Xema.Builder.boolean(key: 42)
{:boolean, key: 42}
Specifies a field. This function will be used inside xema/0
.
Arguments:
name
: the name of the field.type
: the type of the field. Thetype
can also be astruct
or another schema.opts
: the rules for the field.
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.
Returns a tuple of :float
and the given keyword list.
Examples
iex> Xema.Builder.float(key: 42)
{:float, key: 42}
@spec integer() :: :integer
Returns :integer.
Returns a tuple of :integer
and the given keyword list.
Examples
iex> Xema.Builder.integer(key: 42)
{:integer, key: 42}
@spec keyword() :: :keyword
Returns :keyword.
Returns a tuple of :keyword
and the given keyword list.
Examples
iex> Xema.Builder.keyword(key: 42)
{:keyword, key: 42}
@spec list() :: :list
Returns :list.
Returns a tuple of :list
and the given keyword list.
Examples
iex> Xema.Builder.list(key: 42)
{:list, key: 42}
@spec map() :: :map
Returns :map.
Returns a tuple of :map
and the given keyword list.
Examples
iex> Xema.Builder.map(key: 42)
{:map, key: 42}
@spec number() :: :number
Returns :number.
Returns a tuple of :number
and the given keyword list.
Examples
iex> Xema.Builder.number(key: 42)
{:number, key: 42}
@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
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}
.
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_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.
Returns a tuple of :string
and the given keyword list.
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.
Returns a tuple of :tuple
and the given keyword list.
Examples
iex> Xema.Builder.tuple(key: 42)
{:tuple, key: 42}
Creates a schema
.
Creates a schema
with the given name.
Creates a schema
and defines the corresponding struct.