Registry / testing / mdast-util-assert

mdast-util-assert

JSON →
library5.0.0jsnpmunverified

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.

npm install mdast-util-assert
INSTALL
IMPORT
SIG · MDAST-UTIL-ASSERT
M
mdast-util-assert
testingjavascriptv5.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

assert
import { assert } from 'mdast-util-assert'
const assert = require('mdast-util-assert').assert
ESM-only since v4; CommonJS 'require' is not supported.
parent
import { parent } from 'mdast-util-assert'
const parent = require('mdast-util-assert').parent
ESM-only since v4. This specific assertion validates parent nodes and their children.
AssertionError
import { AssertionError } from 'mdast-util-assert'
const AssertionError = require('mdast-util-assert').AssertionError
ESM-only since v4. This error class is thrown when an assertion fails. It's re-exported from `unist-util-assert`.
literal
import { literal } from 'mdast-util-assert'
const literal = require('mdast-util-assert').literal
ESM-only since v4. Use this to assert that a node is an mdast Literal node.

Demonstrates how to use `assert` to validate mdast nodes, showing successful and failing assertions with error handling.

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' }` }
Debug
Known issues
breakingVersion 5.0.0 and above require Node.js 16 or higher due to ecosystem-wide updates.
fix
Upgrade your Node.js environment to version 16 or later to use `mdast-util-assert@5`.
affects: >=5.0.0
breakingThe package became ESM-only (ECMAScript Modules) starting from v4, and v5 solidifies this with an 'exports' map. CommonJS 'require()' is no longer supported directly.
fix
Migrate your project to use ES Modules (import/export syntax). If you are unable to migrate, you might need to pin to a version below 4.0.0 or use dynamic `import()` for ESM-only packages.
affects: >=4.0.0
breakingUpgrading to version 5.0.0 may require updating your `@types/mdast` dependency to ensure compatibility with the latest type definitions.
fix
Update `@types/mdast` to the latest compatible version (`npm install @types/mdast@latest`).
affects: >=5.0.0
breakingVersion 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.
fix
Review and update your TypeScript configurations and type usage to align with the type definitions introduced in v3.
affects: >=3.0.0 <4.0.0
Errors
Common errors & fixes
AssertionError: node should have a type: { children: [] }
Attempting to assert a node object that is missing the mandatory `type` property.
fix
Ensure 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' }
Attempting to assert a `Parent` node (like `paragraph`) with a `value` property instead of `children`.
fix
Correct 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.
Attempting to import the package using CommonJS `require()` syntax in a Node.js environment.
fix
This 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.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies
unist-util-assertrequiredRe-exports assertion functions like `_void`, `wrap`, and `AssertionError` from this package for core assertion logic.
Agent activity
2 hits · last 30 days
node
2
Resources
mdast-util-assert — npm install mdast-util-assert · libregistry