在Hibernate Validator 4.x中是否有(或第三方实现)交叉字段验证的实现?如果不是,那么实现交叉字段验证器的最简洁方法是什么?
例如,如何使用API来验证两个bean属性是否相等(例如验证密码字段是否与密码验证字段匹配).
在注释中,我希望有类似的东西:
public class MyBean {
@Size(min=6, max=50)
private String pass;
@Equals(property="pass")
private String passVerify;
}
Run Code Online (Sandbox Code Playgroud) 我在设置JSON模式时遇到问题,该模式将验证JSON是否包含:
但是当它们的倍数存在时不匹配.
在我的具体情况下,我想要一个
copyAllfileNamesmatchesFiles 和/或 doesntMatchFiles验证,但我不想接受超过那个.
这是我到目前为止所得到的:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [ "unrelatedA" ],
"properties": {
"unrelatedA": {
"type": "string"
},
"fileNames": {
"type": "array"
},
"copyAll": {
"type": "boolean"
},
"matchesFiles": {
"type": "array"
},
"doesntMatchFiles": {
"type": "array"
}
},
"oneOf": [
{"required": ["copyAll"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["fileNames"]}},
{"required": ["fileNames"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["copyAll"]}},
{"anyOf": [
{"required": ["matchesFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}},
{"required": ["doesntMatchFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}}]}
]
} ;
Run Code Online (Sandbox Code Playgroud)
这比我想要的更多.我希望这匹配以下所有内容:
{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": …Run Code Online (Sandbox Code Playgroud) 在我工作的公司中,我们有一个关于是否应该更好地使用基元的包装类(java.lang.Integer,java.lang.Long)或是否直接在映射的POJO中使用基元类型的讨论Hibernate中表的实体.
我们的想法是,我们希望这些值在数据库中不为空.
支持使用原语的论据:
支持使用包装器对象的参数:
我有一个springboot休息服务.用户传入一个json对象,该对象被反序列化到这个java pojo中:
public final class Request {
private String id;
private double code;
private String name;
public String getId() {
return id;
}
public double getCode() {
return code;
}
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
所以用户需要传入以下json:
{
"id": “123457896”,
"code": "Foo",
"name": "test"
}
Run Code Online (Sandbox Code Playgroud)
我想要完成所有这些领域.提供更少或更多的东西会引发异常.有没有办法告诉杰克逊在反序列化时验证输入?我试过@JsonProperty(required=true)但这不起作用; 显然从这里和这里看起来JsonProperty注释没有得到尊重.
我在我的控制器中调用了这个验证器:
@Component
public class RequestValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return false;
}
@Override
public void validate(Object target, Errors …Run Code Online (Sandbox Code Playgroud) 有没有办法使用此处定义的注释定义Hibernate验证规则,说明至少有一个字段不为空?
这将是一个假设的例子(@OneFieldMustBeNotNullConstraint并不存在):
@Entity
@OneFieldMustBeNotNullConstraint(list={fieldA,fieldB})
public class Card {
@Id
@GeneratedValue
private Integer card_id;
@Column(nullable = true)
private Long fieldA;
@Column(nullable = true)
private Long fieldB;
}
Run Code Online (Sandbox Code Playgroud)
在图示的情况下,fieldA可以为null或fieldB可以为null,但不能同时为两者.
一种方法是创建我自己的验证器,但我想避免它已经存在.如果你已经有一个验证器,请分享一个验证器...谢谢!
java ×3
validation ×3
hibernate ×2
json ×2
annotations ×1
jackson ×1
jsonschema ×1
pojo ×1
spring ×1
spring-mvc ×1