From 37b160efe6ab7d607482ab14727aebfa716c191d Mon Sep 17 00:00:00 2001
From: chenglining <chenglining@herotech.ltd>
Date: Thu, 12 Jan 2023 16:13:41 +0800
Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=86=E8=AF=B7=E6=B1=82?=
 =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=95=BF=E5=BA=A6?=
 =?UTF-8?q?=E7=9A=84=E8=A7=84=E5=88=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../java/apijson/orm/AbstractVerifier.java    | 74 ++++++++++++++++---
 .../src/main/java/apijson/orm/Operation.java  |  1 +
 2 files changed, 64 insertions(+), 11 deletions(-)

diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java b/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java
index 22267936f..4063fd172 100755
--- a/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java
+++ b/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java
@@ -28,17 +28,8 @@
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedMap;
+import java.util.*;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import com.alibaba.fastjson.JSONArray;
@@ -134,6 +125,8 @@ public abstract class AbstractVerifier<T extends Object> implements Verifier<T>,
 	// <PUT Comment, <1, { "method":"PUT", "tag":"Comment", "structure":{ "MUST":"id"... }... }>>
 	@NotNull
 	public static Map<String, SortedMap<Integer, JSONObject>> REQUEST_MAP;
+	private static String VERIFY_LENGTH_RULE = "(?<first>[>=<]*)(?<second>[0-9]*)";
+	private static Pattern VERIFY_LENGTH_PATTERN = Pattern.compile(VERIFY_LENGTH_RULE);
 
 	// 正则匹配的别名快捷方式,例如用 "PHONE" 代替 "^((13[0-9])|(15[^4,\\D])|(18[0-2,5-9])|(17[0-9]))\\d{8}$"
 	@NotNull
@@ -1445,6 +1438,26 @@ else if (tv instanceof JSONArray) {
 				throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
 			}
 		}
+		else if (tk.endsWith("{L}")) { //字符串长度
+			if (tv instanceof String) {
+				logic = new Logic(tk.substring(0, tk.length() - 3));
+
+				rk = logic.getKey();
+				rv = real.get(rk);
+				if (rv == null) {
+					return;
+				}
+				String[] tvs = tv.toString().split(",");
+				for (String tvItem : tvs) {
+					if (!verifyRV(tvItem,rv.toString())) {
+						throw new IllegalArgumentException(rk + ":value 中value长度不合法!必须匹配 " + tk + ":" + tv + " !");
+					}
+				}
+			}
+			else {
+				throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
+			}
+		}
 		else if (tk.endsWith("<>")) { //rv包含tv内的值
 			logic = new Logic(tk.substring(0, tk.length() - 2));
 			rk = logic.getKey();
@@ -1485,6 +1498,45 @@ else if (tk.endsWith("<>")) { //rv包含tv内的值
 		}
 	}
 
+	/**
+	 * 校验字符串长度
+	 *
+	 * @param rule	规则
+	 * @param content	内容
+	 * @return
+	 * @throws UnsupportedDataTypeException
+	 */
+	private static boolean verifyRV(String rule,String content) throws UnsupportedDataTypeException {
+		String first = null;
+		String second = null;
+		Matcher matcher = VERIFY_LENGTH_PATTERN.matcher(rule);
+		while (matcher.find()) {
+			first = StringUtil.isEmpty(first)?matcher.group("first"):first;
+			second = StringUtil.isEmpty(second)?matcher.group("second"):second;
+		}
+		// first和second为空表示规则不合法
+		if(StringUtil.isEmpty(first) || StringUtil.isEmpty(second)){
+			throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
+		}
+
+		int secondNum = Integer.parseInt(second);
+		switch (Objects.requireNonNull(first)){
+			case ">":
+				return content.length() > secondNum;
+			case ">=":
+				return content.length() >= secondNum;
+			case "<":
+				return content.length() < secondNum;
+			case "<=":
+				return content.length() <= secondNum;
+			case "<>":
+				return content.length() != secondNum;
+			default:
+		}
+		// 出现不能识别的符号也认为规则不合法
+		throw new UnsupportedDataTypeException("服务器Request表verify配置错误!");
+	}
+
 	/**通过数据库执行SQL语句来验证条件
 	 * @param funChar
 	 * @param real
diff --git a/APIJSONORM/src/main/java/apijson/orm/Operation.java b/APIJSONORM/src/main/java/apijson/orm/Operation.java
index c3a1541bf..8481268f4 100755
--- a/APIJSONORM/src/main/java/apijson/orm/Operation.java
+++ b/APIJSONORM/src/main/java/apijson/orm/Operation.java
@@ -54,6 +54,7 @@ public enum Operation {
 	 * {
 	 *   "phone~": "PHONE",  //phone 必须满足 PHONE 的格式,配置见 {@link AbstractVerifier#COMPILE_MAP}
 	 *   "status{}": [1,2,3],  //status 必须在给出的范围内
+	 *   "content{L}": ">0,<=255",  //content的长度 必须在给出的范围内
 	 *   "balance&{}":">0,<=10000"  //必须满足 balance>0 & balance<=10000
 	 * }
 	 */