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]}
Returns a tuple of :atom
and the given keyword list.
Examples
iex> Xema.Builder.atom(key: 42)
{:atom, [key: 42]}
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 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".
Returns a tuple of :float
and the given keyword list.
Examples
iex> Xema.Builder.float(key: 42)
{:float, [key: 42]}
Returns a tuple of :integer
and the given keyword list.
Examples
iex> Xema.Builder.integer(key: 42)
{:integer, [key: 42]}
Returns a tuple of :keyword
and the given keyword list.
Examples
iex> Xema.Builder.keyword(key: 42)
{:keyword, [key: 42]}
Returns a tuple of :list
and the given keyword list.
Examples
iex> Xema.Builder.list(key: 42)
{:list, [key: 42]}
Returns a tuple of :map
and the given keyword list.
Examples
iex> Xema.Builder.map(key: 42)
{:map, [key: 42]}
Returns a tuple of :number
and the given keyword list.
Examples
iex> Xema.Builder.number(key: 42)
{:number, [key: 42]}
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 do
...> field :name, :string, min_length: 1
...> required [:name]
...> end
...> end
...>
iex> %{"name" => "Tim"} |> Person.cast!() |> Map.from_struct()
%{name: "Tim"}
Returns a tuple of :string
and the given keyword list.
Examples
iex> Xema.Builder.string(key: 42)
{:string, [key: 42]}
Returns :struct
.
Returns the tuple {:struct, module: module}
.
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.