2.3.1 Declaring validation rules
For the Taco class, you want to ensure that the name property isn’t empty or null and that the list of selected ingredients has at least one item. The following listing shows an updated Taco class that uses @NotNull and @Size to declare those validation rules.
Listing 2.11 Adding validation to the Taco domain class
package tacos;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Data;
@Data
public class Taco {
@NotNull
@Size(min=5, message="Name must be at least 5 characters long")
private String name;
@NotNull
@Size(min=1, message="You must choose at least 1 ingredient")
private List<Ingredient> ingredients;
}You’ll notice that in addition to requiring that the name property isn’t null, you declare that it should have a value that’s at least five characters in length
When it comes to declaring validation on submitted taco orders, you must apply annotations to the TacoOrder class. For the address properties, you want to be sure that the user doesn’t leave any of the fields blank. For that, you’ll use the @NotBlank annotation.
Validation of the payment fields, however, is a bit more exotic. You need to ensure not only that the ccNumber property isn’t empty but also that it contains a value that could be a valid credit card number. The ccExpiration property must conform to a format of MM/YY (two-digit month and year), and the ccCVV property needs to be a three-digit number. To achieve this kind of validation, you need to use a few other JavaBean Validation API annotations and borrow a validation annotation from the Hibernate Validator collection of annotations. The following listing shows the changes needed to validate the TacoOrder class. Listing 2.12 Validating order fields
package tacos;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.CreditCardNumber;
import java.util.List;
import java.util.ArrayList;
import lombok.Data;
@Data
public class TacoOrder {
@NotBlank(message="Delivery name is required")
private String deliveryName;
@NotBlank(message="Street is required")
private String deliveryStreet;
@NotBlank(message="City is required")
private String deliveryCity;
@NotBlank(message="State is required")
private String deliveryState;
@NotBlank(message="Zip code is required")
private String deliveryZip;
@CreditCardNumber(message="Not a valid credit card number")
private String ccNumber;
@Pattern(regexp="^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$",
message="Must be formatted MM/YY")
private String ccExpiration;
@Digits(integer=3, fraction=0, message="Invalid CVV")
private String ccCVV;
private List<Taco> tacos = new ArrayList<>();
public void addTaco(Taco taco) {
this.tacos.add(taco);
}
}As you can see, the ccNumber property is annotated with @CreditCardNumber. This annotation declares that the property’s value must be a valid credit card number that passes the Luhn algorithm check(https://en.wikipedia.org/wiki/Luhn_algorithm). This prevents user mistakes and deliberately bad data but doesn’t guarantee that the credit card number is actually assigned to an account or that the account can be used for charging.
Unfortunately, there’s no ready-made annotation for validating the MM/YY format of the ccExpiration property. I’ve applied the @Pattern annotation, providing it with a regular expression that ensures that the property value adheres to the desired format. If you’re wondering how to decipher the regular expression, I encourage you to check out the many online regular expression guides, including http://www.regularexpressions.info/. Regular expression syntax is a dark art and certainly outside the scope of this book
Finally, we annotate the ccCVV property with @Digits to ensure that the value contains exactly three numeric digits.
All of the validation annotations include a message attribute that defines the message you’ll display to the user if the information they enter doesn’t meet the requirements of the declared validation rules.
