Skip to content

Commit ced3fb9

Browse files
authored
新增校验请求字符串长度的规则,感谢 aninZz 的贡献 #498
#498
2 parents fb3fa39 + 37b160e commit ced3fb9

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java

+63-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,8 @@
2828
import java.time.LocalDate;
2929
import java.time.LocalDateTime;
3030
import java.time.LocalTime;
31-
import java.util.ArrayList;
32-
import java.util.Arrays;
33-
import java.util.Collection;
34-
import java.util.HashMap;
35-
import java.util.HashSet;
36-
import java.util.LinkedHashMap;
37-
import java.util.LinkedHashSet;
38-
import java.util.List;
39-
import java.util.Map;
40-
import java.util.Set;
41-
import java.util.SortedMap;
31+
import java.util.*;
32+
import java.util.regex.Matcher;
4233
import java.util.regex.Pattern;
4334

4435
import com.alibaba.fastjson.JSONArray;
@@ -134,6 +125,8 @@ public abstract class AbstractVerifier<T extends Object> implements Verifier<T>,
134125
// <PUT Comment, <1, { "method":"PUT", "tag":"Comment", "structure":{ "MUST":"id"... }... }>>
135126
@NotNull
136127
public static Map<String, SortedMap<Integer, JSONObject>> REQUEST_MAP;
128+
private static String VERIFY_LENGTH_RULE = "(?<first>[>=<]*)(?<second>[0-9]*)";
129+
private static Pattern VERIFY_LENGTH_PATTERN = Pattern.compile(VERIFY_LENGTH_RULE);
137130

138131
// 正则匹配的别名快捷方式,例如用 "PHONE" 代替 "^((13[0-9])|(15[^4,\\D])|(18[0-2,5-9])|(17[0-9]))\\d{8}$"
139132
@NotNull
@@ -1445,6 +1438,26 @@ else if (tv instanceof JSONArray) {
14451438
throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
14461439
}
14471440
}
1441+
else if (tk.endsWith("{L}")) { //字符串长度
1442+
if (tv instanceof String) {
1443+
logic = new Logic(tk.substring(0, tk.length() - 3));
1444+
1445+
rk = logic.getKey();
1446+
rv = real.get(rk);
1447+
if (rv == null) {
1448+
return;
1449+
}
1450+
String[] tvs = tv.toString().split(",");
1451+
for (String tvItem : tvs) {
1452+
if (!verifyRV(tvItem,rv.toString())) {
1453+
throw new IllegalArgumentException(rk + ":value 中value长度不合法!必须匹配 " + tk + ":" + tv + " !");
1454+
}
1455+
}
1456+
}
1457+
else {
1458+
throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
1459+
}
1460+
}
14481461
else if (tk.endsWith("<>")) { //rv包含tv内的值
14491462
logic = new Logic(tk.substring(0, tk.length() - 2));
14501463
rk = logic.getKey();
@@ -1485,6 +1498,45 @@ else if (tk.endsWith("<>")) { //rv包含tv内的值
14851498
}
14861499
}
14871500

1501+
/**
1502+
* 校验字符串长度
1503+
*
1504+
* @param rule 规则
1505+
* @param content 内容
1506+
* @return
1507+
* @throws UnsupportedDataTypeException
1508+
*/
1509+
private static boolean verifyRV(String rule,String content) throws UnsupportedDataTypeException {
1510+
String first = null;
1511+
String second = null;
1512+
Matcher matcher = VERIFY_LENGTH_PATTERN.matcher(rule);
1513+
while (matcher.find()) {
1514+
first = StringUtil.isEmpty(first)?matcher.group("first"):first;
1515+
second = StringUtil.isEmpty(second)?matcher.group("second"):second;
1516+
}
1517+
// first和second为空表示规则不合法
1518+
if(StringUtil.isEmpty(first) || StringUtil.isEmpty(second)){
1519+
throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
1520+
}
1521+
1522+
int secondNum = Integer.parseInt(second);
1523+
switch (Objects.requireNonNull(first)){
1524+
case ">":
1525+
return content.length() > secondNum;
1526+
case ">=":
1527+
return content.length() >= secondNum;
1528+
case "<":
1529+
return content.length() < secondNum;
1530+
case "<=":
1531+
return content.length() <= secondNum;
1532+
case "<>":
1533+
return content.length() != secondNum;
1534+
default:
1535+
}
1536+
// 出现不能识别的符号也认为规则不合法
1537+
throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
1538+
}
1539+
14881540
/**通过数据库执行SQL语句来验证条件
14891541
* @param funChar
14901542
* @param real

APIJSONORM/src/main/java/apijson/orm/Operation.java

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public enum Operation {
5454
* {
5555
* "phone~": "PHONE", //phone 必须满足 PHONE 的格式,配置见 {@link AbstractVerifier#COMPILE_MAP}
5656
* "status{}": [1,2,3], //status 必须在给出的范围内
57+
* "content{L}": ">0,<=255", //content的长度 必须在给出的范围内
5758
* "balance&{}":">0,<=10000" //必须满足 balance>0 & balance<=10000
5859
* }
5960
*/

0 commit comments

Comments
 (0)