Skip to content

Commit

Permalink
feat($Validation): support Date and LocalDateTime range validation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 27, 2021
1 parent 778e152 commit f9898be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package com.jmsoftware.maf.authcenter.user.entity;

import com.jmsoftware.maf.common.bean.PaginationBase;
import com.jmsoftware.maf.springcloudstarter.validation.annotation.DateTimeRangeConstraints;
import com.jmsoftware.maf.springcloudstarter.validation.annotation.DateTimeRangeGroup;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

import static com.jmsoftware.maf.springcloudstarter.validation.annotation.DateTimeRangeType.END_TIME;
import static com.jmsoftware.maf.springcloudstarter.validation.annotation.DateTimeRangeType.START_TIME;

/**
* Description: GetUserPageList, change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 4:32 PM
**/
@Data
@DateTimeRangeConstraints
@EqualsAndHashCode(callSuper = true)
public class GetUserPageListPayload extends PaginationBase {
private String username;
@DateTimeRangeGroup(type = START_TIME)
private LocalDateTime startTime;
@DateTimeRangeGroup(type = END_TIME)
private LocalDateTime endTime;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.maf.springcloudstarter.constant;
package com.jmsoftware.maf.common.constant;

/**
* Description: UniversalDateTime, change description here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.jmsoftware.maf.springcloudstarter.constant.UniversalDateTime;
import com.jmsoftware.maf.common.constant.UniversalDateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -63,20 +64,30 @@ public boolean isValid(Object value, ConstraintValidatorContext context) {
return false;
}
val dateTimeRangeGroup = fieldList.get(0).getAnnotation(DateTimeRangeGroup.class);
Date startTime = null, endTime = null;
Object startTime = null, endTime = null;
switch (dateTimeRangeGroup.type()) {
case START_TIME:
startTime = (Date) ReflectUtil.getFieldValue(value, fieldList.get(0));
endTime = (Date) ReflectUtil.getFieldValue(value, fieldList.get(1));
startTime = ReflectUtil.getFieldValue(value, fieldList.get(0));
endTime = ReflectUtil.getFieldValue(value, fieldList.get(1));
break;
case END_TIME:
startTime = (Date) ReflectUtil.getFieldValue(value, fieldList.get(1));
endTime = (Date) ReflectUtil.getFieldValue(value, fieldList.get(0));
startTime = ReflectUtil.getFieldValue(value, fieldList.get(1));
endTime = ReflectUtil.getFieldValue(value, fieldList.get(0));
break;
default:
}
if (ObjectUtil.isAllNotEmpty(startTime, endTime) && startTime.after(endTime)) {
return false;
if (ObjectUtil.hasNull(startTime, endTime)) {
return true;
}
if (startTime instanceof Date && endTime instanceof Date) {
if (((Date) startTime).after((Date) endTime)) {
return false;
}
}
if (startTime instanceof LocalDateTime && endTime instanceof LocalDateTime) {
if (((LocalDateTime) startTime).isAfter((LocalDateTime) endTime)) {
return false;
}
}
}
return true;
Expand Down

0 comments on commit f9898be

Please sign in to comment.