jddf v0.1.1 JDDF.Schema View Source

Represents a JSON Data Definition Format schema.

You can construct instances of this type yourself, or you can parse it from JSON-like data using from_json/1 or from_json!/1.

Not all invariants that JDDF schemas must adhere to are checked by from_json/1 or from_json!/1. To fully verify the correctness of a schema, use verify/1 or verify!/1.

Link to this section Summary

Functions

Construct a JDDF schema from parsed JSON data.

Construct a JDDF schema from parsed JSON data.

Ensure that the invariants of a JDDF schema are adhered to.

Ensure that the invariants of a JDDF schema are adhered to.

Link to this section Types

Link to this type

form()

View Source
form() ::
  {:empty}
  | {:ref, String.t()}
  | {:type, type()}
  | {:enum, MapSet.t(String.t())}
  | {:elements, JDDF.Schema.t()}
  | {:properties, mapping() | nil, mapping() | nil, boolean()}
  | {:values, JDDF.Schema.t()}
  | {:discriminator, String.t(), mapping()}
Link to this type

mapping()

View Source
mapping() :: %{optional(String.t()) => JDDF.Schema.t()}
Link to this type

t()

View Source
t() :: %JDDF.Schema{definitions: mapping() | nil, form: form()}
Link to this type

type()

View Source
type() ::
  :boolean
  | :float32
  | :float64
  | :int8
  | :uint8
  | :int16
  | :uint16
  | :int32
  | :uint32
  | :string
  | :timestamp

Link to this section Functions

Link to this function

from_json(json)

View Source
from_json(json :: String.t()) ::
  {:ok, Schema.t()} | {:error, JDDF.Schema.InvalidSchemaError.t()}

Construct a JDDF schema from parsed JSON data.

json should be an Elixir representation of JSON data. You should first parse the JSON before passing it into from_json/1. You can construct this data manually, or parse it using:

Most JSON implementations for Elixir are compatible with this function.

iex> JDDF.Schema.from_json(%{"type" => "uint32"})
{:ok, %JDDF.Schema{definitions: nil, form: {:type, :uint32}}}
iex> JDDF.Schema.from_json(%{"type" => "nonsense"})
{:error, %JDDF.Schema.InvalidSchemaError{message: "invalid type"}}
Link to this function

from_json!(json)

View Source
from_json!(json :: String.t()) :: Schema.t()

Construct a JDDF schema from parsed JSON data.

Similar to from_json/1, except it will unwrap the result and will raise in case of errors.

iex> JDDF.Schema.from_json!(%{"type" => "uint32"})
%JDDF.Schema{definitions: nil, form: {:type, :uint32}}
iex> JDDF.Schema.from_json!(%{"type" => "nonsense"})
** (JDDF.Schema.InvalidSchemaError) invalid type
Link to this function

verify(schema)

View Source
verify(schema :: JDDF.Schema.t()) ::
  {:ok, nil} | {:error, JDDF.Schema.InvalidSchemaError.t()}

Ensure that the invariants of a JDDF schema are adhered to.

iex> JDDF.Schema.verify(JDDF.Schema.from_json!(%{}))
{:ok, nil}

iex> schema = JDDF.Schema.from_json!(%{
...>   "properties" => %{ "a" => %{}},
...>   "optionalProperties" => %{ "a" => %{}},
...> })
iex> JDDF.Schema.verify(schema)
{:error, %JDDF.Schema.InvalidSchemaError{message: "properties and optionalProperties share key"}}
Link to this function

verify!(schema)

View Source
verify!(schema :: JDDF.Schema.t()) :: nil

Ensure that the invariants of a JDDF schema are adhered to.

Similar to verify/1, except it will unwrap the result and will raise in case of errors.

iex> JDDF.Schema.verify!(JDDF.Schema.from_json!(%{}))
nil

iex> schema = JDDF.Schema.from_json!(%{
...>   "properties" => %{ "a" => %{}},
...>   "optionalProperties" => %{ "a" => %{}},
...> })
iex> JDDF.Schema.verify!(schema)
** (JDDF.Schema.InvalidSchemaError) properties and optionalProperties share key