asyncApplyValidator function

Future<ValidationResult> asyncApplyValidator (
  1. Validator validator,
  2. Map data,
  3. Angel app
)

Asynchronously apply a validator, running any AngelMatchers.

Implementation

Future<ValidationResult> asyncApplyValidator(
    Validator validator, Map data, Angel app) async {
  var result = validator.check(data);
  if (result.errors.isNotEmpty) return result;

  var errantKeys = <String>[], errors = <String>[];

  for (var key in result.data.keys) {
    var value = result.data[key];
    var description = StringDescription("'$key': expected ");

    for (var rule in validator.rules[key]) {
      if (rule is AngelMatcher) {
        var r = await rule.matchesWithAngel(value, key, result.data, {}, app);

        if (!r) {
          errors.add(rule.describe(description).toString().trim());
          errantKeys.add(key);
          break;
        }
      }
    }
  }

  var m = Map<String, dynamic>.from(result.data);
  for (var key in errantKeys) {
    m.remove(key);
  }

  return result.withData(m).withErrors(errors);
}