Validated class

An annotation that asks for validation, optionally with validation groups.

Where you put it decides what is checked:

  • On a bean class (@Service(), @Component(), ...): every call to its methods is checked. Constraints on a parameter check the argument, constraints on the method check the returned value, and @Valid() checks the constraints inside an argument or result object. A violation throws a ConstraintViolationException before the method runs (or after it, for the result). This is how a service guards its inputs whoever calls it; Spring Boot's auto-configuration provides the MethodValidationPostProcessor that does it.
  • On a method of such a class: only picks the groups for that method. On its own, without @Validated() on the class, it turns nothing on.
  • On a controller's @RequestBody or @RequestPart parameter: like @Valid(), the object is checked and a violation is answered with a 400, but with the groups in value.
  • On a @ConfigurationProperties class: the bound values are checked at startup, and an invalid one stops the application with a report.

A ConstraintViolationException from a service is not turned into a 400 by the web starter: catch it in an @ExceptionHandler if a client can cause it.

// Callers cannot register an empty or malformed address, and the
// service never returns a blank id.
@Service()
@Validated()
class Registration {
  @NotBlank()
  String register(@Email() @NotBlank() String email) => 'id-$email';
}

registration.register(''); // throws ConstraintViolationException

Spring (Java):

@Service
@Validated
class Registration {
  @NotBlank String register(@Email @NotBlank String email) { ... }
}

Constructors

Validated([List<Type> value = const []])
Asks for validation with the groups value (none: the Default group).
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
value → List<Type>
The validation groups to check, such as [OnUpdate]; empty means only the constraints without groups (the Default group).
final

Methods

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

Operators

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