Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a uuid validator annotation?

Tags:

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
like image 612
Emanuel George Hategan Avatar asked May 19 '16 10:05

Emanuel George Hategan


People also ask

How do I check if a string is valid on a UUID?

So to check if a string is valid UUID we can use this regular expression, // Regular expression to check if string is a valid UUID const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi; This matches all the valid UUID since it checks for the above test cases.

Which annotations is used for validation?

The @Valid annotation applies validation rules on the provided object. The BindingResult interface contains the result of validation.

Can we create custom annotation for validation?

In this way, we can create different custom annotations for validation purposes. You can find the full source code here. It is easy to create and use custom annotations in Java. Java developers will be relieved of redundant code by using custom annotations.

What is Spring @validated annotation?

The @Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class.


1 Answers

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 {}; } 
like image 184
Jaiwo99 Avatar answered Nov 15 '22 11:11

Jaiwo99