Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持了请求校验字符串长度的规则 #498

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 63 additions & 11 deletions APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions APIJSONORM/src/main/java/apijson/orm/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* }
*/
Expand Down