gpt4 book ai didi

java - 复杂对象的验证

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:52 24 4
gpt4 key购买 nike

关于 hibernate validator 的新手问题。我有 2 个类,它们引用了另一个子类,并且根据父类对子类的验证应该更改。查看文档,似乎我应该使用 SequenceProvider,因此我为每个父类创建序列提供程序,然后为子类创建适当的验证组,但它似乎不起作用。

这是我的问题 -1)这是正确的方法吗?或者我应该使用其他东西2)如果这是正确的方法,我会错过什么

UserCreate 类 -

Code:
@GroupSequenceProvider(CreateUserGroupSequenceProvider.class)
public class CreateUserRequest {
@Valid
@NotNull
protected IdentityRequest identity;

@Valid
protected RegistrationProfileRequest registrationProfile;

@Valid
protected ProfileRequest profile;
}

UserUpdate 类 -

@GroupSequenceProvider(UpdateUserGroupSequenceProvider.class)
public class UpdateUserRequest {

@Valid
protected IdentityRequest identity;

@Valid
protected RegistrationProfileRequest registrationProfile;

@Valid
protected ProfileRequest profile;
}

这是子类,例如:IdentityRequest -

public class IdentityRequest {

@NotNull(groups = CreateIdentityValidation.class, message = "Invalid email")
@Email(regexp = "(.+@[a-zA-Z0-9-.]+\\.[a-zA-Z0-9-.]+)", groups = {CreateIdentityValidation.class, EmailValidation.class}, message = "Invalid email")
@SafeHtml(whitelistType = NONE, groups = {CreateIdentityValidation.class, EmailValidation.class}, message = "Invalid email")
private String email;

@NotNull(groups = {CreateIdentityValidation.class, PasswordValidation.class}, message = "Invalid password")
@Size(min = 8, max = 255, groups = {CreateIdentityValidation.class, PasswordValidation.class}, message = "Invalid password")
private String password;
}

因此,我创建了 2 个序列提供程序,每个序列提供程序为每个创建和更新请求创建一个。与在 IdentityRequest 中我为每个验证分配的组相比。

例如:当 Parent = CreateRequest 时,电子邮件和密码都是必需的,而当 UpdateRequest 时,电子邮件和密码是可选的。

CreateUserGroupSequenceProvider

public class CreateUserGroupSequenceProvider implements DefaultGroupSequenceProvider<CreateUserRequest> {

@Override
public List<Class<?>> getValidationGroups(CreateUserRequest createUserRequest) {
List<Class<?>> defaultGroupSequence = new ArrayList();
defaultGroupSequence.add(createUserRequest.class);

if (createUserRequest != null) {
addIdentityValidationGroups(defaultGroupSequence, createUserRequest.getIdentity());

addRegistrationProfileValidationGroups(defaultGroupSequence, createUserRequest.getRegistrationProfile());

addProfileValidationGroups(defaultGroupSequence, createUserRequest.getProfile());
}

return defaultGroupSequence;
}

@Override
protected void addIdentityValidationGroups(List<Class<?>> defaultGroupSequence, IdentityRequest identityRequest) {
//validation group for username/password
defaultGroupSequence.add(CreateIdentityValidation.class);
}
}

UpdateUserGroupSequenceProvider

public class UpdateUserGroupSequenceProvider implements DefaultGroupSequenceProvider<UpdateUserRequest> {

@Override
public List<Class<?>> getValidationGroups(UpdateUserRequest updateUserRequest) {
List<Class<?>> defaultGroupSequence = new ArrayList();
defaultGroupSequence.add(UpdateUserRequest.class);

if (updateUserRequest != null) {
addIdentityValidationGroups(defaultGroupSequence, updateUserRequest.getIdentity());

addRegistrationProfileValidationGroups(defaultGroupSequence, updateUserRequest.getRegistrationProfile());

addProfileValidationGroups(defaultGroupSequence, updateUserRequest.getProfile());
}

return defaultGroupSequence;
}

@Override
protected void addIdentityValidationGroups(List<Class<?>> defaultGroupSequence, IdentityRequest identityRequest) {
if (identityRequest != null) {
//Add password validation
if (identityRequest.getPassword() != null) {
defaultGroupSequence.add(IdentityValidation.PasswordValidation.class);
}
//Add email validation
if (identityRequest.getEmail() != null) {
defaultGroupSequence.add(IdentityValidation.EmailValidation.class);
}
}
}
}

任何帮助将不胜感激。即使经过多次调试,我也不清楚如何继续前进,它似乎跳过了 UpdateRequest 的电子邮件/密码验证,并将其缩小到下面的类和代码片段 -

Hibernate validator 版本 = 5.2.2.FinalValidatorImpl.java:第 1504 行

if ( !metaConstraint.getGroupList().contains( valueContext.getCurrentGroup() ) ) {
return false;
}

metaConstraint.getGroupList() = FirstNameValidationvalueContext.getCurrentGroup() = 默认

根据 SequenceProvider 文档,似乎所有验证都添加到“默认”组中。我假设我没有以正确的方式这样做。

如果我编写使用 groupName = FirstNameValidation.class 调用 validator 的单元测试,那么这些验证会起作用,但我使用的是 spring-boot,我希望仅在此对象上添加 @Valid 就会触发这些验证。

最佳答案

我认为您的问题是,默认组的重新定义对于 bean 定义来说只是本地的,无论您是静态重新定义默认组还是通过 GroupSequenceProvider 重新定义。 Bean 验证规范说:

Note however that the Default group sequence overriding is local to the class it is defined on and is not propagated to the associated objects.

检查规范的示例 4.21 - http://beanvalidation.org/1.1/spec/

关于java - 复杂对象的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36657347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com