Unist Node Assertion Utility

4.0.0 · active · verified Sun Apr 19

unist-util-assert is a utility package within the unified/syntax-tree ecosystem designed to validate unist (Universal Syntax Tree) nodes. It provides functions to assert that given `tree` or `node` structures conform to the unist specification, including checks for parent, literal, and void nodes, and their children. The current stable version is 4.0.0. This package maintains an active release cadence, reflecting ongoing development and compatibility updates within the broader unified collective. Key differentiators include its tight integration with the unist specification, robust type checking (it ships with TypeScript types), and its focused approach to node validation, contrasting with more general-purpose assertion libraries or similar utilities for specific node types like mdast or hast.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `assert`, `parent`, and `_void` to validate unist nodes, including examples of valid and invalid node structures that trigger `AssertionError`.

import {_void, assert, parent} from 'unist-util-assert'

// Assert a valid root node
assert({type: 'root', children: []})
// Assert a valid void node
assert({type: 'break'})
// Assert a valid element node
assert({type: 'element', properties: {}, children: []})

// Example of intentionally invalid assertions to demonstrate error types
try {
  assert({children: []})
} catch (error) {
  console.error(error.message) // AssertionError: node should have a type: `{ children: [] }`
}

try {
  parent({type: 'break'})
} catch (error) {
  console.error(error.message) // AssertionError: parent should have `children`: `{ type: 'break' }`
}

try {
  _void({type: 'text', value: 'Alpha'})
} catch (error) {
  console.error(error.message) // AssertionError: void should not have `value`: `{ type: 'text', value: 'Alpha' }`
}

view raw JSON →