Semantic validation
Rules beyond the schema. Template reference in the DocStudio developer reference.
Public from docstudio/core: validateTemplate, validateTemplateSemantics and the related types. Decision record: ADR 0013. Structural validation is described in template-schema.md.
Entry points
validateTemplate(input, { limits?, validator?, registry?, formatters?, allowProps? })runs structural validation first. If the input is not a structurally valid template it returns theSCHEMA_VIOLATIONdiagnostics and no template. Otherwise it runs semantic validation and returns{ valid, template, diagnostics }.validis true when no diagnostic has severityerror; warnings do not invalidate.templateis the typed template when valid.validateTemplateSemantics(template, options)runs only the semantic pass over an already typed template. It never throws, traverses iteratively, and caps diagnostics atmaxDiagnostics.
Checks
| Code | Severity | Condition | Pointer |
|---|---|---|---|
DUPLICATE_ID |
error | A node id repeats across body, header, footer and component slots, or an import alias repeats | The second occurrence (.../id); the message names the first |
UNKNOWN_COMPONENT |
error | A node type has no registry definition, or a component node's ref matches no import |
.../type or .../ref |
UNUSED_IMPORT |
warning | An import is never referenced | /imports/n |
INVALID_EXPRESSION, UNKNOWN_FUNCTION, ARITY, INVALID_PATH, UNSAFE_PATH, LIMIT_EXCEEDED |
error | The M3 parser rejects an expression, forwarded unchanged | The offending part of the expression, such as /body/0/when/fn |
UNKNOWN_FORMATTER |
warning | A literal format name is absent from the supplied formatter registry (only when formatters is given) |
The args of the call |
SCOPE_VIOLATION |
error | A path uses a scope that does not exist where it appears | The path expression |
FRAME_OUTSIDE_FIXED |
error | frame on a node that is not a direct child of a fixed node |
.../frame |
FRAME_MISSING |
warning | A direct child of fixed has no frame |
The node |
INVALID_TABLE |
error or warning | Duplicate column ids, percent widths above 100 in total, a footer row spanning more columns than the table has (errors); a footer cell with neither text nor expression (warning) | The column, columns or footer row |
UNKNOWN_STYLE |
warning | styleRef names a style the template does not define (the application configuration may define it) |
.../styleRef |
Expression sites checked
Text content segments, when and styleWhen[].when on any node, repeater and table source, qr and barcode value, table column cell and visible, and footer cell expr. Component props are open objects and are checked by composite expansion (M9).
Scope rules
The check follows the nesting at the point where an expression appears. depth is the number of enclosing repeaters and table rows.
| Where | $index |
$rows |
$count |
$parent chain |
|---|---|---|---|---|
Top level, header, footer, empty content, sources |
no | no | no | none |
| Repeater children | yes | yes | yes | up to depth |
| Table cell | yes | yes | yes | up to depth |
Table column visible, footer cell |
no | yes | yes | up to depth |
The of part of an aggregate |
yes | yes | yes | one more than around it |
$root is always valid. $props is valid only with allowProps: true, and a slot node only with allowSlots: true; component expansion sets both when it validates a component template. A path that climbs more $parent steps than the depth reports SCOPE_VIOLATION.
Performance
Median on macOS (Node 24): 259 nodes 0.66 ms, 1,555 nodes 4.1 ms, 9,331 nodes 26 ms, about 2.8 microseconds per node. See validation/artifacts/benchmarks/core-m7-semantic.json.