ConstraintViolation class final

One broken rule: which constraint failed, where (the property path), on which value, and the finished message.

Applications receive violations in two places: in a list from BeanValidator.validateObject and the other validate... methods when they check an object themselves, and in ConstraintViolationException.constraintViolations when a @Validated() bean rejects a call. Unlike a FieldError, the message is already text (interpolated, not a list of message codes); use it for logs, or map propertyPath to your own response.

// Report each problem of an imported row.
for (final v in validator.validateObject(form)) {
  log.warning('${v.propertyPath}: ${v.message}'); // name: must not be blank
}

Spring (Java):

for (ConstraintViolation<SignupForm> v : validator.validate(form)) {
  log.warn(v.getPropertyPath() + ": " + v.getMessage());
}

Constructors

ConstraintViolation({required String propertyPath, required Constraint constraint, required Object? invalidValue, required String message, Object? rootBean, Object? leafBean, String? messageTemplate})
A violation of constraint at propertyPath; the validator creates them, applications rarely do (in tests, say).
const

Properties

constraint → Constraint
The constraint that failed; its name (NotBlank) tells the kind of problem and its attributes the limits.
final
hashCode → int
The hash code for this object.
no setterinherited
invalidValue → Object?
The value that broke the constraint; the object itself for a class-level constraint.
final
leafBean → Object?
The object that holds the invalid property, such as the Address for address.city; the validated object itself for a class-level constraint. Jakarta's getLeafBean().
final
message → String
The finished message, placeholders filled in, such as size must be between 0 and 20.
final
messageTemplate → String?
The message before interpolation, such as size must be between {min} and {max}.
final
propertyPath → String
Where the invalid value is, from the validated object: name, address.city, items[0].sku, names[1].<list element>; empty for a class-level constraint. In method validation it starts with the method name: register.email, register.<return value>.
final
rootBean → Object?
The object validation started from (the bean, for method validation); null for BeanValidator.validateValue, where no instance exists, as in Jakarta.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

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