【问题标题】:Micronaut custom validation constraints, write custom message depending on the valueMicronaut 自定义验证约束,根据值编写自定义消息
【发布时间】:2020-03-19 11:52:54
【问题描述】:

Micronaut 中是否有一种方法可以定义自定义消息以在 Micronaut 中进行验证。

这是我的代码:

这是注解类:

@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()
}

这是我的验证工厂:

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 powehr.pandore.mn.validation.CheckDecimal

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>
    }
}

而我就是这样使用我的约束:

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

我希望能够在每种违反约束的情况下编写特定的消息。这里的上下文似乎没有提供很多可能性。

有没有办法在 checkDecimalValidator() 中定义特定的消息验证?

【问题讨论】:

    标签: java groovy micronaut


    【解决方案1】:

    不,没有。对此建模的验证 API 的设计使每个验证都有自己的注释。

    您应该能够在所有情况下使用现有注释。

    minimum = -1.0d 等价于@DecimalMin("1.0")

    maximum = 97.0d 等价于@DecimalMax("97.0")

    integerLength = 2, decimalLength = 4 等价于@Digits(integer = 2, fraction = 4)

    【讨论】:

    • 我知道现有的 Min Max 注释,感谢您的建议。我只是想说明这个案例。我对 String 有更复杂的验证。因此,对于我需要的每种字符串特定格式的情况,我必须定义一个特定的注释,希望能够传递一个枚举值并根据约束定义消息或根据传递的值更改消息。
    • @Nolyurn 注解中的值能够在消息中解析。因此,如果您有@SomeMin(enum = ABC),您可以在注释message() default "{enum}" 中定义,然后在您的messages.properties 中定义ABC=...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 2018-01-29
    • 1970-01-01
    • 2012-12-12
    • 2011-11-27
    相关资源
    最近更新 更多