支持先format, 在验证 #4
21
validate.go
21
validate.go
@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
"github.com/tidwall/sjson"
|
"github.com/tidwall/sjson"
|
||||||
"strings"
|
"strings"
|
||||||
|
jsonFilter "git.zhangdeman.cn/zhangdeman/json_filter"
|
||||||
)
|
)
|
||||||
|
|
||||||
var validatorInstance *validator.Validate
|
var validatorInstance *validator.Validate
|
||||||
@ -35,8 +36,23 @@ func init() {
|
|||||||
//
|
//
|
||||||
// Date : 15:12 2025/3/18
|
// Date : 15:12 2025/3/18
|
||||||
func Run(sourceData []byte, fieldList []StructField) ([]byte, error) {
|
func Run(sourceData []byte, fieldList []StructField) ([]byte, error) {
|
||||||
|
mapRuleList := make([]jsonFilter.MapRule, 0)
|
||||||
|
for _, itemParam := range fieldList {
|
||||||
|
mapRuleList = append(mapRuleList, jsonFilter.MapRule{
|
||||||
|
SourcePath: itemParam.SourcePath,
|
||||||
|
TargetPath: itemParam.TargetPath,
|
||||||
|
Required: false,
|
||||||
|
DataType: itemParam.Type,
|
||||||
|
DefaultValue: itemParam.DefaultValue,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
filterInstance := jsonFilter.NewFilter(string(sourceData), mapRuleList)
|
||||||
|
if err := filterInstance.Deal(); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
formatByteData := filterInstance.Byte()
|
||||||
handleInstance := &handle{
|
handleInstance := &handle{
|
||||||
sourceData: sourceData,
|
sourceData: formatByteData,
|
||||||
fieldList: fieldList,
|
fieldList: fieldList,
|
||||||
parentFieldTable: map[string]bool{},
|
parentFieldTable: map[string]bool{},
|
||||||
}
|
}
|
||||||
@ -70,6 +86,9 @@ type handle struct {
|
|||||||
|
|
||||||
// Run 执行验证
|
// Run 执行验证
|
||||||
func (h *handle) Run() ([]byte, error) {
|
func (h *handle) Run() ([]byte, error) {
|
||||||
|
if len(h.fieldList) == 0 {
|
||||||
|
return []byte("{}"), nil
|
||||||
|
}
|
||||||
for _, field := range h.fieldList {
|
for _, field := range h.fieldList {
|
||||||
if len(field.Errmsg) == 0 {
|
if len(field.Errmsg) == 0 {
|
||||||
field.Errmsg = field.JsonTag + " : 参数校验不通过"
|
field.Errmsg = field.JsonTag + " : 参数校验不通过"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user