Quillon.Schema.Validator (Quillon v0.1.0)

Copy Markdown View Source

Validates Quillon documents against a schema.

The validator checks:

  • Node types exist in the schema
  • Children match the content expression
  • Required attributes are present
  • Marks are allowed on the node type
  • Marks don't conflict (via excludes)
  • Mark types exist in the schema

Examples

iex> doc = Quillon.document([Quillon.paragraph("Hello")])
iex> {:ok, ^doc} = Quillon.Schema.Validator.validate(doc)

iex> bad_doc = {:unknown, %{}, []}
iex> {:error, errors} = Quillon.Schema.Validator.validate(bad_doc)
iex> [%{type: :unknown_type}] = errors

Summary

Types

A validation error

Error types

Functions

Validate a document against the default schema.

Validate a document against a schema.

Validate a document, raising on error.

Validate a document against a schema, raising on error.

Types

error()

@type error() :: %{path: [non_neg_integer()], type: error_type(), message: String.t()}

A validation error

error_type()

@type error_type() ::
  :unknown_type
  | :invalid_content
  | :missing_attr
  | :mark_not_allowed
  | :mark_conflict
  | :unknown_mark

Error types

Functions

validate(node)

@spec validate(tuple()) :: {:ok, tuple()} | {:error, [error()]}

Validate a document against the default schema.

Examples

iex> doc = Quillon.document([Quillon.paragraph("Hello")])
iex> {:ok, ^doc} = Quillon.Schema.Validator.validate(doc)

validate(node, schema)

@spec validate(tuple(), Quillon.Schema.t()) :: {:ok, tuple()} | {:error, [error()]}

Validate a document against a schema.

Returns {:ok, node} if valid, or {:error, errors} with a list of all errors.

Examples

iex> schema = Quillon.Schema.default()
iex> doc = Quillon.document([Quillon.paragraph("Hello")])
iex> {:ok, ^doc} = Quillon.Schema.Validator.validate(doc, schema)

iex> schema = Quillon.Schema.default()
iex> bad_doc = {:unknown, %{}, []}
iex> {:error, [%{type: :unknown_type}]} = Quillon.Schema.Validator.validate(bad_doc, schema)

validate!(node)

@spec validate!(tuple()) :: tuple() | no_return()

Validate a document, raising on error.

Examples

iex> doc = Quillon.document([Quillon.paragraph("Hello")])
iex> ^doc = Quillon.Schema.Validator.validate!(doc)

validate!(node, schema)

@spec validate!(tuple(), Quillon.Schema.t()) :: tuple() | no_return()

Validate a document against a schema, raising on error.

Examples

iex> schema = Quillon.Schema.default()
iex> doc = Quillon.document([Quillon.paragraph("Hello")])
iex> ^doc = Quillon.Schema.Validator.validate!(doc, schema)