Emanuel Hategan Emanuel Hategan - 1 month ago 6
Java Question

Is there a uuid validator annotation?

I can't find a

@UUID
(or similar) annotation for validating input parameters in a java web app.

I've looked so far in


  1. javax.validation.constraints

  2. org.hibernate.validator.constraints


Answer

yes, build it by yourself

@Target(ElementType.FIELD)
@Constraint(validatedBy={})
@Retention(RUNTIME)
@Pattern(regexp="^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$")
public @interface UUID {
    String message() default "{invalid.uuid}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}