ParamValidateUtil.java
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.xly.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xly.entity.ParamRule;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Objects;
/**
* 参数校验工具
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class ParamValidateUtil {
private final ObjectMapper objectMapper;
/**
* 校验参数
* @param paramRulesJson 数据库中的参数规则JSON
* @param params 大模型解析的JSON参数
*/
public void validate(String paramRulesJson, Map<String, Object> params) {
try {
// 解析参数规则
Map<String, ParamRule> ruleMap = objectMapper.readValue(
paramRulesJson,
objectMapper.getTypeFactory().constructMapType(
Map.class, String.class, ParamRule.class
)
);
if (Objects.isNull(ruleMap) || ruleMap.isEmpty()) {
return;
}
// 遍历校验
for (Map.Entry<String, ParamRule> entry : ruleMap.entrySet()) {
String paramName = entry.getKey();
ParamRule rule = entry.getValue();
Object paramValue = params.get(paramName);
// 非空校验
if (Boolean.TRUE.equals(rule.getBEmpty ()) && (Objects.isNull(paramValue) || paramValue.toString().isBlank())) {
throw new IllegalArgumentException(paramName + "不能为空");
}
// 类型校验 根据参数类型校验
if (Objects.nonNull(paramValue) && !paramValue.toString().isBlank()) {
String type = rule.getSType();
// if ("number".equals(type) && ValiDataUtil.me().isPureNumber(paramValue.toString())) {
// throw new IllegalArgumentException(paramName + "必须为数字类型");
// }
}
}
} catch (Exception e) {
log.error("参数校验失败", e);
throw new IllegalArgumentException("参数规则解析失败:" + e.getMessage());
}
}
}