Spring アプリケーションでネストされた検証を実行しようとしています。
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
private Child child;
// getters and setters omited
}
public class Child{
public interface OptionalGroup {}
@NotBlank(groups = OptionalGroup.class)
private String aField;
}
私はすでに javax.validation パッケージから @Valid を見ましたが、グループをサポートしていません。春の@Validatedアノテーションもチェックしましたが、フィールドに適用できません。
私は次のようなことをしたい:
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
@CascadeValidate(groups = MyGroup.class, cascadeGroups = OptionalGroup.class)
// 'groups' correspond to parent group and 'cascadeGroups' to a group that needs to be apply to the Child field.
private Child child;
}
そして、私は実用的に好きな場所で行うことができます:
@Inject SmartValidator validator;
public void validationMethod(Parent parent, boolean condition) throws ValidationException {
if(condition){
MapBindingResult errors= new MapBindingResult(new HashMap<>(), target.getClass().getSimpleName());
validator.validate(parent, errors, Parent.MyGroup.class); // validate all constraints associated to MyGroup
if(errors.hasErrors()) throw new ValidationException(errors); // A custom exception
}
}
それを行う方法はありますか?
どうもありがとう