Elastic Path Commerce Development

Java Bean Validation

Java Bean Validation

Java Bean Validation, also known as JSR-303, is an annotation based validation specification for validating fields on objects. Java Bean Validation uses flexible annotations, which can be overridden or extended through the Java code or in an XML file, to apply validation constraints.

Elastic Path uses the Apache BVal implementation of JSR-303 in our Customer and Attribute domain objects to validate incoming information.

Apache BVal provides:

  • Spring support
  • Customizable constraints
  • Extensible validation
  • Constraint grouping

The Elastic Path platform uses BVal in the following locations:

  • Customer Fields in Customer.java
  • Addresses Fields in Address.java
  • Attributes Fields (Short-Text, Long-Text, and Short-Text multivalue) in AttributeValueWithType.java

To get started with Java Bean Validation, take a look at the following documentation:

This section gives an overview on how Elastic Path uses Java Bean Validation.

Validating an Entity

To validate entities in Elastic Path:

  1. Get the validator instance
  2. Pass the object to the validator through the validate(object) method

For example, this is how you validate a customer:

Validator validator = beanFactory.getBean("validator");
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);

The validate(object) method returns a set of constraint violations. If this set is empty, the object passes validation. Otherwise, you can pass the constraint violations set to loggers or UI widgets to display validation errors.

Constraint Annotations

Constraints specify the criteria the validation framework uses to determine if an object is valid. In Bean Validation, constraints are primarily provided in the form of annotations.

In Elastic Path, annotations are placed directly above an object's interfaces, classes, fields, and getter methods as shown:

@RegisteredCustomerPasswordNotBlankWithSize(min = Customer.MINIMUM_PASSWORD_LENGTH, max = GlobalConstants.SHORT_TEXT_MAX_LENGTH)
public interface Customer extends Entity, UserDetails, DatabaseLastModifiedDate {

...


	/**
	 * Get the username.
	 *
	 * @return the username
	 */
	@Override
	@NotNull
	@NotBlank
	@Size(max = GlobalConstants.SHORT_TEXT_MAX_LENGTH)
	@CustomerUsernameCheck
	String getUsername();

Common Annotations

The following table lists some common bean validation annotations in the Elastic Path code:

Annotation Purpose
@NotNull Verifies the value is not null.
@Email Verifies the email address is valid.
@Size Verifies a value's length is between the minimum and maximum value
@Valid Verifies the value passed validation

In addition, there are custom Elastic Path Bean Validation annotations listed in the following table:

Annotation Purpose
@NotBlank Verifies a field is not empty and containing only whitespace.
@EpEmail Verifies an email has a "." in the domain name. For example: Oliver@elasticpath.com
@AttributeRequired Verifies an entity's required attributes are defined.
@ValidCountry Verifies a country and sub-country are valid.
@RegisteredCustomerPasswordNotBlankWithSize

Verifies a password is valid.

@BooleanConstraint Verifies a String value is strictly true or false.
@DecimalConstraint Verifies a value can be parsed as a decimal number.
@IntegerConstraint Verifies a value can be parsed as an integer.
@ISO8601DateConstraint Verifies a date conforms to the ISO 8601 date standard.
@ISO8601DateTimeConstraint Verifies a date and time conforms to the ISO 8601 Date/Time standard.
@LengthConstraint Verifies a String length is within a given range.
@LongTextValueSize Verifies an attribute value is of the LongText type and that the length is within a given range.
@MultiOptionConstraint Verifies a comma-seperated String of options is a part of a given set of valid options.
@ShortTextMultiValuesElementSize Verifies an attribute value is of the MultiValue type and that the length is within a given range.
@ShortTextValueSize Verifies an attribute value is of the ShortText type and that the length is within a given range.
@SingleOptionConstraint Verifies a String contains one of a set of valid options.

Custom Annotations

In addition to the out-of-the-box annotations, you can create custom constraints to suit your needs. For more information on how to create custom constraints, see Creating a Custom Constraint.

Validation Groups

Bean Validation supports group validation, which allows you to group constraints into subsets that validate input at different times.

By default, constraints do not validate in a particular order. However, you can specify a validation order by using validation groups. For example, you can create two groups to validate a country / sub-country relationship. In the first group, you verify that the country exists. In the second group, you verify the sub-country exists within the country.

XML Constraints

In Bean Validation, constraints can be specified in an XML file. This allows you to overwrite or extend an entity's validation without modifying the entity's source code. For more information on how to use XML constraints, refer to Applying Constraints with XML.