HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.util.Optional<java.lang.String>'

huangapple go评论61阅读模式
英文:

HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.util.Optional<java.lang.String>'

问题

你好,我正在尝试在YAML中进行验证:

- name: campo
  in: query
  schema:
    type: string
    pattern: '[S|N]' #pattern
    maxLength: 1

使用Maven Spring Boot和委托。在RestController上,我添加了@Validated注解,我可以验证required=true的字段,但是如果字段不是必需的,根据架构设计,参数将是Optional<String>。对于这个java.util.Optional,尝试测试和验证会引发以下错误:

{
  "timestamp": "2020-10-21T07:50:18.651+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "HV000030: 找不到约束'javax.validation.constraints.Size'的验证器,验证类型为'java.util.Optional<java.lang.String>'。请检查'metodo.campo'的配置",
  "path": "/%7BbasePath%7D/microservicio/metodo"
}

是否有解决此问题的方法,以便正确处理Optional<String>的验证?如果可以在YAML + 验证(@Valid和@Validated)中完成,我们不想在Java上编写所有验证代码。

编辑:@Size和其他验证是使用OpenAPI版本3.0.3从YAML生成的。

英文:

Hello guys I'm trying to get the validation on the YAML

- name: campo
        in: query
        schema:
          type: string
          pattern: &#39;[S|N]&#39; #pattern
          maxLength: 1

Using maven spring boot and delegate. On the RestController I added annotation @Validated and I'm able to validate required=true fields, but if fields are not require, as per architecture design, parameter will be Optional&lt;String&gt;. For this java.util.Optional, trying to test and validate, throw title error:

{
  &quot;timestamp&quot;: &quot;2020-10-21T07:50:18.651+0000&quot;,
  &quot;status&quot;: 500,
  &quot;error&quot;: &quot;Internal Server Error&quot;,
  &quot;message&quot;: &quot;HV000030: No validator could be found for constraint &#39;javax.validation.constraints.Size&#39; validating type &#39;java.util.Optional&lt;java.lang.String&gt;&#39;. Check configuration for &#39;metodo.campo&#39;&quot;,
  &quot;path&quot;: &quot;/%7BbasePath%7D/microservicio/metodo&quot;
}

¿Anyway to solve this issue to have this Optional&lt;String&gt; validation proccessed correctly? We don't want to write all code for validation on Java if can be done on YAML + validation (@Valid and @Validated).

EDIT: The validation @Size and others is generated from YAML with open api version 3.0.3

答案1

得分: 5

你应该写:Optional&lt;@Size String&gt;(这将检查可选项内部的字符串大小),而不是@Size Optional&lt;String&gt;(这将检查可选项的大小,这是没有意义的)。

参见Hibernate Validator:使用 java.util.Optional


package org.hibernate.validator.referenceguide.chapter02.containerelement.optional;

public class Car {

    private Optional&lt;@MinTowingCapacity(1000) Integer&gt; towingCapacity = Optional.empty();

    //...

}
英文:

You should write: Optional&lt;@Size String&gt; (which would check the size of the String inside the optional) instead of @Size Optional&lt;String&gt; (which is supposed to check the size of the optional, which is meaningless).

See Hibernate Validator: With java.util.Optional:



package org.hibernate.validator.referenceguide.chapter02.containerelement.optional;

public class Car {

    private Optional&lt;@MinTowingCapacity(1000) Integer&gt; towingCapacity = Optional.empty();

    //...

}

答案2

得分: 0

较新版本的 Jakarta Bean Validation 2.0 自2019年8月起新增对 Optional 的验证支持。您可以在它们的变更日志摘要 2. 新内容 中找到这些信息:

> 对 java.util.Optional 的支持

截至今年9月,唯一获得认证的实现是 Hibernate Validator 6.0.17.Final

考虑进行升级,否则您将不得不省略大小验证并手动执行它。

英文:

The newer version Jakarta Bean Validation 2.0 newly supports Optional validation as of August 2019. You can find this information out at their changelog summary 2. What’s new:

> Support for java.util.Optional

The only certified implementation, is Hibernate Validator 6.0.17.Final as of September this year.

Consider an upgrade, ohterwise you would have to omit the size validation and perform it manually.

huangapple
  • 本文由 发表于 2020年10月21日 22:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64465887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定