Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit 49564f0

Browse files
committed
refactor: 💄 Refactor validation constants and update usage
Migrate validation annotations to JSRAnnotationConstants and deprecate ValidatorAnnotations. Update all relevant usages across the codebase to utilize the new constants. This change streamlines the validation logic and improves code maintainability.
1 parent 81431c0 commit 49564f0

10 files changed

Lines changed: 208 additions & 50 deletions

src/main/java/com/ly/doc/constants/DocValidatorAnnotationEnum.java

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,97 +33,127 @@ public enum DocValidatorAnnotationEnum {
3333
/**
3434
* Spring validator annotations `@NotEmpty`
3535
*/
36-
NOT_EMPTY("NotEmpty"),
36+
NOT_EMPTY(JSRAnnotationConstants.NOT_EMPTY),
3737

3838
/**
3939
* Spring validator annotations `@NotBlank`
4040
*/
41-
NOT_BLANK("NotBlank"),
41+
NOT_BLANK(JSRAnnotationConstants.NOT_BLANK),
4242

4343
/**
4444
* Spring validator annotations `@NotNull`
4545
*/
46-
NOT_NULL("NotNull"),
46+
NOT_NULL(JSRAnnotationConstants.NOT_NULL),
4747

4848
/**
4949
* Spring validator annotations `@Null`
5050
*/
51-
NULL("Null"),
51+
NULL(JSRAnnotationConstants.NULL),
5252

5353
/**
5454
* Spring validator annotations `@AssertTrue`
5555
*/
56-
ASSERT_TRUE("AssertTrue"),
56+
ASSERT_TRUE(JSRAnnotationConstants.ASSERT_TRUE),
5757

5858
/**
5959
* Spring validator annotations `@AssertFalse`
6060
*/
61-
ASSERT_FALSE("AssertFalse"),
61+
ASSERT_FALSE(JSRAnnotationConstants.ASSERT_FALSE),
6262

6363
/**
6464
* Spring validator annotations `@Min`
6565
*/
66-
MIN("Min"),
66+
MIN(JSRAnnotationConstants.MIN),
6767

6868
/**
6969
* Spring validator annotations `@Max`
7070
*/
71-
MAX("Max"),
71+
MAX(JSRAnnotationConstants.MAX),
7272

7373
/**
7474
* Spring validator annotations `@DecimalMin`
7575
*/
76-
DECIMAL_MIN("DecimalMin"),
76+
DECIMAL_MIN(JSRAnnotationConstants.DECIMAL_MIN),
7777

7878
/**
7979
* Spring validator annotations `@DecimalMax`
8080
*/
81-
DECIMAL_MAX("DecimalMax"),
81+
DECIMAL_MAX(JSRAnnotationConstants.DECIMAL_MAX),
8282

8383
/**
8484
* Spring validator annotations `@Size`
8585
*/
86-
SIZE("Size"),
86+
SIZE(JSRAnnotationConstants.SIZE),
8787

8888
/**
8989
* Spring validator annotations `@Digits`
9090
*/
91-
DIGITS("Digits"),
91+
DIGITS(JSRAnnotationConstants.DIGITS),
9292

9393
/**
9494
* Spring validator annotations `@Past`
9595
*/
96-
PAST("Past"),
96+
PAST(JSRAnnotationConstants.PAST),
97+
98+
/**
99+
* Spring validator annotations `@PastOrPresent`
100+
*/
101+
PAST_OR_PRESENT(JSRAnnotationConstants.PAST_OR_PRESENT),
97102

98103
/**
99104
* Spring validator annotations `@Future`
100105
*/
101-
FUTURE("Future"),
106+
FUTURE(JSRAnnotationConstants.FUTURE),
107+
108+
/**
109+
* Spring validator annotations `@FutureOrPresent`
110+
*/
111+
FUTURE_OR_PRESENT(JSRAnnotationConstants.FUTURE_OR_PRESENT),
102112

103113
/**
104114
* Spring validator annotations `@Pattern`
105115
*/
106-
PATTERN("Pattern"),
116+
PATTERN(JSRAnnotationConstants.PATTERN),
117+
118+
/**
119+
* Spring validator annotations `@Positive`
120+
*/
121+
POSITIVE(JSRAnnotationConstants.POSITIVE),
122+
123+
/**
124+
* Spring validator annotations `@PositiveOrZero`
125+
*/
126+
POSITIVE_OR_ZERO(JSRAnnotationConstants.POSITIVE_OR_ZERO),
127+
128+
/**
129+
* Spring validator annotations `@Negative`
130+
*/
131+
NEGATIVE(JSRAnnotationConstants.NEGATIVE),
132+
133+
/**
134+
* Spring validator annotations `@NegativeOrZero`
135+
*/
136+
NEGATIVE_OR_ZERO(JSRAnnotationConstants.NEGATIVE_OR_ZERO),
107137

108138
/**
109139
* Spring validator annotations `@Email`
110140
*/
111-
EMAIL("Email"),
141+
EMAIL(JSRAnnotationConstants.EMAIL),
112142

113143
/**
114144
* Spring validator annotations `@Length`
115145
*/
116-
LENGTH("Length"),
146+
LENGTH(JSRAnnotationConstants.LENGTH),
117147

118148
/**
119149
* Spring validator annotations `@Range`
120150
*/
121-
RANGE("Range"),
151+
RANGE(JSRAnnotationConstants.RANGE),
122152

123153
/**
124154
* Spring validator annotations `@Validated`
125155
*/
126-
VALIDATED("Validated");
156+
VALIDATED(JSRAnnotationConstants.VALIDATED);
127157

128158
/**
129159
* annotation value

src/main/java/com/ly/doc/constants/JSRAnnotationConstants.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,142 @@
2424
* @author yu 2024/6/7
2525
*/
2626
public interface JSRAnnotationConstants {
27+
28+
// begin from javax.validation:validation-api
29+
/**
30+
* Spring validator annotations `@AssertFalse`
31+
*/
32+
String ASSERT_FALSE = "AssertFalse";
33+
34+
/**
35+
* Spring validator annotations `@AssertTrue`
36+
*/
37+
String ASSERT_TRUE = "AssertTrue";
38+
39+
/**
40+
* Spring validator annotations `@DecimalMax`
41+
*/
42+
String DECIMAL_MAX = "DecimalMax";
43+
44+
/**
45+
* Spring validator annotations `@DecimalMin`
46+
*/
47+
String DECIMAL_MIN = "DecimalMin";
48+
49+
/**
50+
* Spring validator annotations `@Digits`
51+
*/
52+
String DIGITS = "Digits";
53+
54+
/**
55+
* Spring validator annotations `@Email`
56+
*/
57+
String EMAIL = "Email";
58+
59+
/**
60+
* Spring validator annotations `@Future`
61+
*/
62+
String FUTURE = "Future";
63+
64+
/**
65+
* Spring validator annotations `@FutureOrPresent`
66+
*/
67+
String FUTURE_OR_PRESENT = "FutureOrPresent";
68+
69+
/**
70+
* Spring validator annotations `@Max`
71+
*/
2772
String MAX = "Max";
2873

74+
/**
75+
* Spring validator annotations `@Min`
76+
*/
2977
String MIN = "Min";
3078

79+
/**
80+
* Spring validator annotations `@Negative`
81+
*/
82+
String NEGATIVE = "Negative";
83+
84+
/**
85+
* Spring validator annotations `@NegativeOrZero`
86+
*/
87+
String NEGATIVE_OR_ZERO = "NegativeOrZero";
88+
89+
/**
90+
* Spring validator annotations `@NotBlank`
91+
*/
92+
String NOT_BLANK = "NotBlank";
93+
94+
/**
95+
* Spring validator annotations `@NotEmpty`
96+
*/
97+
String NOT_EMPTY = "NotEmpty";
98+
99+
/**
100+
* Spring validator annotations `@NotNull`
101+
*/
102+
String NOT_NULL = "NotNull";
103+
104+
/**
105+
* Spring validator annotations `@Null`
106+
*/
107+
String NULL = "Null";
108+
109+
/**
110+
* Spring validator annotations `@Past`
111+
*/
112+
String PAST = "Past";
113+
114+
/**
115+
* Spring validator annotations `@PastOrPresent`
116+
*/
117+
String PAST_OR_PRESENT = "PastOrPresent";
118+
119+
/**
120+
* Spring validator annotations `@Pattern`
121+
*/
122+
String PATTERN = "Pattern";
123+
124+
/**
125+
* Spring validator annotations `@Positive`
126+
*/
127+
String POSITIVE = "Positive";
128+
129+
/**
130+
* Spring validator annotations `@PositiveOrZero`
131+
*/
132+
String POSITIVE_OR_ZERO = "PositiveOrZero";
133+
134+
/**
135+
* Spring validator annotations `@Size`
136+
*/
31137
String SIZE = "Size";
32138

139+
// end from javax.validation:validation-api
140+
141+
// begin from org.hibernate.validator:hibernate-validator
142+
/**
143+
* Spring validator annotations `@Length`
144+
*/
33145
String LENGTH = "Length";
146+
147+
/**
148+
* Spring validator annotations `@Range`
149+
*/
150+
String RANGE = "Range";
151+
152+
// end from org.hibernate.validator:hibernate-validator
153+
154+
/**
155+
* java annotations `@Valid`
156+
* jakarta.validation.Valid
157+
* javax.validation.Valid
158+
*/
159+
String VALID = "Valid";
160+
161+
/**
162+
* Spring validator annotations `@Validated`
163+
*/
164+
String VALIDATED = "Validated";
34165
}

src/main/java/com/ly/doc/constants/ValidatorAnnotations.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,41 @@
2222

2323
/**
2424
* @author yu 2019/12/22.
25+
*
26+
* @see JSRAnnotationConstants
2527
*/
28+
@Deprecated
2629
public interface ValidatorAnnotations {
2730

2831
/**
2932
* java annotations `@Valid`
3033
* jakarta.validation.Valid
3134
* javax.validation.Valid
3235
*/
33-
String VALID = "Valid";
36+
String VALID = JSRAnnotationConstants.VALID;
3437

3538
/**
3639
* Spring validator annotations `@NotEmpty`
3740
*/
38-
String NOT_EMPTY = "NotEmpty";
41+
String NOT_EMPTY = JSRAnnotationConstants.NOT_EMPTY;
3942

4043
/**
4144
* Spring validator annotations `@NotBlank`
4245
*/
43-
String NOT_BLANK = "NotBlank";
46+
String NOT_BLANK = JSRAnnotationConstants.NOT_BLANK;
4447

4548
/**
4649
* Spring validator annotations `@NotNull`
4750
*/
48-
String NOT_NULL = "NotNull";
51+
String NOT_NULL = JSRAnnotationConstants.NOT_NULL;
4952

5053
/**
5154
* Spring validator annotations `@Null`
5255
*/
53-
String NULL= "Null";
56+
String NULL = JSRAnnotationConstants.NULL;
5457

5558
/**
5659
* Spring validator annotations `@Validated`
5760
*/
58-
String VALIDATED = "Validated";
61+
String VALIDATED = JSRAnnotationConstants.VALIDATED;
5962
}

src/main/java/com/ly/doc/helper/FormDataBuildHelper.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
import java.util.*;
2424

25-
import com.ly.doc.constants.DocGlobalConstants;
26-
import com.ly.doc.constants.ParamTypeConstants;
27-
import com.ly.doc.constants.ValidatorAnnotations;
25+
import com.ly.doc.constants.*;
2826
import com.ly.doc.model.ApiConfig;
2927
import com.ly.doc.model.CustomField;
3028
import com.ly.doc.model.DocJavaField;
@@ -34,7 +32,6 @@
3432
import com.power.common.util.RandomUtil;
3533
import com.power.common.util.StringUtil;
3634
import com.ly.doc.builder.ProjectDocConfigBuilder;
37-
import com.ly.doc.constants.DocTags;
3835
import com.thoughtworks.qdox.model.JavaAnnotation;
3936
import com.thoughtworks.qdox.model.JavaClass;
4037
import com.thoughtworks.qdox.model.JavaField;
@@ -119,7 +116,7 @@ public static List<FormData> getFormData(String className, Map<String, String> r
119116
List<JavaAnnotation> javaAnnotations = docField.getAnnotations();
120117
for (JavaAnnotation annotation : javaAnnotations) {
121118
String simpleAnnotationName = annotation.getType().getValue();
122-
if (ValidatorAnnotations.NULL.equals(simpleAnnotationName)) {
119+
if (JSRAnnotationConstants.NULL.equals(simpleAnnotationName)) {
123120
if (CollectionUtil.isEmpty(groupClasses)) {
124121
continue out;
125122
}

src/main/java/com/ly/doc/helper/JsonBuildHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public static String buildJson(String typeName, String genericCanonicalName,
236236
List<JavaAnnotation> annotations = docField.getAnnotations();
237237
for (JavaAnnotation annotation : annotations) {
238238
String annotationName = annotation.getType().getValue();
239-
if (ValidatorAnnotations.NULL.equals(annotationName) && !isResp) {
239+
if (JSRAnnotationConstants.NULL.equals(annotationName) && !isResp) {
240240
if (CollectionUtil.isEmpty(groupClasses)) {
241241
continue out;
242242
}

src/main/java/com/ly/doc/helper/ParamsBuildHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
223223
if (null != annotation.getProperty(DocAnnotationConstants.VALUE_PROP)) {
224224
fieldName = StringUtil.removeQuotes(annotation.getProperty(DocAnnotationConstants.VALUE_PROP).toString());
225225
}
226-
} else if (ValidatorAnnotations.NULL.equals(simpleAnnotationName) && !isResp) {
226+
} else if (JSRAnnotationConstants.NULL.equals(simpleAnnotationName) && !isResp) {
227227
if (CollectionUtil.isEmpty(groupClasses)) {
228228
continue out;
229229
}
@@ -240,10 +240,10 @@ public static List<ApiParam> buildParams(String className, String pre, int level
240240

241241
if (hasGroup) {
242242
strRequired = true;
243-
} else {
243+
} else if (CollectionUtil.isEmpty(groupClasses)) {
244244
// If the annotation is @Valid or @Validated, the Default group is added by default and groupClasses will not be empty;
245245
// In other cases, if groupClasses is still empty, then strRequired is false.
246-
strRequired = CollectionUtil.isEmpty(groupClasses);
246+
strRequired = false;
247247
}
248248
}
249249
}

0 commit comments

Comments
 (0)