-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Validators is a Java library for Bean validation covering common validation scenarios without the baggage of additional third-party dependencies.
Use the @DateTime
constraint to validate that a sequence of characters matches at least one of the date patterns provided.
class TestBean {
@DateTime(patterns = "yyyy-MM-dd", message = "Invalid ISO date")
String isoDate;
public Date getParsedIsoDate() {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(isoDate);
} catch (ParseException e) {
// Exception will not occur if the bean instance has been validated successfully.
}
return null;
}
}
The @Expression
constraint should be used in scenarios where either multiple fields must be considered or where slightly more complicated logic is necessary to check for an individual field's validity. The annotation's value
attribute must contain a value Java EL expression that evaluates to a boolean value. The item being annotated (field or class) can be referenced in the expression string as 'self'. Values referenced in the expression must be available to the EL runtime, typically via public "getter" methods.
For example, it is a common requirement for users to provide a password confirmation when creating a new system account or when changing passwords. Using the @Expression
annotation, a simple check for equality can be performed and, if false
is the result, the 'newPasswordConfirmation' field will be identified as the offending value.
@Expression(value = "self.newPassword eq self.newPasswordConfirmation",
/* Check the constraint only if a value is provided. */
when = "self.newPasswordConfirmation ne null",
node = "newPasswordConfirmation",
message = "Password confirmation must match new password")
class PasswordBean {
@NotNull
private String newPassword;
@NotNull
private String newPasswordConfirmation;
public String getNewPassword() {
return newPassword;
}
public String getNewPasswordConfirmation() {
return newPasswordConfirmation;
}
...
}
The @JdbcStatement
constraint can be used when it is necessary to check a value or condition using SQL and a database. As with @Expression
, this annotation supports Java EL expressions in parameter values. The parameters given (optional) are bound to parameter markers in the SQL given in the value
attribute in the order in which they are specified. If no dataSourceLookup
value is given, the default data source will be retrieved, i.e., java:comp/DefaultDataSource
.
The constraint will fail if the query returns no results.
@JdbcStatement(value = "SELECT 1 FROM POSTAL_CODES WHERE CODE = ?",
/* Check the constraint only if a value is provided. */
when = "self.postalCode ne null",
dataSourceLookup = "java:comp/env/jdbc/myDataSource",
parameters = { "self.postalCode" },
node = "postalCode",
message = "Invalid postal code")
public class AddressBean {
String address;
String city;
String state;
@NotNull
String postalCode;
public String getPostalCode() {
return postalCode;
}
...
}