Categories: java, groovy, micronaut

Micronaut custom validation constraints, write custom message depending on the value

1 answer

Is there a way in Micronaut to define custom message for validation in Micronaut.

Here is my code :

This is the annotation class :

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Constraint(validatedBy = []) @Documented @interface CheckDecimal {     String message() default "Invalid Decimal ({validatedValue})";      double minimum()      double maximum()      int integerLength()      int decimalLength() } 

This is my validation factory :

import io.micronaut.context.annotation.Factory import io.micronaut.core.annotation.AnnotationValue import io.micronaut.validation.validator.constraints.ConstraintValidator import io.micronaut.validation.validator.constraints.ConstraintValidatorContext import  import javax.inject.Singleton  @Factory class ValidatorFactory {      @Singleton     ConstraintValidator<CheckDecimal, Double> checkDecimalValidator() {         return { Double value,                  AnnotationValue<CheckDecimal> annotation,                  ConstraintValidatorContext context ->              final parameters = annotation.convertibleValues.asMap()             final keySet = parameters.keySet()              if(value == null) {                 return true             }              if(keySet.contains('minimum')) {                 double minimumValidator = parameters.get('minimum')                 if(value < minimumValidator) {                     // Define specific constraint message here                     return false                 }             }              if(keySet.contains('maximum')) {                 double maximumValidator = parameters.get('maximum')                 if(value > maximumValidator) {                     // Define specific constraint message here                     return false                 }             }               if(keySet.contains('integerLength')) {                 final integerLengthValidator = parameters.get('integerLength')                 final integerLength = value.toString().split(/\./)[0].length()                  if(integerLength > integerLengthValidator) {                     // Define specific constraint message here                     return false                 }             }              if(keySet.contains('decimalLength')) {                 final decimalLengthValidator = parameters.get('decimalLength')                 final decimalLength = value.toString().split(/\./)[1].length()                  if(decimalLength > decimalLengthValidator) {                     // Define specific constraint message here                     return false                 }             }              return true         } as ConstraintValidator<CheckDecimal, Double>     } } 

And i use my constraint like that :

 @CheckDecimal(minimum = -1.0d, maximum = 97.0d, integerLength = 2, decimalLength = 4) 

I'd like to be able to write specific message in each case of constraint violation. The context doesn't seem to offer many possibility here.

Is there a way to define specific message validation in checkDecimalValidator() ?

All answers to this question, which has the identifier 60756681

The best answer:

No there isn't. The validation API this is modeled after is designed so that each validation has its own annotation.

You should have be able to use the existing annotations for all cases though.

minimum = -1.0d is equivalent to @DecimalMin("1.0")

maximum = 97.0d is equivalent to @DecimalMax("97.0")

integerLength = 2, decimalLength = 4 is equivalent to @Digits(integer = 2, fraction = 4)

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data