Skip to content

Commit 311689c

Browse files
@initial 7.0.0-preview.422.1 Add validator for request params
1 parent ed396d2 commit 311689c

3 files changed

Lines changed: 186 additions & 1 deletion

File tree

src/main/java/com/talexframe/frame/core/modules/network/connection/RequestAnalyser.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.talexframe.frame.core.pojo.wrapper.WrappedResponse;
1717
import com.talexframe.frame.utils.ReqMethodUtil;
1818
import com.talexframe.frame.utils.UrlUtil;
19+
import com.talexframe.frame.utils.ValidatorUtil;
1920
import lombok.Getter;
2021
import lombok.Setter;
2122
import lombok.SneakyThrows;
@@ -148,6 +149,8 @@ private void process() {
148149
TParam param = paramReceiver.tParam;
149150
Parameter parameter = paramReceiver.getParameter();
150151

152+
TPParamValidator validator = parameter.getAnnotation(TPParamValidator.class);
153+
151154
try {
152155

153156
String fieldName = param.field() != null ? param.field() : parameter.getName();
@@ -164,6 +167,39 @@ private void process() {
164167

165168
Object obj = json.get(fieldName, parameter.getType());
166169

170+
if ( validator != null ) {
171+
172+
int back = ValidatorUtil.validateData(validator, obj);
173+
174+
if( back != 100 ) {
175+
176+
wr.returnDataByFailed(ResultData.ResultEnum.INFORMATION_ERROR, "Data error");
177+
178+
switch ( back ) {
179+
180+
case ValidatorUtil.MISS_MAX:
181+
log.info("[解析层] AccessDenied # MissingMax " + fieldName);
182+
case ValidatorUtil.MISS_MIN:
183+
log.info("[解析层] AccessDenied # MissingMin " + fieldName);
184+
case ValidatorUtil.MISS_PATTERN:
185+
log.info("[解析层] AccessDenied # MissingPattern " + fieldName);
186+
case ValidatorUtil.MISS_MAX_LENGTH:
187+
log.info("[解析层] AccessDenied # MissingMaxLength " + fieldName);
188+
case ValidatorUtil.MISS_MIN_LENGTH:
189+
log.info("[解析层] AccessDenied # MissingMinLength " + fieldName);
190+
case ValidatorUtil.MISS_ASSERT:
191+
log.info("[解析层] AccessDenied # MissingAssert " + fieldName);
192+
case ValidatorUtil.MISS_DATA:
193+
log.info("[解析层] AccessDenied # MissingData " + fieldName);
194+
195+
}
196+
197+
return;
198+
199+
}
200+
201+
}
202+
167203
params.add(obj);
168204

169205
}
@@ -178,7 +214,6 @@ private void process() {
178214

179215
}
180216

181-
182217
}
183218

184219
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.talexframe.frame.core.pojo.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* 加在字段上进行请求校验
10+
* {@link com.talexframe.frame.core.pojo.annotations Package }
11+
*
12+
* @author TalexDreamSoul 22/04/17 上午 08:34 Project: TalexFrame
13+
*/
14+
@Retention( RetentionPolicy.RUNTIME )
15+
@Target( { ElementType.PARAMETER, ElementType.TYPE } )
16+
public @interface TPParamValidator {
17+
18+
/**
19+
* 校验规则 # 必须满足当前正则表达式
20+
*/
21+
String pattern() default "";
22+
23+
/**
24+
* 校验规则 # 必须满足不为 null
25+
*/
26+
boolean notNull() default true;
27+
28+
/**
29+
* 自动类型断言 # true
30+
* 必须元素为true可用
31+
*/
32+
boolean assertTrue() default false;
33+
34+
/**
35+
* 自动类型断言 # false
36+
* 必须元素为false可用
37+
*/
38+
boolean assertFalse() default false;
39+
40+
/**
41+
* 校验规则 # 必须满足最小值
42+
*/
43+
int min() default Integer.MIN_VALUE;
44+
45+
/**
46+
* 校验规则 # 必须满足最大值
47+
*/
48+
int max() default Integer.MAX_VALUE;
49+
50+
/**
51+
* 校验规则 # 必须满足最小长度
52+
*/
53+
int minLength() default Integer.MIN_VALUE;
54+
55+
/**
56+
* 校验规则 # 必须满足最大长度
57+
*/
58+
int maxLength() default Integer.MAX_VALUE;
59+
60+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.talexframe.frame.utils;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
import com.talexframe.frame.core.pojo.annotations.TPParamValidator;
5+
6+
/**
7+
* {@link com.talexframe.frame.utils Package }
8+
*
9+
* @author TalexDreamSoul 22/04/22 下午 09:20 Project: TalexFrame
10+
*/
11+
public class ValidatorUtil {
12+
13+
public static final int SUCCESS = 100;
14+
public static final int MISS_DATA = 0;
15+
public static final int MISS_PATTERN = -100;
16+
public static final int MISS_ASSERT = -10;
17+
public static final int MISS_MIN_LENGTH = -21;
18+
public static final int MISS_MAX_LENGTH = -22;
19+
20+
public static final int MISS_MIN = -31;
21+
public static final int MISS_MAX = -32;
22+
23+
public static int validateData(TPParamValidator validator, Object data) {
24+
25+
if( validator == null ) return SUCCESS;
26+
27+
if( data == null ) return validator.notNull() ? MISS_DATA : SUCCESS;
28+
29+
if( !StrUtil.isBlankIfStr(validator.pattern()) ) {
30+
31+
if( !data.toString().matches(validator.pattern()) ) return MISS_PATTERN;
32+
33+
}
34+
35+
if( validator.assertFalse() || validator.assertTrue() ) {
36+
37+
if( data instanceof Boolean ) {
38+
39+
boolean b = (boolean) data;
40+
41+
return ((b && validator.assertTrue()) || (!b && validator.assertFalse())) ? SUCCESS : MISS_ASSERT;
42+
43+
} else {
44+
45+
return MISS_ASSERT;
46+
47+
}
48+
49+
}
50+
51+
String str = (String) data;
52+
53+
if( validator.maxLength() > 0 && str.length() < validator.minLength() ) return MISS_MIN_LENGTH;
54+
if( validator.maxLength() > 0 && str.length() > validator.maxLength() ) return MISS_MAX_LENGTH;
55+
56+
if( validator.min() != Integer.MIN_VALUE ) {
57+
58+
try {
59+
60+
int tmp = Integer.parseInt(str);
61+
return tmp < validator.min() ? MISS_MIN : SUCCESS;
62+
63+
} catch (NumberFormatException e) {
64+
65+
return MISS_MIN;
66+
67+
}
68+
69+
}
70+
71+
if( validator.max() != Integer.MAX_VALUE ) {
72+
73+
try {
74+
75+
int tmp = Integer.parseInt(str);
76+
return tmp > validator.max() ? MISS_MAX : SUCCESS;
77+
78+
} catch (NumberFormatException e) {
79+
80+
return MISS_MAX;
81+
82+
}
83+
84+
}
85+
86+
return SUCCESS;
87+
88+
}
89+
90+
}

0 commit comments

Comments
 (0)