Now, let's move on to create the RegistrationPayload class itself. Here is how it looks:
...
public class RegistrationPayload {
@Size(min = 2, max = 50, message = "Username must be between 2 and 50 characters")
@NotNull
private String username;
@Email(message = "Email address should be valid")
@Size(max = 100, message = "Email address must not be more than 100 characters")
@NotNull
private String emailAddress;
@Size(min = 6, max = 30, message = "Password must be between 6 and 30 characters")
@NotNull
private String password;
// getters and setters
}
As you can see, this RegistrationPayload class has three fields and the constraints we apply to them match those we added on the frontend. One difference is that the @Size annotation considers a null value to be valid. That's why we have to put the @NotNull annotation for each field too.
We will also need to add the following test methods in RegistrationPayloadTests to cover every constraint applied on each field:
- validate_payloadWithInvalidEmail_shouldFail()
- validate_payloadWithEmailAddressLongerThan100_shouldFail()
- validate_payloadWithUsernameShorterThan2_shouldFail()
- validate_payloadWithUsernameLongerThan50_shouldFail()
- validate_payloadWithPasswordShorterThan6_shouldFail()
- validate_payloadWithPasswordLongerThan30_shouldFail()
We will not go through each test method here. If you're interested in the implementation of these methods, you can find them in the commit history on GitHub.