1
1
package cn .sticki .spel .validator .javax .util ;
2
2
3
+ import cn .sticki .spel .validator .core .SpelValidExecutor ;
4
+ import cn .sticki .spel .validator .core .result .FieldError ;
5
+ import cn .sticki .spel .validator .core .result .ObjectValidResult ;
6
+ import cn .sticki .spel .validator .javax .SpelValid ;
3
7
import lombok .extern .slf4j .Slf4j ;
4
8
5
9
import javax .validation .ConstraintViolation ;
8
12
import java .util .Collection ;
9
13
import java .util .List ;
10
14
import java .util .Set ;
15
+ import java .util .stream .Collectors ;
11
16
12
17
/**
13
18
* 测试验证工具类
@@ -25,12 +30,30 @@ public class ValidateUtil {
25
30
/**
26
31
* 参数校验
27
32
* <p>
28
- * 调用此方法会触发 javax.validation.constraints.* 的校验,类似于使用 @Valid 注解
33
+ * 调用此方法会触发约束校验
29
34
*
30
- * @return 校验结果,如果校验通过则返回空列表
35
+ * @return 校验结果
31
36
*/
32
- public static <T > Set <ConstraintViolation <T >> validate (T obj ) {
33
- return validator .validate (obj );
37
+ public static ObjectValidResult validate (Object obj ) {
38
+ // 如果对象没有使用 SpelValid 注解,则直接调用验证执行器进行验证
39
+ // 这种情况下,只会验证本框架提供的约束注解
40
+ if (!obj .getClass ().isAnnotationPresent (SpelValid .class )) {
41
+ return SpelValidExecutor .validateObject (obj );
42
+ }
43
+
44
+ // 通过 @Valid 的方式进行验证
45
+ Set <ConstraintViolation <Object >> validate = validator .validate (obj );
46
+ if (validate == null || validate .isEmpty ()) {
47
+ return ObjectValidResult .EMPTY ;
48
+ }
49
+ ObjectValidResult validResult = new ObjectValidResult ();
50
+ List <FieldError > list = validate .stream ().map (ValidateUtil ::convert ).collect (Collectors .toList ());
51
+ validResult .addFieldError (list );
52
+ return validResult ;
53
+ }
54
+
55
+ private static FieldError convert (ConstraintViolation <Object > violation ) {
56
+ return FieldError .of (violation .getPropertyPath ().toString (), violation .getMessage ());
34
57
}
35
58
36
59
/**
@@ -61,8 +84,8 @@ public static boolean checkConstraintResult(VerifyObject verifyObject) {
61
84
int failCount = 0 ;
62
85
try {
63
86
// 执行约束校验
64
- Set < ConstraintViolation < Object >> validate = ValidateUtil .validate (object );
65
- failCount += processVerifyResult (verifyFailedFields , ConstraintViolationSet .of (validate ));
87
+ ObjectValidResult validResult = ValidateUtil .validate (object );
88
+ failCount += processVerifyResult (verifyFailedFields , ConstraintViolationSet .of (validResult . getErrors () ));
66
89
} catch (Exception e ) {
67
90
if (expectException ) {
68
91
log .info ("Passed, Capture exception {}, message: {}" , e .getClass (), e .getMessage ());
@@ -109,13 +132,13 @@ private static int processVerifyResult(Collection<VerifyFailedField> verifyFaile
109
132
110
133
boolean fieldMatch = false , find = false ;
111
134
112
- VerifyFailedField violation = violationSet .getAndRemove (fieldName , message );
113
- if (violation != null ) {
135
+ FieldError fieldError = violationSet .getAndRemove (fieldName , message );
136
+ if (fieldError != null ) {
114
137
find = true ;
115
- log .info ("Real exception information: {}" , violation . getMessage ());
138
+ log .info ("Real exception information: {}" , fieldError . getErrorMessage ());
116
139
117
140
// 异常信息不同时验证失败(没填写异常信息则不校验异常信息)
118
- if (message != null && !message .equals (violation . getMessage ())) {
141
+ if (message != null && !message .equals (fieldError . getErrorMessage ())) {
119
142
log .error ("Failed" );
120
143
} else {
121
144
fieldMatch = true ;
@@ -134,8 +157,8 @@ private static int processVerifyResult(Collection<VerifyFailedField> verifyFaile
134
157
135
158
LogContext .remove (fieldNameLogKey );
136
159
// 被忽略的字段
137
- for (VerifyFailedField violation : violationSet .getAll ()) {
138
- log .error ("Field [{}] is ignored" , violation .getName ());
160
+ for (FieldError violation : violationSet .getAll ()) {
161
+ log .error ("Field [{}] is ignored" , violation .getFieldName ());
139
162
failCount ++;
140
163
}
141
164
return failCount ;
0 commit comments