-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
161 additions
and
86 deletions.
There are no files selected for viewing
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
...dator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ConstraintViolationSet.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 cn.sticki.spel.validator.javax.util; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 约束违反集合 | ||
* <p> | ||
* 用于存储校验结果,根据字段名和期望的错误信息来获取字段约束结果 | ||
* | ||
* @author 阿杆 | ||
* @since 2024/10/29 | ||
*/ | ||
public class ConstraintViolationSet { | ||
|
||
private static final ConstraintViolationSet EMPTY = new ConstraintViolationSet(Collections.emptyList()); | ||
|
||
private final Map<String, List<VerifyFailedField>> verifyMap; | ||
|
||
public ConstraintViolationSet(Collection<VerifyFailedField> failedFields) { | ||
if (failedFields == null || failedFields.isEmpty()) { | ||
verifyMap = Collections.emptyMap(); | ||
return; | ||
} | ||
|
||
this.verifyMap = failedFields.stream().collect(Collectors.groupingBy(VerifyFailedField::getName)); | ||
} | ||
|
||
public static ConstraintViolationSet of(Set<ConstraintViolation<Object>> validate) { | ||
if (validate == null || validate.isEmpty()) { | ||
return EMPTY; | ||
} | ||
List<VerifyFailedField> list = validate.stream().map(ConstraintViolationSet::convert).collect(Collectors.toList()); | ||
return new ConstraintViolationSet(list); | ||
} | ||
|
||
public static ConstraintViolationSet of(List<VerifyFailedField> validate) { | ||
return new ConstraintViolationSet(validate); | ||
} | ||
|
||
private static VerifyFailedField convert(ConstraintViolation<Object> violation) { | ||
return VerifyFailedField.of(violation.getPropertyPath().toString(), violation.getMessage()); | ||
} | ||
|
||
/** | ||
* 根据字段和期望的错误信息来获取字段约束结果 | ||
* | ||
* @param fieldName 字段名 | ||
* @param expectMessage 期望的错误信息 | ||
* @return 字段约束结果,当 expectMessage 不为null时,会优先匹配具有相同message的数据 | ||
*/ | ||
public VerifyFailedField getAndRemove(String fieldName, String expectMessage) { | ||
List<VerifyFailedField> violationList = verifyMap.get(fieldName); | ||
if (violationList == null || violationList.isEmpty()) { | ||
return null; | ||
} | ||
if (violationList.size() == 1 || expectMessage == null) { | ||
VerifyFailedField violation = violationList.get(0); | ||
verifyMap.remove(fieldName); | ||
return violation; | ||
} | ||
// 当存在多个约束时,优先匹配具有相同message的数据。 | ||
// 否则当一个字段有多个约束条件时,无法匹配到期望的约束。 | ||
for (VerifyFailedField violation : violationList) { | ||
if (expectMessage.equals(violation.getMessage())) { | ||
violationList.remove(violation); | ||
return violation; | ||
} | ||
} | ||
|
||
return violationList.remove(0); | ||
} | ||
|
||
/** | ||
* 获取所有的约束违反字段 | ||
*/ | ||
public Set<VerifyFailedField> getAll() { | ||
return verifyMap.values().stream().flatMap(List::stream).collect(Collectors.toSet()); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/LogContext.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,63 @@ | ||
package cn.sticki.spel.validator.javax.util; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* 日志上下文 | ||
* | ||
* @author 阿杆 | ||
* @since 2024/10/29 | ||
*/ | ||
@Slf4j | ||
public class LogContext { | ||
|
||
/** | ||
* 设置验证对象日志上下文 | ||
*/ | ||
public static void setValidateObject(Object object) { | ||
String className = object.getClass().getSimpleName(); | ||
Class<?> enclosingClass = object.getClass().getEnclosingClass(); | ||
if (enclosingClass != null) { | ||
className = enclosingClass.getSimpleName() + "." + className; | ||
} | ||
|
||
MDC.put("className", className); | ||
MDC.put("fullClassName", abbreviate(object.getClass().getName())); | ||
if (object instanceof ID) { | ||
MDC.put("id", String.valueOf(((ID) object).getId())); | ||
} | ||
} | ||
|
||
/** | ||
* 清除验证对象日志上下文 | ||
*/ | ||
public static void clearValidateObject() { | ||
MDC.remove("id"); | ||
MDC.remove("className"); | ||
MDC.remove("fieldName"); | ||
MDC.remove("fullClassName"); | ||
} | ||
|
||
public static void set(String key, String value) { | ||
MDC.put(key, value); | ||
} | ||
|
||
public static void remove(String key) { | ||
MDC.remove(key); | ||
} | ||
|
||
/** | ||
* 缩写类名 | ||
*/ | ||
private static String abbreviate(String className) { | ||
String[] parts = className.split("\\."); | ||
StringBuilder abbreviated = new StringBuilder(); | ||
for (int i = 0; i < parts.length - 1; i++) { | ||
abbreviated.append(parts[i].charAt(0)).append("."); | ||
} | ||
abbreviated.append(parts[parts.length - 1]); | ||
return abbreviated.toString(); | ||
} | ||
|
||
} |
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