Matches class

A constraint that requires a string to match the regular expression regexp as a whole.

Use it for formats with a fixed shape: postal codes, ISBNs, product codes. The whole value must match, as with Java's Matcher.matches(), so ^ and $ are not needed. null passes. Since the default message shows the expression, give a message users understand, or a Pattern.<object>.<field> key in messages.properties.

Its error code is Jakarta's Pattern; the class has another name because Pattern is a Dart core type. The arguments of a field error are [field, flags, regexp], as in Spring. Only for strings: on another type the generator fails the build and the validator throws an UnexpectedTypeException; a malformed expression throws a ConstraintDeclarationException.

// A five-digit postal code, and a code of letters in any case.
@Introspected()
class Address {
  @Matches(r'\d{5}', message: 'must be 5 digits')
  String? zip;

  @Matches('[a-z]+', flags: [PatternFlag.caseInsensitive])
  String code = 'abc';
}

Spring (Java):

class Address {
  @Pattern(regexp = "\\d{5}", message = "must be 5 digits")
  private String zip;

  @Pattern(regexp = "[a-z]+", flags = Pattern.Flag.CASE_INSENSITIVE)
  private String code;
}
Inheritance

Constructors

Matches(String regexp, {List<PatternFlag> flags = const [], String? message, List<Type> groups = const []})
The constraint for strings that match regexp with flags; message replaces must match "{regexp}", and groups limits it to those groups.
const

Properties

attributes → Map<String, Object?>
The settings of the constraint by name, such as {'max': 20, 'min': 0}: they fill the {max} placeholders of messages and, sorted by name, become the arguments {1}, {2}, ... of field errors. Empty by default; override it when a custom constraint has settings.
no setteroverride
defaultMessage → String
The text of a violation when message is not set, with the same placeholders, such as size must be between {min} and {max}.
no setteroverride
flags → List<PatternFlag>
Options for matching regexp, such as PatternFlag.caseInsensitive; none by default.
final
groups → List<Type>
The validation groups the constraint belongs to, such as [OnUpdate]; it is only checked when one of them is asked for. Empty means Default.
finalinherited
hashCode → int
The hash code for this object.
no setterinherited
message → String?
The text of a violation instead of defaultMessage; null keeps the default.
finalinherited
name → String
The error code of a violation, such as NotBlank: the start of the message codes (NotBlank.signup.name, ..., NotBlank) and the Jakarta name of the constraint. Custom constraints usually return their class name.
no setteroverride
regexp → String
The regular expression, in Dart's RegExp syntax, that the whole value must match.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

appliesTo(List<Type> requested) → bool
Whether the constraint is checked when a validation asks for the requested groups: when one of its groups is requested.
inherited
checkParameters() → void
Throws a ConstraintDeclarationException when the constraint's own settings are invalid: Size(min: -1), min > max, a malformed regular expression. Does nothing by default.
override
interpolate(String template) → String
template with its {attribute} placeholders replaced by the values of attributes; other placeholders and ${...} expressions stay, and the escapes \{, \}, \$, \\ become the literal characters.
inherited
interpolatedMessage() → String
message, or defaultMessage, with the {attribute} placeholders filled in; message keys and ${...} expressions stay, since only the validator can resolve them.
inherited
isValid(Object? value) → bool
Whether value satisfies the constraint; the method a custom constraint implements.
override
isValidWithContext(Object? value, ConstraintValidatorContext context) → bool
Whether value satisfies the constraint, with access to the validation context; the method BeanValidator calls. By default it returns isValid.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
supports(Object value) → bool
Whether the constraint can check a value of this type at all; value is never null. @Size supports strings, collections and maps, not an int.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited