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 section Functions
is_valid?(schema, value)
View Source
is_valid?(Xema.t() | Xema.Schema.t(), any()) :: boolean()
is_valid?(Xema.t() | Xema.Schema.t(), any()) :: boolean()
Returns true
if the value
is a valid value against the given schema
;
otherwise returns false
.
new(schema)
View Source
new(Xema.Schema.t() | Xema.Schema.type() | tuple() | atom() | keyword()) ::
Xema.t()
new(Xema.Schema.t() | Xema.Schema.type() | tuple() | atom() | keyword()) :: Xema.t()
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.
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
valid?(Xema.t() | Xema.Schema.t(), any()) :: boolean()
valid?(Xema.t() | Xema.Schema.t(), any()) :: boolean()
Returns true
if the value
is a valid value against the given schema
;
otherwise returns false
.
validate(schema, value)
View Source
validate(Xema.t() | Xema.Schema.t(), any()) :: Xema.Validator.result()
validate(Xema.t() | Xema.Schema.t(), any()) :: Xema.Validator.result()
Returns :ok
if the value
is a valid value against the given schema
;
otherwise returns an error tuple.
validate!(xema, value)
View Source
validate!(Xema.t() | Xema.Schema.t(), any()) :: :ok
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
.