api-assert 一个的Java校验小框架、简单、好用

Api-Assert 介绍

api-assert 是一个经量级的小小框架,或者说是一个工具类,用于提供在日常开发中,经常需要一些条件判断,如果条件成立需要抛出异常编写的重复,api-assert 提供了链式校验的方式,目前提供了几种检查器:

  • com.old.apiAssert.check.FirstApiAssert 第一检查器,检查器可以提供链式编程,并且当第一个条件成立之后就不会再替换异常信息,并由调用者最终决定是否抛出异常
  • com.old.apiAssert.check.FunctionApiAssert 由调用者提供异常对象,当条件成立时会立刻抛出异常
  • com.old.apiAssert.check.OperateApiAssert 与 Optional 类的思想类似,用于提供对一个对象的校验与对象内部属性的校验,支持 Lambda,并支持 FunctionApiAssert 检查器的功能。
  • com.old.apiAssert.check.ReflectionApiAssert 调用者传入异常类型,检查器当条件成立之后会立刻抛出异常
  • com.old.apiAssert.check.EnumFunctionApiAssert 与 FunctionApiAssert 功能相同,其消息内容为 Enum 类型
  • com.old.apiAssert.check.EnumOperateApiAssert 与 OperateApiAssert 功能相同,其消息内容为 Enum 类型

简单介绍

在日常使用中,常用的是OperateApiAssertFunctionApiAssertFunctionApiAssert 可以作为常量使用,指定一个异常,声明一个常量进行使用,避免重复创建对象的浪费。

OperateApiAssert 支持FunctionApiAssert的功能,并提供与 mybatis-plus 的 lambdaWrapper 使用类似的方式,可以直接获取元素的内部属性进行判断。

EnumFunctionApiAssertEnumOperateApiAssert与原有检查器相同,但其消息类型为枚举类型。

注意

2.0.0 版本与之前的版本不太兼容,使用需要注意。2.0 版本将框架进行了重写,使用了继承方式,抛弃了原有的复合方式。并重新抽取了父类与规范。

版本

2.0.0

因 2.0 版本将框架进行了重构,所以版本号使用新的大版本号。2.0.0 相比之前的版本,扩展性更高,重复代码更少。并且提供了 Enum 作为消息内容的校验器。

  • 重构代码
  • 新增枚举校验器 EnumOperationApiAssert、EnumFunctionApiAssert
  • OperationApiAssert 增加 校验对象、标准校验器的 then 方法

用例

添加 maven 依赖

<dependency>
  <groupId>io.github.min1854</groupId>
  <artifactId>apiAssert</artifactId>
  <version>2.0.0</version>
</dependency>

使用

使用试便

public class Demo {
   

    public OperateApiAssert<TestEntity> createAssert() {
   
        TestEntity entity = new TestEntity();
        entity.setId(1);
        entity.setDeleteFlag(false);
        return OperateApiAssert.create(entity, NoArgConstructorException::new);
    }


    @Test(expected = ApiAssertException.class
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值