支持先format, 在验证
This commit is contained in:
		
							
								
								
									
										21
									
								
								validate.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								validate.go
									
									
									
									
									
								
							@ -20,6 +20,7 @@ import (
 | 
			
		||||
	"github.com/tidwall/gjson"
 | 
			
		||||
	"github.com/tidwall/sjson"
 | 
			
		||||
	"strings"
 | 
			
		||||
	jsonFilter "git.zhangdeman.cn/zhangdeman/json_filter"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var validatorInstance *validator.Validate
 | 
			
		||||
@ -35,8 +36,23 @@ func init() {
 | 
			
		||||
//
 | 
			
		||||
// Date : 15:12 2025/3/18
 | 
			
		||||
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{
 | 
			
		||||
		sourceData:       sourceData,
 | 
			
		||||
		sourceData:       formatByteData,
 | 
			
		||||
		fieldList:        fieldList,
 | 
			
		||||
		parentFieldTable: map[string]bool{},
 | 
			
		||||
	}
 | 
			
		||||
@ -70,6 +86,9 @@ type handle struct {
 | 
			
		||||
 | 
			
		||||
// Run 执行验证
 | 
			
		||||
func (h *handle) Run() ([]byte, error) {
 | 
			
		||||
	if len(h.fieldList) == 0 {
 | 
			
		||||
		return []byte("{}"), nil
 | 
			
		||||
	}
 | 
			
		||||
	for _, field := range h.fieldList {
 | 
			
		||||
		if len(field.Errmsg) == 0 {
 | 
			
		||||
			field.Errmsg = field.JsonTag + " : 参数校验不通过"
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user