validate method Null safety
- ValidationContext context,
- dynamic input
Validates the input
value.
Subclasses override this method to provide validation behavior.
input
is the value being validated. If the value is invalid, the reason
is added to context
via ValidationContext.addError.
Additional information about the validation event and the attribute being evaluated
is available in context
.
in context
.
This method is not run when input
is null.
The type of input
will have already been type-checked prior to executing this method.
Implementation
void validate(ValidationContext context, dynamic input) {
switch (type!) {
case ValidateType.absent:
{}
break;
case ValidateType.present:
{}
break;
case ValidateType.comparison:
{
final expressions = context.state as List<ValidationExpression>;
for (final expr in expressions) {
expr.compare(context, input);
}
}
break;
case ValidateType.regex:
{
final regex = context.state as RegExp;
if (!regex.hasMatch(input as String)) {
context.addError("does not match pattern ${regex.pattern}");
}
}
break;
case ValidateType.oneOf:
{
final options = context.state as List<dynamic>;
if (options.every((v) => input != v)) {
context.addError(
"must be one of: ${options.map((v) => "'$v'").join(",")}.",
);
}
}
break;
case ValidateType.length:
{
final expressions = context.state as List<ValidationExpression>;
for (final expr in expressions) {
expr.compare(context, (input as String).length);
}
}
break;
}
}