Skip to content

Instantly share code, notes, and snippets.

@php-coder
Created January 15, 2012 17:09
Show Gist options
  • Save php-coder/1616466 to your computer and use it in GitHub Desktop.
Save php-coder/1616466 to your computer and use it in GitHub Desktop.
Custom @Email annotation which requires top-level domain
package ru.mystamps.web.validation.jsr303;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.Pattern;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Workaround implementation for Hibernate's {@code @Email} annotation.
*
* To require top-level domain I compose {@code @Email} annotation with
* {@code @Pattern} where latter just add more strict checking.
*
* @see <a href="http://stackoverflow.com/q/4459474">Question at StackOverflow</a>
* @see <a href="https://hibernate.onjira.com/browse/HVAL-43">Issue in Hibernate's bugtracker</a>
* @author Slava Semushin <slava.semushin@gmail.com>
* @since 10.01.2012
*/
@org.hibernate.validator.constraints.Email
@Pattern(regexp = ".+@.+\\..+")
@ReportAsSingleViolation
@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface Email {
String message()
default "{ru.mystamps.web.validation.jsr303.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