0

I'm using Javax annotations to validate a bunch of fields upon receiving a request in my Spring app. Some annotations are default and some are custom. I'd like to be able to specify the order in which each of the annotations are executed and error message is provided.

The following is a sample of my use of these annotations:

@Email(message = UserMessages.EMAIL_INVALID)
@UniqueEmail
private String email;

@NotEmpty(message = UserMessages.FIRSTNAME_NULL)
@Size(min = FirstnameLength_MIN, message = UserMessages.FIRSTNAME_TOO_SHORT)
@Size(max = FirstnameLength_MAX, message = UserMessages.FIRSTNAME_TOO_LONG)
private String firstName;

@NotEmpty(message = UserMessages.LASTNAME_NULL)
@Size(min = LastnameLength_MIN, message = UserMessages.LASTNAME_TOO_SHORT)
@Size(max = LastnameLength_MAX, message = UserMessages.LASTNAME_TOO_LONG)
private String lastName;

@NotEmpty(message = UserMessages.USERNAME_NULL)
@Size(min = UsernameLength_MIN, message = UserMessages.USERNAME_TOO_SHORT)
@Size(max = UsernameLength_MAX, message = UserMessages.USERNAME_TOO_LONG)
@NewUsername
private String username;

@NotEmpty(message = UserMessages.PASSWORD_NULL)
@Size(min = PasswordLength_MIN, message = UserMessages.PASSWORD_TOO_SHORT)
@Size(max = PasswordLength_MAX, message = UserMessages.PASSWORD_TOO_LONG)
private String password;

In perfect case scenario I'd like for each annotation to be validated in sequential order and not proceed any further.

I've had a look around and saw that 'groups' can be specified to control the order in which annotations are validated, however my impression is that the order can only be applied to custom annotations that I've created interfaces and validators for. Please correct me if I'm wrong.

Many thanks!

curiousdev
  • 626
  • 8
  • 24
  • 1
    Possible duplicate of [Control validation annotations order?](https://stackoverflow.com/questions/5571231/control-validation-annotations-order) – jannis Jun 22 '19 at 15:11
  • I doubt it. I'm using @Valid tag to execute validation therefore never calling validator.validate method myself. Is there a way to override? – curiousdev Jun 22 '19 at 16:00

0 Answers0