1 回答
TA贡献1829条经验 获得超9个赞
这里有两个选项:
1 - 在 setter 上添加注释(简单但不可重用:
@Parsed
@Validate(nullable = false)
public void setSomeString(String value){
if(value.length() < 3 || value.length() > 5){
throw new DataValidationException("SomeString can't have length " + value.length());
}
this.someString = value;
}
2 - 扩展类并在注释上使用该类:ValidatedConversion@Convert
public class LengthValidator extends ValidatedConversion {
private int min;
private int max;
public LengthValidator(String... args) {
super(false, false); //not null / not blank
this.min = Integer.parseInt(args[0]);
this.max = Integer.parseInt(args[1]);
}
@Override
protected void validate(Object value) {
super.validate(value); //let super check for null and whatever you need.
String string = value.toString();
if(string.length() < min || string.length() > max){
throw new com.univocity.parsers.common.DataValidationException("Value can't have length " + string.length());
}
}
}
然后将其添加到您的属性中:
@Parsed
@Convert(conversionClass = LengthValidator.class, args = {"3", "5"})
private String someString;
希望这有帮助。
添加回答
举报