Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lwydyby/valid-version
Supports versioned java bean validation tools
https://github.com/lwydyby/valid-version
Last synced: 8 days ago
JSON representation
Supports versioned java bean validation tools
- Host: GitHub
- URL: https://github.com/lwydyby/valid-version
- Owner: lwydyby
- Created: 2020-01-03T02:52:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-03T03:24:30.000Z (almost 5 years ago)
- Last Synced: 2024-10-30T04:56:02.554Z (about 2 months ago)
- Language: Java
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### 简介
java bean基于注解的校验框架,由于要使用openstack java sdk,而openstack本身是支持多个版本的,
所以需要同一个实体类按版本号进行不同的校验,所以java/spring自带的校验工具并不能满足需求,所以
手撸了一个简易的可以分版本校验的通用框架。(目前只支持了notNull注解,后续根据工作需求会同步更新
,或者有人使用后可以提PR)### 使用说明
#### 1. 添加注解
````java
@Data
public class Project {
@NotNull(version = 2.8)
private String projectId;}
````#### 2.调用校验
````java
class test{
public static void main(String[] args){
IResult result=ValidFactory.on(value,version,group).valid().result();
//控制台打印结果
result.print();
if(!result.pass()){
result.throwsEx();
}
}
}
````### 增加其他校验流程
#### 1. 重写AbstractAnnotationCondition
````java
public class NotNullCondition extends AbstractAnnotationCondition {
@Override
protected ICondition buildCondition(NotNull annotation) {
return new NotNullConditionImpl(annotation);
}static class NotNullConditionImpl implements ICondition{
NotNull notNull;
private NotNullConditionImpl(NotNull notNull) {
this.notNull = notNull;
}@Override
public boolean condition(IConditionContext conditionContext) {
double version=notNull.version();
double validVersion=conditionContext.version();
if(validVersion groupList= Arrays.asList(group);
groupList.retainAll(Arrays.asList(validGroup));
if(groupList.size()==0){
//不在校验范围内,跳出
return true;
}
}
return !StringUtils.isEmpty(conditionContext.value());
}
}
}
````#### 2. 编写自定义注解,来指定校验所用的类
````java
@Inherited
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Condition(NotNullCondition.class)
public @interface NotNull {
/**
* 校验的起始版本号
* @return 版本号
*/
double version();/**
* 校验的分组
* @return 校验的分组
*/
Class[] group() default {};String message() default "%s Must Not NUll";
}
````