I can't find a @UUID
(or similar) annotation for validating input parameters in a java web app.
I've looked so far in
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.
The @Valid annotation applies validation rules on the provided object. The BindingResult interface contains the result of 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.
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.
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 {}; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With