|
28 | 28 | import java.time.LocalDate;
|
29 | 29 | import java.time.LocalDateTime;
|
30 | 30 | 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; |
42 | 33 | import java.util.regex.Pattern;
|
43 | 34 |
|
44 | 35 | import com.alibaba.fastjson.JSONArray;
|
@@ -134,6 +125,8 @@ public abstract class AbstractVerifier<T extends Object> implements Verifier<T>,
|
134 | 125 | // <PUT Comment, <1, { "method":"PUT", "tag":"Comment", "structure":{ "MUST":"id"... }... }>>
|
135 | 126 | @NotNull
|
136 | 127 | 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); |
137 | 130 |
|
138 | 131 | // 正则匹配的别名快捷方式,例如用 "PHONE" 代替 "^((13[0-9])|(15[^4,\\D])|(18[0-2,5-9])|(17[0-9]))\\d{8}$"
|
139 | 132 | @NotNull
|
@@ -1445,6 +1438,26 @@ else if (tv instanceof JSONArray) {
|
1445 | 1438 | throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
|
1446 | 1439 | }
|
1447 | 1440 | }
|
| 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 | + } |
1448 | 1461 | else if (tk.endsWith("<>")) { //rv包含tv内的值
|
1449 | 1462 | logic = new Logic(tk.substring(0, tk.length() - 2));
|
1450 | 1463 | rk = logic.getKey();
|
@@ -1485,6 +1498,45 @@ else if (tk.endsWith("<>")) { //rv包含tv内的值
|
1485 | 1498 | }
|
1486 | 1499 | }
|
1487 | 1500 |
|
| 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 | + |
1488 | 1540 | /**通过数据库执行SQL语句来验证条件
|
1489 | 1541 | * @param funChar
|
1490 | 1542 | * @param real
|
|
0 commit comments