数据验证后增加 Result / Marshal / Transform 方法
This commit is contained in:
58
validate.go
58
validate.go
@@ -8,7 +8,6 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
@@ -33,7 +32,7 @@ func init() {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:12 2025/3/18
|
||||
func Run(sourceData []byte, fieldList []StructField) ([]byte, error) {
|
||||
func Run(sourceData []byte, fieldList []StructField) (*handle, error) {
|
||||
filterRuleList := make([]filter.MapRule, 0)
|
||||
for _, item := range fieldList {
|
||||
filterRuleList = append(filterRuleList, filter.MapRule{
|
||||
@@ -73,7 +72,10 @@ func Run(sourceData []byte, fieldList []StructField) ([]byte, error) {
|
||||
}
|
||||
handleInstance.dynamicStruct = dynamicStructGenerate.NewStruct(tagTable)
|
||||
|
||||
return handleInstance.Run()
|
||||
if err := handleInstance.Run(); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return handleInstance, nil
|
||||
}
|
||||
|
||||
type handle struct {
|
||||
@@ -82,12 +84,13 @@ type handle struct {
|
||||
dynamicStruct dynamicStructGenerate.IBuilder
|
||||
formatVal string
|
||||
parentFieldTable map[string]bool // 父级字段路径表
|
||||
res any // 处理结果
|
||||
}
|
||||
|
||||
// Run 执行验证
|
||||
func (h *handle) Run() ([]byte, error) {
|
||||
func (h *handle) Run() error {
|
||||
if len(h.fieldList) == 0 {
|
||||
return []byte("{}"), nil
|
||||
return nil
|
||||
}
|
||||
for _, field := range h.fieldList {
|
||||
if len(field.Errmsg) == 0 {
|
||||
@@ -98,7 +101,7 @@ func (h *handle) Run() ([]byte, error) {
|
||||
if h.parentFieldTable[field.TargetPath] {
|
||||
// 中间层级字段, 无需额外处理, 验一下必传就行
|
||||
if !gjson.GetBytes(h.sourceData, field.SourcePath).Exists() && field.Required {
|
||||
return nil, errors.New(field.TargetPath + " : 数据源路径数据不存在 => " + field.SourcePath)
|
||||
return errors.New(field.TargetPath + " : 数据源路径数据不存在 => " + field.SourcePath)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -122,17 +125,46 @@ func (h *handle) Run() ([]byte, error) {
|
||||
}
|
||||
val := h.dynamicStruct.Build().New()
|
||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
if err := defaults.Set(val); nil != err {
|
||||
// 默认值设置失败
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
if err := validatorInstance.Struct(val); nil != err {
|
||||
return nil, GetValidateErr(val, err, TagErrMsg)
|
||||
return GetValidateErr(val, err, TagErrMsg)
|
||||
}
|
||||
targetByte, _ := json.Marshal(val)
|
||||
return targetByte, nil
|
||||
h.res = val // 记录结果
|
||||
return nil
|
||||
}
|
||||
|
||||
// Result 获取结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:50 2025/4/28
|
||||
func (h *handle) Result() any {
|
||||
return h.res
|
||||
}
|
||||
|
||||
// Marshal 获取序列化之后的结果
|
||||
func (h *handle) Marshal(marshalType string) ([]byte, error) {
|
||||
if marshalType == "" {
|
||||
marshalType = "json" // 默认按照json序列化
|
||||
}
|
||||
return serialize.Wrapper.Marshal(marshalType, h.Result())
|
||||
}
|
||||
|
||||
// Transform 数据转换
|
||||
func (h *handle) Transform(targetType string, receiver any) error {
|
||||
if targetType == "" {
|
||||
targetType = "json" // 默认按照json序列化
|
||||
}
|
||||
byteData, err := serialize.Wrapper.Marshal(targetType, h.Result())
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return serialize.Wrapper.Unmarshal(targetType, byteData, receiver)
|
||||
}
|
||||
|
||||
// checkRequired 格式化必传参数
|
||||
@@ -164,6 +196,10 @@ func (h *handle) checkRequired(field StructField) (bool, bool) {
|
||||
func (h *handle) generateTag(field StructField) string {
|
||||
tagList := []string{
|
||||
fmt.Sprintf(`json:"%s"`, field.JsonTag), // json tag
|
||||
fmt.Sprintf(`xml:"%s"`, field.JsonTag), // xml tag
|
||||
fmt.Sprintf(`yaml:"%s"`, field.JsonTag), // yaml tag
|
||||
fmt.Sprintf(`toml:"%s"`, field.JsonTag), // toml tag
|
||||
fmt.Sprintf(`ini:"%s"`, field.JsonTag), // ini tag
|
||||
}
|
||||
if len(field.Errmsg) == 0 {
|
||||
field.Errmsg = field.JsonTag + ": 参数校验不通过"
|
||||
|
||||
Reference in New Issue
Block a user