mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-01-18 03:28:19 +01:00
Update App version to 0.0.4
Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
This commit is contained in:
@@ -345,15 +345,35 @@ func (f *FieldConfig) validateField() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
val, err := toString(f.CurrentValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("field `%s` cannot use validate_type: %s", f.Name, err)
|
||||
switch v := f.CurrentValue.(type) {
|
||||
case []string:
|
||||
// Validate each element in the slice
|
||||
for _, item := range v {
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
if err := f.validateScalarValue(item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
default:
|
||||
val, err := toString(f.CurrentValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("field `%s` cannot use validate_type: %s", f.Name, err)
|
||||
}
|
||||
if val == "" {
|
||||
return nil
|
||||
}
|
||||
if err := f.validateScalarValue(val); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if val == "" {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateScalarValue applies ValidateTypes to a single string value.
|
||||
func (f *FieldConfig) validateScalarValue(val string) error {
|
||||
for _, validateType := range f.ValidateTypes {
|
||||
switch validateType {
|
||||
case "ipv4":
|
||||
@@ -376,6 +396,5 @@ func (f *FieldConfig) validateField() error {
|
||||
return fmt.Errorf("field `%s` cannot use validate_type: unsupported validator: %s", f.Name, validateType)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -22,6 +22,22 @@ func toString(v interface{}) (string, error) {
|
||||
return v, nil
|
||||
case null.String:
|
||||
return v.String, nil
|
||||
case []string:
|
||||
if len(v) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
if len(v) == 1 {
|
||||
return v[0], nil
|
||||
}
|
||||
return strings.Join(v, ","), nil
|
||||
case []interface{}:
|
||||
if len(v) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
if s, ok := v[0].(string); ok {
|
||||
return s, nil
|
||||
}
|
||||
return "", fmt.Errorf("unsupported type in slice: %T", v[0])
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("unsupported type: %s", reflect.TypeOf(v))
|
||||
|
||||
Reference in New Issue
Block a user