JsonXema (json_xema v0.6.5)

View Source

A JSON Schema validator.

Summary

Types

The return type of a validation run.

t()

This struct contains the schema and references of the schema.

Functions

This function creates a new JsonXema from the given schema.

Converts %JsonXema{} to %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 Elixir.JsonXema.ValidationError. See validate3 for available options.

Types

result()

@type result() :: Xema.Validator.result()

The return type of a validation run.

schema()

@type schema() :: Xema.Schema.t()

t()

@type t() :: %JsonXema{refs: map(), schema: Xema.Schema.t()}

This struct contains the schema and references of the schema.

Functions

new(data, opts \\ [])

@spec new(
  boolean() | map(),
  keyword()
) :: t()

This function creates a new JsonXema from the given schema.

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

iex> ~s({"type": "string"})
...> |> Jason.decode!()
...> |> JsonXema.new()
%JsonXema{refs: %{}, schema: %Xema.Schema{type: :string}}

to_xema(json_xema)

@spec to_xema(t()) :: Xema.t()

Converts %JsonXema{} to %Xema{}.

valid?(schema, value)

@spec valid?(t() | schema(), any()) :: boolean()

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

validate(schema, value, opts \\ [])

@spec validate(t() | schema(), any(), keyword()) :: result()

Returns :ok if the value is a valid value against the given schema; otherwise returns an error tuple.

With the option :fail, you can define when the validation is aborted. This also influences how many error reasons are returned.

  • :immediately aborts the validation when the first validation fails.
  • :early (default) aborts on failed validations, but runs validations for all properties and items.
  • :finally aborts after all possible validations.

validate!(xema, value, opts \\ [])

@spec validate!(t() | schema(), any(), keyword()) :: :ok

Returns :ok if the value is a valid value against the given schema; otherwise raises a Elixir.JsonXema.ValidationError. See validate3 for available options.