mdast-util-assert: Markdown Abstract Syntax Tree Assertion Utility
mdast-util-assert is a focused utility from the unified collective designed to validate the structure and properties of mdast (Markdown Abstract Syntax Tree) nodes. It provides functions to assert that a given object conforms to the expected shape of mdast nodes, including parent, literal, and void nodes. This package is particularly useful within API contexts where specific node types are expected, ensuring data integrity and preventing unexpected runtime errors. The current stable version is 5.0.0, which requires Node.js 16+ and is exclusively an ES Module (ESM). Release cadence generally follows semver, with major versions introducing breaking changes related to Node.js compatibility or module systems, while minor versions address bug fixes and documentation. Its key differentiator is its specialization for mdast nodes, building upon and re-exporting parts of the more general `unist-util-assert` for universal syntax trees.
Common errors
-
AssertionError: node should have a type: { children: [] }cause Attempting to assert a node object that is missing the mandatory `type` property.fixEnsure all mdast nodes passed to assertion functions have a `type` property, for example: `{ type: 'root', children: [] }`. -
AssertionError: parent should have children: { type: 'paragraph', value: 'foo' }cause Attempting to assert a `Parent` node (like `paragraph`) with a `value` property instead of `children`.fixCorrect the node structure. Parent nodes should have a `children` array property, while `Literal` nodes have a `value` property. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ...mdast-util-assert.js not supported.
cause Attempting to import the package using CommonJS `require()` syntax in a Node.js environment.fixThis package is ESM-only since v4. Migrate your code to use `import` statements (e.g., `import { assert } from 'mdast-util-assert';`) and ensure your project is configured for ESM.
Warnings
- breaking Version 5.0.0 and above require Node.js 16 or higher due to ecosystem-wide updates.
- breaking The package became ESM-only (ECMAScript Modules) starting from v4, and v5 solidifies this with an 'exports' map. CommonJS 'require()' is no longer supported directly.
- breaking Upgrading to version 5.0.0 may require updating your `@types/mdast` dependency to ensure compatibility with the latest type definitions.
- breaking Version 3.0.0 introduced significant TypeScript implications. If you or your dependents were using TypeScript but not explicitly expecting type enforcement, this could cause type errors.
Install
-
npm install mdast-util-assert -
yarn add mdast-util-assert -
pnpm add mdast-util-assert
Imports
- assert
const assert = require('mdast-util-assert').assertimport { assert } from 'mdast-util-assert' - parent
const parent = require('mdast-util-assert').parentimport { parent } from 'mdast-util-assert' - AssertionError
const AssertionError = require('mdast-util-assert').AssertionErrorimport { AssertionError } from 'mdast-util-assert' - literal
const literal = require('mdast-util-assert').literalimport { literal } from 'mdast-util-assert'
Quickstart
import { assert } from 'mdast-util-assert'
// Assert valid mdast nodes
assert({type: 'root', children: []})
assert({type: 'break'})
assert({type: 'listItem', checked: true, children: []})
// This will throw an AssertionError due to missing 'type' property
try {
assert({children: []})
} catch (error) {
console.error(error.message) // AssertionError: node should have a type: `{ children: [] }`
}
// This will throw an AssertionError due to incorrect property for a 'paragraph' node
try {
assert({type: 'paragraph', value: 'foo'})
} catch (error) {
console.error(error.message) // AssertionError: parent should have children: `{ type: 'paragraph', value: 'foo' }`
}