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) {
  if (value is! List) return [];
  final violations = <SchemaViolation>[];
  if (minItems != null && value.length < minItems!) {
    violations.add(
      SchemaViolation(
        path: path,
        message: 'must have at least $minItems items',
      ),
    );
  }
  if (maxItems != null && value.length > maxItems!) {
    violations.add(
      SchemaViolation(
        path: path,
        message: 'must have at most $maxItems items',
      ),
    );
  }
  if (items != null) {
    // Apply the items schema only to elements at indices >= itemsStartIndex.
    // When prefixItems is absent, itemsStartIndex is 0 so all elements are
    // covered (backwards-compatible behaviour). When prefixItems is present,
    // itemsStartIndex equals the prefix length so only elements beyond the
    // prefix are validated by the items schema.
    for (var i = itemsStartIndex; i < value.length; i++) {
      violations.addAll(items!.validate(value[i], '$path[$i]'));
    }
  }
  return violations;
}