ValidationResult
Every validation returns a ValidationResult<T>: a ValidationSuccess<T> holding the typed output, or a ValidationFailure<T> holding the issues. The error views are defined on the base class and are empty on success, so you can read them without matching first. Results and issues explains how to use them.
Members
Section titled “Members”| Member | Type | On success | On failure |
|---|---|---|---|
isValid |
bool |
true |
false |
issues |
List<ValidationIssue> |
[] |
every issue, in the order found |
messages |
List<String> |
[] |
each issue’s message, in issue order |
errors |
Map<String, List<String>> |
{} |
messages grouped by error path |
getError(path) |
String? |
null |
the first message at exactly path, or null |
getErrors(path) |
List<String> |
[] |
every message at exactly path |
toString() |
String |
ValidationSuccess(data: ...) |
ValidationFailure(errors: {...}), with the empty path shown as (root) |
ValidationSuccess<T> adds data, the output of type T. ValidationFailure<T> adds input, the raw value that was validated.
path is an error path: dot-joined field keys, map keys and list indices, such as address.city or items.1.id. The validated value itself has the empty path '', which is also where object-level errors from .custom() on a schema live. getError never throws; a path with no issue returns null.
Where T comes from
Section titled “Where T comes from”| Call | T |
|---|---|
l.string().validate(x) |
String? |
l.string().required().validate(x) |
String |
l.list(l.int().required()).validate(x) |
List<int>? |
schema.validate(x) |
Map<String, Object?>?, or non-nullable after .required() |
schema.validateSchema(x, fromJson: User.fromJson) |
User |
$UserValidate(x) |
User |
Example
Section titled “Example”final result = l.string().email().required().withName('email').validate('bad');
switch (result) { case ValidationSuccess(:final data): print('valid: $data'); case ValidationFailure(:final messages): print('invalid: $messages');}
print(result.isValid);print(result.errors);print(result.getError(''));Printed output of the three result shapes:
ValidationSuccess(data: [email protected])ValidationFailure(errors: {(root): [value must be a valid email address]})ValidationFailure(errors: {email: [email must be a valid email address]})On a success, every error view is empty:
{}[]null[]