java开源验证框架OVAL

  • @Length,@MaxLength,@MinLength

maxLength,minLength只有value属性,表示和value进行比较

min和max是Length的属性

汉字算一个长度

  • @NotBlank、@NotNull、@NotEmpty

NotBlank:Check if the string representation is not empty and does not only contain white spaces.

NotNull:Check if not null.

NotEmpty:Check if the string representation is not empty ("").

  • @CheckWith

Check the value by a method of the sameclass that takes the value as argument and returns true if valid and false ifinvalid.

使用net.sf.oval.constraint.CheckWithCheck.SimpleCheck实现该接口的类中isSatisfied方法来判断,返回true有效,false无效;如果实现类是内部的,必须为静态类。

示例:验证User类中的age字段

@CheckWith(value=CheckAge.class,message="agemust in (18~65)")

private int age;

 

验证类如下:

publicclass CheckAge implements CheckWithCheck.SimpleCheck {

    private static final long serialVersionUID =1L;

    @Override

    public boolean isSatisfied(Object validatedObject, Object value) {

        if (!validatedObject instanceof User) {

             return false;

        }

        User user = (User)validatedObject;

        int age = user.getAge();

        if(age <18 || age > 65)

            return false;

        else

            return true;

    }

}
 

  • 完全验证

  • 选择性验证(多个验证选择性验证)

List<ConstraintViolation> message = validator.validate(entity, profiles);

根据profiles进行选择性验证entity对象,可以指定多个profiles

  • 验证对象的某个字段

public List<ConstraintViolation> validateFieldValue(final Object validatedObject, final Field validatedField,final Object fieldValueToValidate)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值