Skip to content

Instantly share code, notes, and snippets.

@robertoschwald
Last active August 10, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertoschwald/ce23c8c23ebd5b93fc3f60c150e35cea to your computer and use it in GitHub Desktop.
Save robertoschwald/ce23c8c23ebd5b93fc3f60c150e35cea to your computer and use it in GitHub Desktop.
Hibernate ApacheEmailValidator. Use instead of @pattern or @Email to let Apache Commons EmailValidator do the validation.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
import org.apache.commons.lang.StringUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* Check a string if it's a valid eMail using {@see org.apache.commons.validator.routines.EmailValidator}
* You need commons-validator-1.4 or later as a dependency.
*
* @author Robert Oschwald
*/
public class ApacheEmailValidator implements ConstraintValidator<EmailPattern, String> {
/**
* Initialize the validator in preparation for isValid calls.
* The constraint annotation for a given constraint declaration
* is passed.
* <p/>
* This method is guaranteed to be called before any use of this instance for
* validation.
*
* @param annotation annotation instance for a given constraint declaration
*/
public void initialize(EmailPattern annotation) {
}
/**
* {@inheritDoc} check if given string is a valid mail.
*
* @see javax.validation.ConstraintValidator#isValid(java.lang.Object,
* javax.validation.ConstraintValidatorContext)
*/
public boolean isValid(final String value, final ConstraintValidatorContext context) {
if (StringUtils.isEmpty(value)) {
return true;
}
return org.apache.commons.validator.routines.EmailValidator.getInstance().isValid(value);
}
}
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Email pattern check using Apache Commons EmailValidator
* as the Hibernate EmailValidator is limited.
*
* The string has to be a well-formed email address.
*
* @author Robert Oschwald
*/
@Documented
@Constraint(validatedBy = ApacheEmailValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface EmailPattern {
String message() default "{org.hibernate.validator.constraints.Email.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment