validate method

  1. @override
List<SchemaViolation> validate(
  1. dynamic value,
  2. String path
)
override

Validates value at path and returns every violation found.

path is a dot-notation string identifying the location of value within the document (e.g. 'address.city', '' for the root). Violations use path as their SchemaViolation.path.

Implementation

@override
List<SchemaViolation> validate(dynamic value, String path) {
  // Applies only to arrays; other types are silently skipped.
  if (value is! List) return [];

  // Count elements that satisfy the sub-schema. Violations from the
  // sub-schema are used only as a non-match signal and are never forwarded
  // to the caller.
  var matchCount = 0;
  for (final element in value) {
    if (itemSchema.validate(element, path).isEmpty) {
      matchCount++;
    }
  }

  final violations = <SchemaViolation>[];

  if (matchCount < minContains) {
    violations.add(
      SchemaViolation(
        path: path,
        message:
            'must contain at least $minContains matching item(s) '
            '(found $matchCount)',
      ),
    );
  }

  if (maxContains != null && matchCount > maxContains!) {
    violations.add(
      SchemaViolation(
        path: path,
        message:
            'must contain at most $maxContains matching item(s) '
            '(found $matchCount)',
      ),
    );
  }

  return violations;
}