Skip to main content
Version: Canary 🚧

Guards API

Defensive Architecture

The guards module provides a set of assertion functions to enforce the structural integrity of your document. You use these inside the measure or draw functions of your components to ensure they are being used in the correct context.

Critical Behavior

If a guard fails, it will panic with a helpful error message, stopping the compilation immediately. Use guards only for requirements that are non-negotiable for your component's logic.

Hierarchy Guards​

These functions check the Path of the current component to ensure it is nested correctly.

assert-inside​

Asserts that the component is nested within at least one of the specified ancestors.

guards.assert-inside(ctx, ..ancestors)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.
..ancestorsstrRequiredA list of component kinds (names) allowed as ancestors.

assert-not-inside​

Asserts that the component is not nested within any of the specified ancestors.

guards.assert-not-inside(ctx, ..ancestors)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.
..ancestorsstrRequiredA list of forbidden ancestor component kinds.

assert-direct-parent​

Asserts that the immediate parent matches one of the specified kinds. This is stricter than assert-inside.

guards.assert-direct-parent(ctx, ..parents)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.
..parentsstrRequiredA list of allowed direct parent component kinds.

assert-root​

Asserts that the component is at the root of the Loom tree (it has no Loom parents).

guards.assert-root(ctx)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.

assert-max-depth​

Asserts that the current nesting depth does not exceed a limit.

guards.assert-max-depth(ctx, max)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.
maxintRequiredThe maximum allowable nesting depth integer.

Context Guards​

These functions validate the data present in the ctx dictionary.

assert-has-key​

Asserts that a specific key exists in the context.

guards.assert-has-key(ctx, key, msg: none)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.
keystrRequiredThe dictionary key to check for existence.
msgstrnoneAn optional custom error message to display if the assertion fails.

assert-value​

Asserts that a context key exists and its value matches one of the allowed options.

guards.assert-value(ctx, key, ..allowed)
ParameterTypeDefaultDescription
ctxdictionaryRequiredThe current context object.
keystrRequiredThe context key to retrieve and check.
..allowedanyRequiredA list of valid values that the context key is allowed to hold.