Xema.Builder (xema v0.13.6) View Source
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
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.
Link to this section Functions
Specs
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
Specs
any() :: :any
Returns :any.
Specs
Returns a tuple of :any
and the given keyword list.
Examples
iex> Xema.Builder.any(key: 42)
{:any, key: 42}
Specs
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
Specs
atom() :: :atom
Returns :atom.
Specs
Returns a tuple of :atom
and the given keyword list.
Examples
iex> Xema.Builder.atom(key: 42)
{:atom, key: 42}
Specs
boolean() :: :boolean
Returns :boolean.
Specs
Returns a tuple of :boolean
and the given keyword list.
Examples
iex> Xema.Builder.boolean(key: 42)
{:boolean, key: 42}
Specs
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. Thetype
can also be astruct
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".
Specs
float() :: :float
Returns :float.
Specs
Returns a tuple of :float
and the given keyword list.
Examples
iex> Xema.Builder.float(key: 42)
{:float, key: 42}
Specs
integer() :: :integer
Returns :integer.
Specs
Returns a tuple of :integer
and the given keyword list.
Examples
iex> Xema.Builder.integer(key: 42)
{:integer, key: 42}
Specs
keyword() :: :keyword
Returns :keyword.
Specs
Returns a tuple of :keyword
and the given keyword list.
Examples
iex> Xema.Builder.keyword(key: 42)
{:keyword, key: 42}
Specs
list() :: :list
Returns :list.
Specs
Returns a tuple of :list
and the given keyword list.
Examples
iex> Xema.Builder.list(key: 42)
{:list, key: 42}
Specs
map() :: :map
Returns :map.
Specs
Returns a tuple of :map
and the given keyword list.
Examples
iex> Xema.Builder.map(key: 42)
{:map, key: 42}
Specs
number() :: :number
Returns :number.
Specs
Returns a tuple of :number
and the given keyword list.
Examples
iex> Xema.Builder.number(key: 42)
{:number, key: 42}
Specs
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}
.
Specs
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"}
Specs
string() :: :string
Returns :string.
Specs
Returns a tuple of :string
and the given keyword list.
Examples
iex> Xema.Builder.string(key: 42)
{:string, key: 42}
Specs
strux() :: :struct
Returns :struct
.
Specs
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.
Specs
tuple() :: :tuple
Returns :tuple.
Specs
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.