validate method
- dynamic value,
- 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! String) return [];
final violations = <SchemaViolation>[];
final len = value.characters.length;
if (minLength != null && len < minLength!) {
violations.add(
SchemaViolation(
path: path,
message: 'must have at least $minLength characters',
),
);
}
if (maxLength != null && len > maxLength!) {
violations.add(
SchemaViolation(
path: path,
message: 'must have at most $maxLength characters',
),
);
}
if (pattern != null) {
// Per JSON Schema spec §6.3.3, patterns are not implicitly anchored —
// the pattern only needs to match somewhere within the string.
if (!pattern!.hasMatch(value)) {
violations.add(
SchemaViolation(
path: path,
message: 'must match pattern ${pattern!.pattern}',
),
);
}
}
return violations;
}