xema v0.2.0 Xema View Source
A schema validator inspired by JSON Schema
Link to this section Summary
Types
The available schema keywords
The available type notations
The Xema base struct contains the meta data of a schema
Functions
This function defines the schemas
Link to this section Types
Link to this type
schema_keywords()
View Source
schema_keywords() :: :additional_items | :additional_properties | :all_of | :any_of | :dependencies | :enum | :exclusive_maximum | :exclusive_minimum | :items | :keys | :max_items | :max_length | :max_properties | :maximum | :min_items | :min_length | :min_properties | :minimum | :multiple_of | :not | :one_of | :pattern | :pattern_properties | :properties | :required | :unique_items
The available schema keywords.
Link to this type
schema_types()
View Source
schema_types() :: :any | :boolean | :float | :integer | :list | :map | nil | :number | :string
The available type notations.
Link to this type
t()
View Source
t() :: %Xema{content: Xema.Schema.t(), description: String.t() | nil, id: String.t() | nil, keywords: term(), schema: String.t() | nil, title: String.t() | nil}
The Xema base struct contains the meta data of a schema.
id
a unique identifier.schema
declares the used schema.title
of the schema.description
of the schema.type
contains the specification of the schema.
Link to this section Functions
Link to this function
new(type, keywords \\ [])
View Source
new(schema_types() | tuple(), keyword()) :: Xema.t()
This function defines the schemas.
The first argument sets the type
of the schema. The second arguments
contains the ‘keywords’ of the schema.
Parameters
- type: type of the schema.
- opts: keywords of the schema.
Examples
iex> Xema.new :string, min_length: 3, max_length: 12
%Xema{
content: %Xema.Schema{
max_length: 12,
min_length: 3,
type: :string,
as: :string
}
}
For nested schemas you can use {:type, opts: ...}
like here.
Examples
iex> import Xema, only: [is_valid?: 2, validate: 2]
Xema
iex> schema = Xema.new :list, items: {:number, minimum: 2}
%Xema{
content: %Xema.Schema{
type: :list,
as: :list,
items: %Xema.Schema{
type: :number,
as: :number,
minimum: 2
}
}
}
iex> validate(schema, [2, 3, 4])
:ok
iex> is_valid?(schema, [2, 3, 4])
true
iex> validate(schema, [2, 3, 1])
{:error, [%{
at: 2,
error: %{value: 1, minimum: 2}
}]}
Link to this function
validate(schema, value)
View Source
validate(Xema.t() | Xema.Schema.t(), any()) :: Xema.Validator.result()