简体   繁体   English

原始布尔值的 javax.validation.constraints?

[英]javax.validation.constraints for primitive boolean?

Small question on javax.validation.constraints and how to validate the primitive boolean (not Boolean)关于 javax.validation.constraints 的小问题以及如何验证原始布尔值(不是布尔值)

Currently, I am using @Min(1), @Size(min = 1, max = 10), @NotBlank, etc, to validate my primitive int, and String object.目前,我正在使用 @Min(1)、@Size(min = 1, max = 10)、@NotBlank 等来验证我的原始 int 和 String 对象。 It is doing the job fine, very happy.它做得很好,很高兴。

How to validate the same for a primitive boolean please?请如何验证原始布尔值的相同内容? Meaning, I have a boolean that I would like to be a must input in the JSON request.意思是,我有一个布尔值,我想是在JSON请求必须输入。

I understand if I do Boolean, I can combine it wit some kind of @NotNull validation.我明白如果我做布尔值,我可以将它与某种 @NotNull 验证结合起来。 How to do the same with the primitive boolean please?请如何对原始布尔值做同样的事情?

I would like the client to must input this field.我希望客户必须输入此字段。 And the value of the field must be either true or false.并且该字段的值必须为真或假。

I do not want to default it to false or true if the field is not present.如果该字段不存在,我不想将其默认为 false 或 true。

This is a valid example这是一个有效的例子

{
    "field": false
}

Here, there is a typo in the field, hence, the field field is not present, it is not valid.此处,字段中存在拼写错误,因此字段字段不存在,无效。

{
    "fielddwdw": false
}

Here, the value of the field is "true", the string, hence invalid.此处,该字段的值为“true”,即字符串,因此无效。

{
    "field": "true"
}

Here, the value is neither true or false, invalid这里的值不是真也不是假,无效

{
    "field": foo
}

I was hoping for some kind of我希望有某种

    @MagicalBooleanValidationAnnoation
    private boolean field;

But just would like to achieve this use case.但只是想实现这个用例。

Thank you谢谢

You can't do the same as a Boolean with @NotNull annotation for a primitive boolean , because the primitive boolean has only 2 possible values, and none of them represent the lack of a value.对于原始boolean ,您不能使用带有@NotNull注释的Boolean做同样的事情,因为原始boolean只有 2 个可能的值,并且没有一个表示缺少值。

If you want a 3-state value, with states true , false , and missing , you must use Boolean .如果您想要一个三态值,状态为truefalsemissing ,则必须使用Boolean

Or you can use an enum with 3 values.或者您可以使用具有 3 个值的enum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.validation.constraints中的转义序列无效 - Invalid escape sequencein javax.validation.constraints 来自 javax.validation.constraints 的注释不起作用 - Annotations from javax.validation.constraints not working 关注点重复javax.validation.constraints和javax.persistence.Column - Duplication of concerns javax.validation.constraints and javax.persistence.Column 以编程方式“关闭”bean验证(javax.validation.constraints) - “Turn off” bean validation programmatically (javax.validation.constraints) 结合自定义 ConstraintValidator 的 javax.validation.constraints 注释 - Annotations from javax.validation.constraints in combination with custom ConstraintValidator 来自javax.validation.constraints的注释不起作用(忽略) - Annotations from javax.validation.constraints not working (ignored) Java 中的 json 验证器 - 使用 javax.validation.constraints - json validator in Java - using javax.validation.constraints 如何为 `javax.validation.constraints` 注释添加自定义验证器? - how add custom validator for `javax.validation.constraints` annotation? Swagger软件包javax.validation.constraints不存在 - Swagger package javax.validation.constraints does not exist 是否可以将 spring boot @Value 与 javax.validation.constraints 结合使用? - Is it possible to combine spring boot @Value with javax.validation.constraints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM