Nasty.Utils.Validator (Nasty v0.3.0)

View Source

AST validation utilities for ensuring structural consistency.

Validates that AST nodes conform to expected schemas and that the tree structure is internally consistent.

Examples

iex> Nasty.Utils.Validator.validate(document)
{:ok, document}

iex> Nasty.Utils.Validator.validate(malformed_node)
{:error, "Invalid node structure: ..."}

Summary

Functions

Checks if an AST node is valid.

Validates an AST node and all its descendants.

Validates an AST node, raising on error.

Validates language consistency throughout the tree.

Validates that spans are consistent throughout the tree.

Functions

valid?(node)

@spec valid?(term()) :: boolean()

Checks if an AST node is valid.

Examples

iex> Nasty.Utils.Validator.valid?(document)
true

iex> Nasty.Utils.Validator.valid?(malformed_node)
false

validate(node)

@spec validate(term()) :: {:ok, term()} | {:error, String.t()}

Validates an AST node and all its descendants.

Returns {:ok, node} if valid, or {:error, reason} if invalid.

Examples

iex> Nasty.Utils.Validator.validate(document)
{:ok, document}

iex> Nasty.Utils.Validator.validate(invalid_node)
{:error, "Document language must be an atom"}

validate!(node)

@spec validate!(term()) :: term()

Validates an AST node, raising on error.

Examples

iex> Nasty.Utils.Validator.validate!(document)
document

iex> Nasty.Utils.Validator.validate!(invalid_node)
** (RuntimeError) Invalid AST: Document language must be an atom

validate_language(node)

@spec validate_language(term()) :: :ok | {:error, String.t()}

Validates language consistency throughout the tree.

Ensures all nodes have the same language marker.

Examples

iex> Nasty.Utils.Validator.validate_language(document)
:ok

validate_spans(node)

@spec validate_spans(term()) :: :ok | {:error, String.t()}

Validates that spans are consistent throughout the tree.

Checks that:

  • Parent spans contain all child spans
  • Spans don't overlap incorrectly
  • Byte offsets match positions

Examples

iex> Nasty.Utils.Validator.validate_spans(document)
:ok