xema v0.6.1 Xema View Source

A schema validator inspired by JSON Schema

Link to this section Summary

Functions

Returns true if the value is a valid value against the given schema; otherwise returns false

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

Link to this type

t() View Source
t() :: %Xema{refs: any(), schema: any()}

Link to this section Functions

Link to this function

is_valid?(schema, value) View Source
is_valid?(Xema.t() | Xema.Schema.t(), any()) :: boolean()

This function is deprecated. Use valid? instead.

Returns true if the value is a valid value against the given schema; otherwise returns false.

This function creates the schema from the given data.

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.

Returns the source for a given xema.

Examples

iex> {:integer, minimum: 1} |> Xema.new() |> Xema.source()
{:integer, minimum: 1}
Link to this function

valid?(schema, value) View Source
valid?(Xema.t() | Xema.Schema.t(), any()) :: boolean()

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.

Link to this function

validate!(xema, value) View Source
validate!(Xema.t() | Xema.Schema.t(), any()) :: :ok

Returns :ok if the value is a valid value against the given schema; otherwise raises a Xema.ValidationError.