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 objects (maps); other types are silently skipped.
  if (value is! Map) return [];
  final violations = <SchemaViolation>[];
  for (final key in value.keys.cast<String>()) {
    // Skip keys covered by `properties`.
    if (declaredKeys.contains(key)) continue;
    // Skip keys matched by any `patternProperties` pattern.
    if (patternRegexes.any((r) => r.hasMatch(key))) continue;
    // Apply the sub-schema to the additional property value.
    final fieldPath = path.isEmpty ? key : '$path.$key';
    violations.addAll(schema.validate(value[key], fieldPath));
  }
  return violations;
}