xema v0.8.1 Xema View Source
A schema validator inspired by JSON Schema.
All available keywords to construct a schema are described on page Usage.
This module can be used to construct a schema module.
use Xema
imports Xema.Builder
and extends the module with the functions
__MODULE__.valid?/2
__MODULE__.validate/2
__MODULE__.validate!/2
The macro xema/2
supports the construction of a schema. After that
the schema is available as a function.
A schema can also be tagged with @default true
and then called by
__MODULE__.valid?/1
__MODULE__.validate/1
__MODULE__.validate!/1
Example
iex> defmodule Schema do
...> use Xema
...>
...> @pos integer(minimum: 0)
...> @neg integer(maximum: 0)
...>
...> @default true
...> xema :user,
...> map(
...> properties: %{
...> name: string(min_length: 1),
...> age: @pos
...> }
...> )
...>
...> xema :nums,
...> map(
...> properties: %{
...> pos: list(items: @pos),
...> neg: list(items: @neg)
...> }
...> )
...> end
iex>
iex> Schema.valid?(:user, %{name: "John", age: 21})
true
iex> Schema.valid?(%{name: "John", age: 21})
true
iex> Schema.valid?(%{name: "", age: 21})
false
iex> Schema.validate(%{name: "John", age: 21})
:ok
iex> Schema.validate(%{name: "", age: 21})
{:error, %{properties: %{name: %{min_length: 1, value: ""}}}}
iex> Schema.valid?(:nums, %{pos: [1, 2, 3]})
true
iex> Schema.valid?(:nums, %{neg: [1, 2, 3]})
false
Link to this section Summary
Functions
This function creates the schema from the given data
.
Returns the source for a given xema
. The output can differ from the input
if the schema contains references. To get the original source the schema
must be created with inline: false
.
Returns true
if the value
is a valid value against the given schema
;
otherwise returns false
.
Returns :ok
if the value
is a valid value against the given schema
;
otherwise returns an error tuple.
Returns :ok
if the value
is a valid value against the given schema
;
otherwise raises a Xema.ValidationError
.
Link to this section Types
t()
View Source
t() :: %Xema{refs: map(), schema: Xema.Schema.t()}
t() :: %Xema{refs: map(), schema: Xema.Schema.t()}
This struct contains the schema and references of the schema.
Link to this section Functions
new(data, opts \\ []) View Source
This function creates the schema from the given data
.
Possible options:
:loader
- a loader for remote schemas. This option will overwrite the loader from the config. See Configure a loader to how to define a loader.inline
- inlined all references in the schema. Default:true
.
Examples
Simple schema:
iex> schema = Xema.new :string
iex> Xema.valid? schema, "hello"
true
iex> Xema.valid? schema, 42
false
Schema:
iex> schema = Xema.new {:string, min_length: 3, max_length: 12}
iex> Xema.valid? schema, "hello"
true
iex> Xema.valid? schema, "hi"
false
Nested schemas:
iex> schema = Xema.new {:list, items: {:number, minimum: 2}}
iex> Xema.validate(schema, [2, 3, 4])
:ok
iex> Xema.valid?(schema, [2, 3, 4])
true
iex> Xema.validate(schema, [2, 3, 1])
{:error, %{items: [{2, %{value: 1, minimum: 2}}]}}
More examples can be found on page Usage.
source(xema) View Source
Returns the source for a given xema
. The output can differ from the input
if the schema contains references. To get the original source the schema
must be created with inline: false
.
Examples
iex> {:integer, minimum: 1} |> Xema.new() |> Xema.source()
{:integer, minimum: 1}
valid?(schema, value) View Source
Returns true
if the value
is a valid value against the given schema
;
otherwise returns false
.
validate(schema, value) View Source
Returns :ok
if the value
is a valid value against the given schema
;
otherwise returns an error tuple.
validate!(xema, value) View Source
Returns :ok
if the value
is a valid value against the given schema
;
otherwise raises a Xema.ValidationError
.