Skip to content

Instantly share code, notes, and snippets.

@takeouchida
Created October 3, 2015 04:05
Show Gist options
  • Save takeouchida/b520471123bca4f71c4d to your computer and use it in GitHub Desktop.
Save takeouchida/b520471123bca4f71c4d to your computer and use it in GitHub Desktop.
An annotation class for validating mac addresses with Hibernate Validator.
package hibernatevalidator.constraints;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.Pattern;
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.*;
@Pattern(regexp = "^([0-9a-fA-F]{2})(:[0-9a-fA-F]{2}){5}$")
@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
@ReportAsSingleViolation
public @interface Macaddr {
String message() default "must be macaddr";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@jindalsumit1991
Copy link

Great work, thanks for this.
It would be great if couple of lines are added about the usage. That will be even more helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment