Merge pull request '支持先format, 在验证' (#4) from feture/bug_fix into master

Reviewed-on: #4
This commit is contained in:
白茶清欢 2025-03-31 22:05:36 +08:00
commit 3477e2ec10

View File

@ -86,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 + " : 参数校验不通过"
@ -111,7 +114,11 @@ func (h *handle) Run() ([]byte, error) {
// 支持嵌套结构体
fieldTag := h.generateTag(field)
// 这里需要设置为对应类型的零值就行, 此处传入值的目的只是为了确认数据类型
h.dynamicStruct.AddField(field.JsonTag, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false)
path := field.TargetPath
if path == "" {
path = field.JsonTag
}
h.dynamicStruct.AddField(path, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false)
}
val := h.dynamicStruct.Build().New()
if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err {