Throw javax.validation.UnexpectedTypeException: HV000030 使用@NotEmpty 验证 Scala 的 Map 时出现异常

Throw javax.validation.UnexpectedTypeException: HV000030 exception when validating Scala's Map with @NotEmpty

当我尝试验证 Scala 的映射时,无论是可变的还是不可变的,我都遇到了这样的异常:

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'scala.collection.mutable.Map<java.lang.String, java.lang.String>'. Check configuration for 'corporationSecrets'
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:163) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:116) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateComposingConstraints(ConstraintTree.java:398) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:98) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:621) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:584) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:528) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:496) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:461) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:411) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:208) ~[hibernate-validator-5.3.6.Final.jar:5.3.6.Final]
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:102) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.validation.DataBinder.validate(DataBinder.java:877) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:274) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:330) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
    ... 29 common frames omitted

我的代码很简单,喜欢

@NotEmpty
val corporationSecrets : scala.collection.mutable.Map[String, String] = new scala.collection.mutable.HashMap[String, String]

如何验证 scala 的映射以检查它是否为空?

如果你想检查地图集合是否为空,你可以这样做:if(mapVariable.isEmpty() || mapVariable == null){...}

或写traversable shouldBe emptyjavaMap should not be empty如第二个link(link到API)。查看 this or this 了解更多信息。

编辑

尝试将@Valid 添加到集合中。

这是来自 Hibernate Validator Reference 的示例。

public class Car { 
@NotNull 
@Valid 
private List<Person> passengers = new ArrayList<Person>(); 
}

这是标准的 JSR-303 行为。请参阅 spec 的第 3.1.3 节。

所以你要明白的是,在 Hibernate Validator 中,对于给定的约束,你要为不同的类型注册约束验证器。

通常,对于@Size 约束,我们将为String、Collection、Map、数组注册约束验证器...

这里的问题是 Scala 类型没有实现经典的 Java 接口,所以我们找不到它们的约束验证器。这就是你得到异常的原因:HV 识别约束但找不到约束验证器将其应用于你的 Scala Map。

据我从代码中可以看出,https://github.com/bean-validation-scala/bean-validation-scala 将是获得 Scala 的 BV 和 HV 支持的方法。它将验证委托给原始 HV 实现,因此您应该具有与 Java.

所做的非常相似的行为

我不认为它已经过时,因为请记住,在我们开始 BV 2.0 和 HV 6.0 的工作之前,BV/HV 项目有一段时间没有太大变化。关于 BV 2.0 和 HV 6.0 支持(我们有新的限制等),ping 原作者可能是个好主意。