修复数据验证的BUG + 升级不同类型数据默认值的获取
This commit is contained in:
26
validate.go
26
validate.go
@ -88,19 +88,14 @@ func (h *handle) Run() ([]byte, error) {
|
||||
Args: nil,
|
||||
})
|
||||
}
|
||||
sourceValue, err := h.getSourceDataValue(field)
|
||||
if nil != err {
|
||||
// 格式化数据
|
||||
if _, err := h.formatDataValue(field); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
// 支持嵌套结构体
|
||||
fieldTag := h.generateTag(field)
|
||||
if nil == sourceValue {
|
||||
// 没出现异常, 但是value为nil, 视作参数不存在处理
|
||||
// TODO: 这里需要设置为对应类型的零值
|
||||
h.dynamicStruct.AddField(field.JsonTag, "", "", fieldTag, false)
|
||||
continue
|
||||
}
|
||||
h.dynamicStruct.AddField(field.JsonTag, "", sourceValue, fieldTag, false)
|
||||
// 这里需要设置为对应类型的零值就行, 此处传入值的目的只是为了确认数据类型
|
||||
h.dynamicStruct.AddField(field.JsonTag, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false)
|
||||
}
|
||||
val := h.dynamicStruct.Build().New()
|
||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err {
|
||||
@ -139,7 +134,7 @@ func (h *handle) checkRequired(field StructField) (bool, bool) {
|
||||
}
|
||||
|
||||
// getSourceDataValue 获取源数据值
|
||||
func (h *handle) getSourceDataValue(field StructField) (any, error) {
|
||||
func (h *handle) formatDataValue(field StructField) (any, error) {
|
||||
sourceValue := gjson.GetBytes(h.sourceData, field.SourcePath)
|
||||
if !sourceValue.Exists() {
|
||||
if field.Required {
|
||||
@ -171,6 +166,7 @@ func (h *handle) getSourceDataValue(field StructField) (any, error) {
|
||||
}
|
||||
}()
|
||||
switch field.Type {
|
||||
|
||||
case consts.DataTypeInt: // Int类型
|
||||
fallthrough
|
||||
case consts.DataTypeIntPtr: // Uint类型
|
||||
@ -181,15 +177,17 @@ func (h *handle) getSourceDataValue(field StructField) (any, error) {
|
||||
case consts.DataTypeUintPtr: // Uint类型
|
||||
val, err = gjson_hack.Uint(sourceValue)
|
||||
return val, err
|
||||
case consts.DataTypeFloat:
|
||||
case consts.DataTypeFloat32:
|
||||
fallthrough
|
||||
case consts.DataTypeFloatPtr: // Float类型
|
||||
case consts.DataTypeFloat32Ptr: // Float类型
|
||||
val, err = gjson_hack.Float64(sourceValue)
|
||||
return val, err
|
||||
case consts.DataTypeString: // String类型
|
||||
return sourceValue.String(), nil
|
||||
val = sourceValue.String()
|
||||
return val, nil
|
||||
case consts.DataTypeBool: // Bool类型
|
||||
return sourceValue.Bool(), nil
|
||||
val = sourceValue.Bool()
|
||||
return val, nil
|
||||
case consts.DataTypeSliceFloat: // Float slice
|
||||
val, err = gjson_hack.SliceFloat(sourceValue)
|
||||
return val, err
|
||||
|
Reference in New Issue
Block a user