strictEditCheck function
An EditCheck that runs validateDocument on the nodes an op touched
(with options: formulas, the project's screens, variables, components
and theme). Its
diagnostics keep the validator's codes, paths and suggestions; an error
rejects the op.
Implementation
EditCheck strictEditCheck([ValidateOptions options = const ValidateOptions()]) => (input) {
final doc = input.document;
final ids = <String>{...input.nodes.keys};
final around = <String>{};
for (final id in ids) {
final p = doc.parentOf(id);
if (p != null) around.add(p);
for (final c in doc[id]?.childIds ?? const <String>[]) {
if (doc[c] != null) around.add(c);
}
}
// The rest of the document as stubs: what states, actions and
// navigation name exists.
final nodes = <String, Object?>{
for (final n in doc.nodes.values)
if (!ids.contains(n.id) && !around.contains(n.id)) n.id: {'type': n.type},
for (final id in around)
if (!ids.contains(id)) id: doc[id]!.toJson()..remove('id'),
for (final e in input.nodes.entries) e.key: e.value,
};
final mini = <String, Object?>{
'schema': kSchemaVersion,
'roots': [for (final r in doc.roots) if (nodes.containsKey(r)) r],
'nodes': nodes,
...{
for (final e in doc.extra.entries)
if (documentKeys.contains(e.key)) e.key: e.value,
},
};
final o = ValidateOptions(
screens: {...options.screens, ...input.screens, for (final n in doc.nodes.values) if (n.type == 'canvas') n.id},
variables: options.variables,
formulas: options.formulas,
breakpoints: options.breakpoints,
components: options.components,
theme: options.theme,
level: options.level,
);
final out = <EditDiagnostic>[];
for (final d in validateDocument(mini, options: o)) {
if (_structural.contains(d.code.name)) continue;
final id = d.nodeId ?? _nodeOf(d.path);
// A formula of a variable the op declared.
final variable = id == null ? _formulaVariableOf(d.path, doc) : null;
if (variable == null && (id == null || !ids.contains(id))) continue;
if (variable != null && !(input.variables[variable.scope]?.contains(variable.name) ?? false)) continue;
final later = input.incomplete && streamReferenceCodes.contains(d.code.name);
final message = variable == null ? d.message : 'Variable "${variable.name}": ${d.message}';
out.add(EditDiagnostic(
op: input.op,
code: d.code.name,
error: d.isError && !later,
path: d.path,
message: later ? '$message (Accepted while the UI streams in: it must exist when the stream ends.)' : message,
nodeId: id,
suggestion: d.suggestion,
));
}
return out;
};