-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Validation): support date time range value validation
- Loading branch information
1 parent
0951f97
commit 96c8b83
Showing
9 changed files
with
244 additions
and
25 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
common/src/main/java/com/jmsoftware/maf/common/bean/EnumerationBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jmsoftware.maf.common.bean; | ||
|
||
/** | ||
* <h1>EnumerationBase</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: [email protected] | ||
* @date 6/6/21 5:38 PM | ||
**/ | ||
public interface EnumerationBase<T extends Number> { | ||
/** | ||
* Gets value. | ||
* | ||
* @return the value | ||
*/ | ||
T getValue(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...in/java/com/jmsoftware/maf/springcloudstarter/configuration/MinioClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.jmsoftware.maf.springcloudstarter.configuration; | ||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
|
||
/** | ||
* <h1>MinioConfiguration</h1> | ||
* <p> | ||
* Change description here. | ||
* | ||
* @author Johnny Miller (鍾俊), email: [email protected] | ||
* @date 4/29/21 2:23 PM | ||
**/ | ||
@Data | ||
@Slf4j | ||
@Validated | ||
@Component | ||
@ConfigurationProperties(prefix = MinioClientConfiguration.PREFIX) | ||
public class MinioClientConfiguration { | ||
public static final String PREFIX = "minio.client.configuration"; | ||
@NotBlank | ||
private String endpoint; | ||
@NotNull | ||
private Integer port; | ||
@NotBlank | ||
private String accessKey; | ||
@NotBlank | ||
private String secretKey; | ||
private Boolean secure = Boolean.TRUE; | ||
private String bucketName; | ||
private String configDir; | ||
} |
24 changes: 24 additions & 0 deletions
24
...com/jmsoftware/maf/springcloudstarter/validation/annotation/DateTimeRangeConstraints.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.jmsoftware.maf.springcloudstarter.validation.annotation; | ||
|
||
import com.jmsoftware.maf.springcloudstarter.validation.validator.DateTimeRangeValidator; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Description: DateTimeRangeConstraints, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: [email protected], date: 6/3/2021 2:08 PM | ||
**/ | ||
@Documented | ||
@Constraint(validatedBy = DateTimeRangeValidator.class) | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DateTimeRangeConstraints { | ||
String message() default "Invalid date time range"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
17 changes: 17 additions & 0 deletions
17
.../java/com/jmsoftware/maf/springcloudstarter/validation/annotation/DateTimeRangeGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.jmsoftware.maf.springcloudstarter.validation.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Description: DateTimeRangeGroup, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: [email protected], date: 6/3/2021 2:58 PM | ||
**/ | ||
@Documented | ||
@Target({ElementType.FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DateTimeRangeGroup { | ||
String groupName() default "defaultDateTimeRangeGroup"; | ||
|
||
DateTimeRangeType type() default DateTimeRangeType.START_TIME; | ||
} |
17 changes: 17 additions & 0 deletions
17
...n/java/com/jmsoftware/maf/springcloudstarter/validation/annotation/DateTimeRangeType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.jmsoftware.maf.springcloudstarter.validation.annotation; | ||
|
||
/** | ||
* Description: DateTimeRangeType, change description here. | ||
* | ||
* @author 钟俊 (zhongjun), email: [email protected], date: 6/3/2021 3:07 PM | ||
*/ | ||
public enum DateTimeRangeType { | ||
/** | ||
* Start time | ||
*/ | ||
START_TIME, | ||
/** | ||
* End time | ||
*/ | ||
END_TIME, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...va/com/jmsoftware/maf/springcloudstarter/validation/validator/DateTimeRangeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.jmsoftware.maf.springcloudstarter.validation.validator; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import cn.hutool.core.util.ObjectUtil; | ||
import cn.hutool.core.util.ReflectUtil; | ||
import com.jmsoftware.maf.springcloudstarter.validation.annotation.DateTimeRangeConstraints; | ||
import com.jmsoftware.maf.springcloudstarter.validation.annotation.DateTimeRangeGroup; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import java.lang.reflect.Field; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
|
||
/** | ||
* Description: DateTimeRangeValidator, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: [email protected], date: 6/3/2021 2:10 PM | ||
**/ | ||
@Slf4j | ||
public class DateTimeRangeValidator implements ConstraintValidator<DateTimeRangeConstraints, Object> { | ||
public static final int MAX_GROUP_SIZE = 2; | ||
|
||
@Override | ||
public boolean isValid(Object value, ConstraintValidatorContext context) { | ||
val fields = value.getClass().getDeclaredFields(); | ||
final HashSet<Field> annotatedFieldSet = CollUtil.newHashSet(); | ||
for (val field : fields) { | ||
val annotation = field.getAnnotation(DateTimeRangeGroup.class); | ||
if (ObjectUtil.isNotNull(annotation)) { | ||
annotatedFieldSet.add(field); | ||
} | ||
} | ||
if (CollUtil.isEmpty(annotatedFieldSet)) { | ||
log.warn("There is not fields annotated by {} in the class({})", value.getClass().getName(), | ||
DateTimeRangeGroup.class.getSimpleName()); | ||
return true; | ||
} | ||
final HashMap<String, LinkedList<Field>> dateTimeRangeGroupMap = CollUtil.newHashMap(fields.length); | ||
for (val field : annotatedFieldSet) { | ||
val annotation = field.getAnnotation(DateTimeRangeGroup.class); | ||
if (!dateTimeRangeGroupMap.containsKey(annotation.groupName())) { | ||
dateTimeRangeGroupMap.put(annotation.groupName(), CollUtil.newLinkedList(field)); | ||
} else { | ||
dateTimeRangeGroupMap.get(annotation.groupName()).add(field); | ||
if (dateTimeRangeGroupMap.get(annotation.groupName()).size() > MAX_GROUP_SIZE) { | ||
log.error("The length of DateTimeRangeGroup({}) cannot exceed {}!", annotation.groupName(), MAX_GROUP_SIZE); | ||
return false; | ||
} | ||
} | ||
} | ||
for (val entry : dateTimeRangeGroupMap.entrySet()) { | ||
val groupName = entry.getKey(); | ||
val fieldList = entry.getValue(); | ||
if (fieldList.size() != MAX_GROUP_SIZE) { | ||
log.error("The length of DateTimeRangeGroup({}) is not correct!", groupName); | ||
return false; | ||
} | ||
val dateTimeRangeGroup = fieldList.get(0).getAnnotation(DateTimeRangeGroup.class); | ||
Date 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)); | ||
break; | ||
case END_TIME: | ||
startTime = (Date) ReflectUtil.getFieldValue(value, fieldList.get(1)); | ||
endTime = (Date) ReflectUtil.getFieldValue(value, fieldList.get(0)); | ||
break; | ||
default: | ||
} | ||
if (ObjectUtil.isAllNotEmpty(startTime, endTime) && startTime.after(endTime)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.