validate method

  1. @override
Future<void> validate([
  1. String? parent
])
override

Validates that there is some filesystem entity in parent that matches pattern and the child entry. This finds all entities in parent matching pattern, then passes each of their names to child provided in the constructor and validates the result. If exactly one succeeds, this is considered valid.

Implementation

@override
Future<void> validate([String? parent]) async {
  var inSandbox = parent == null;
  parent ??= sandbox;
  var matchingEntries = await Directory(parent)
      .list()
      .map((entry) =>
          entry is File ? entry.resolveSymbolicLinksSync() : entry.path)
      .where((entry) => matchesAll(pattern, p.basename(entry)))
      .toList();
  matchingEntries.sort();

  var location = inSandbox ? 'sandbox' : '"${prettyPath(parent)}"';
  if (matchingEntries.isEmpty) {
    fail('No entries found in $location matching $_patternDescription.');
  }

  var results = await Future.wait(matchingEntries
      .map((entry) {
        var basename = p.basename(entry);
        return runZonedGuarded(() {
          return Result.capture(Future.sync(() async {
            await _fn(basename).validate(parent);
            return basename;
          }));
        }, (_, __) {
          // Validate may produce multiple errors, but we ignore all but the first
          // to avoid cluttering the user with many different errors from many
          // different un-matched entries.
        });
      })
      .whereType<Future<Result<String>>>()
      .toList());

  var successes = results.where((result) => result.isValue).toList();
  if (successes.isEmpty) {
    await waitAndReportErrors(results.map((result) => result.asFuture));
  } else if (successes.length > 1) {
    fail('Multiple valid entries found in $location matching '
        '$_patternDescription:\n'
        '${bullet(successes.map((result) => result.asValue!.value))}');
  }
}