Introduction
An attribute validator class is used to validate the value to be assigned to an attribute.
This wiki page aims to show hot to implement and deploy a new attribute validator class.
Implementation
An attribute validator class must extend AbstractValidator class.
Conditions must be evaluated into the method doValidate(...).
In case of an attribute value not valid an InvalidAttrValueException exception must be thrown.
A sample validator has been provided below.
package org.apache.syncope.core.persistence.validation.attrvalue; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.syncope.core.persistence.beans.AbstractSchema; import org.apache.syncope.core.persistence.beans.AbstractAttrValue; public class EmailAddressValidator extends AbstractValidator { private static final Pattern EMAIL_PATTERN = Pattern.compile( "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$", Pattern.CASE_INSENSITIVE); public EmailAddressValidator(final AbstractSchema schema) { super(schema); } @Override protected void doValidate(final AbstractAttrValue attributeValue) throws InvalidAttrValueException { Matcher matcher = EMAIL_PATTERN.matcher( (CharSequence) attributeValue.getValue()); if (!matcher.matches()) { throw new InvalidAttrValueException(attributeValue); } } }
Deploy
An attribute validator class can be deployed:
- at project definition time
by adding own implementation into the overlay project, before to build Syncope. - at run-time
by including into the container classpath own implementation (container must be re-started to reload the classpath).