xema v0.7.0 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 defxema/2
supports the construction of a schema. After that
the schema is available as a function.
Example
defmodule Schema do
use Xema
@pos integer(minimum: 0)
@neg integer(maximum: 0)
defxema user,
map(
properties: %{
name: string(min_length: 1),
age: @pos
}
)
defxema nums,
map(
properties: %{
pos: list(items: @pos),
neg: list(items: @neg)
}
)
end
The module Schema
can now be used like this.
true = Schema.valid?(:user, %{name: "John", age: 21})
false = Schema.valid?(:user, %{name: "", age: 21})
:ok = Schema.validate(:user, %{name: "John", age: 21})
{:error, reason} = Schema.validate(:user, %{name: "", age: 21})
true = Schema.valid?(:nums, %{pos: [1, 2, 3]})
false = Schema.valid?(:nums, %{neg: [1, 2, 3]})
%Xema{} = Schema.user()
%Xema{} = Schema.nums()
Link to this section Summary
Functions
This function creates the schema from the given data
Returns the source for a given xema
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()}
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.
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
.
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
.