validateProject function

List<Diagnostic> validateProject(
  1. Map<String, Object?> documents, {
  2. ValidateOptions options = const ValidateOptions(),
  3. SiteManifest? manifest,
})

Checks several documents of one project (a site's pages): each against the others' screens and node ids. Keys are file names; diagnostics carry them in Diagnostic.file.

Implementation

List<Diagnostic> validateProject(Map<String, Object?> documents,
    {ValidateOptions options = const ValidateOptions(), SiteManifest? manifest}) {
  final screens = <String>{...options.screens};
  final owner = <String, String>{};
  final out = <Diagnostic>[];
  // Component definitions: checked as a library first (types, file names,
  // cycles); every document's calls are then checked against them.
  final (library, componentProblems) = _componentLibrary(documents, options.components);
  out.addAll(componentProblems);
  final projectVariables = {...options.variables, for (final v in manifest?.variables ?? const <Variable>[]) v.name};
  if (manifest != null) out.addAll(_projectVariableCollisions(documents, manifest));
  for (final e in documents.entries) {
    // Node ids are per document: a page's actions and conditions name its
    // own nodes, a component's are its own. Only canvas ids are site-wide
    // (navigate and openModal name them), so only they must not repeat.
    if (readMap(e.value)?.containsKey(kWidgetKey) ?? false) continue;
    final nodes = readMap(readMap(e.value)?['nodes']) ?? const {};
    for (final id in nodes.keys) {
      if (readMap(nodes[id])?['type'] != 'canvas') continue;
      final before = owner[id];
      if (before != null) {
        out.add(Diagnostic(Codes.duplicateId, pointer('/nodes', id),
            'Canvas id "$id" is also a canvas of $before: navigate and openModal name a screen by its canvas id, so a link to it '
            'reaches one of them. Give each canvas of the site its own id (other node ids are per document and may repeat).',
            nodeId: id, file: e.key));
      }
      owner[id] = e.key;
      screens.add(id);
    }
  }
  for (final e in documents.entries) {
    final o = ValidateOptions(
        screens: screens,
        variables: projectVariables,
        formulas: options.formulas,
        breakpoints: options.breakpoints,
        components: library,
        theme: options.theme ?? manifest?.theme);
    out.addAll(validateDocument(e.value, options: o).map((d) => d.withFile(e.key)));
  }
  return out;
}