validate method

ValidationResult validate(
  1. UiNode tree
)

Validate a tree against this schema (FR-002). Returns a ValidationResult with a list of every encountered error so the agent can fix them all in one retry rather than fixing one-at-a-time.

Implementation

ValidationResult validate(UiNode tree) {
  final errors = <ValidationError>[];

  // Empty tree edge case (spec Edge Cases): a `root` node with no children
  // and no props is the empty-tree case the spec calls out.
  if (tree.children.isEmpty && tree.props.isEmpty && tree.type == 'root') {
    errors.add(
      const ValidationError(
        ValidationErrorKind.emptyTree,
        'Tree contains no renderable content; emit at least one child node.',
      ),
    );
  }

  _validateNode(tree, errors);

  // Cap overflow check (FR-002 acceptance 2).
  final capError = capOverflowError(tree);
  if (capError != null) errors.add(capError);

  if (errors.isNotEmpty) {
    return ValidationResult.invalid(errors);
  }
  return const ValidationResult.valid();
}