Error messages
Each validation has one default message. The table below was produced by running every validation against a failing input with website/scripts/run.sh, so it matches the code. value is the field name placeholder: inside a schema it is the field key, after withName it is that name. A message or a message builder replaces the default.
Default messages
Section titled “Default messages”| Validation | Code | Default message |
|---|---|---|
l.string() | invalidType | value must be a string |
l.int() | invalidType | value must be an integer |
l.double() | invalidType | value must be a double |
l.num() | invalidType | value must be a number |
l.bool() | invalidType | value must be a bool |
l.list(...) | invalidType | value must be a list |
l.map() | invalidType | value must be a map |
l.schema({...}) | invalidType | value must be a map |
l.file() | invalidType | value must be a file |
l.nullValue() | invalidType | value must be null |
l.union([...]) | invalidUnion | value does not match any allowed option |
l.oneOf(['a', 'b']) | notOneOf | value must be one of: a, b |
.required() | required | value is required |
.custom(f) | custom | value does not pass custom validation |
.customWithSchema(f) | custom | value does not pass schema custom validation |
.min(8) on a string | tooShort | value must be at least 8 characters long |
.max(3) on a string | tooLong | value must not be more than 3 characters long |
.length(3) | invalidLength | value must be exactly 3 characters long |
.contains('@') | missingSubstring | value does not contain "@" |
.startsWith('+') | missingPrefix | value does not start with "+" |
.endsWith('.dart') | missingSuffix | value does not end with ".dart" |
.regex(RegExp(r'^\d+$')) | invalidPattern | value must match the pattern ^\d+$ |
.email() | invalidEmail | value must be a valid email address |
.uri() | invalidUri | value must be a valid uri |
.uri(allowedSchemes: ['https']) | invalidUri | value must be a valid uri. Allowed scheme is https |
.url() | invalidUrl | value must be a valid URL |
.url(allowedSchemes: ['http', 'https']) | invalidUrl | value must be a valid URL. Allowed schemes are http, https |
.ip() | invalidIp | value must be a valid IP address |
.ip(version: IpVersion.v4) | invalidIp | value must be a valid IPv4 address |
.uuid() | invalidUuid | value must be a valid uuid |
.cuid() | invalidCuid | value must be a valid cuid |
.cuid2() | invalidCuid2 | value must be a valid cuid2 |
.dateTime() | invalidDateTime | value must be a valid date |
.emoji() | invalidEmoji | value must be a valid emoji |
.min(18) on a number | tooSmall | value must be greater than or equal to 18 |
.max(100) on a number | tooBig | value must be less than or equal to 100 |
.finite() | notFinite | value must be a finite number |
.strict() unknown key | unrecognizedKey | role is not an allowed key |
map key validator | invalidKey | value has an invalid key "x" |
l.maxDepth exceeded | tooDeep | value is nested deeper than 1 levels |
fromJson threw | fromJsonFailed | value could not be converted |
Field names
Section titled “Field names”The name in a message is decided by where the validator runs:
| Where | Field name |
|---|---|
Validated directly, no withName |
value |
Validated directly, after .withName('x') |
x |
| A schema field | the field key, so email is required |
| An element of a list field | the list’s field name, so tags must be a string at path tags.1 |
| A value of a map field | the map’s field name |
| A key of a map field | key, unless the key validator has a withName |
| A nested schema that is not a map | the nested field’s key, so profile must be a map |
| Validated directly and not a map | value must be a map, or the schema’s withName |
Messages do not include the path. Read issue.errorPath or the key of the error map for that.
Messages for list and map input
Section titled “Messages for list and map input”Each failing element of a list reports at its index, and a map reports values at their key and keys as invalidKey issues at the map’s path:
l.list(l.string().required()).validate(['a', 1, null]).errors;// {1: [value must be a string], 2: [value is required]}
l.map(keyValidator: l.string().email().required(), valueValidator: l.int().required()) .validate({'not-an-email': 'x'}) .errors;// {: [value has an invalid key "not-an-email"], not-an-email: [value must be an integer]}Errors and error paths describes the error map in full.