fix(validation): correct CustomValidator and strutils.Parser handling, add tests

This commit is contained in:
yusing
2025-10-15 14:20:47 +08:00
parent b09bfd6c1e
commit feafdf05f2
9 changed files with 519 additions and 41 deletions

View File

@@ -90,37 +90,6 @@ func ValidateWithFieldTags(s any) gperr.Error {
return errs.Error()
}
var validatorType = reflect.TypeFor[CustomValidator]()
func ValidateWithCustomValidator(v reflect.Value) gperr.Error {
if v.Kind() == reflect.Struct {
if v.Type().Implements(validatorType) {
return v.Interface().(CustomValidator).Validate()
}
if v.CanAddr() {
return validateWithValidator(v.Addr())
}
return nil
}
if v.Kind() == reflect.Pointer {
if v.IsNil() {
return nil
}
if v.Type().Implements(validatorType) {
return v.Interface().(CustomValidator).Validate()
}
return validateWithValidator(v.Elem())
}
return nil
}
func validateWithValidator(v reflect.Value) gperr.Error {
if v.Type().Implements(validatorType) {
return v.Interface().(CustomValidator).Validate()
}
return nil
}
func dive(dst reflect.Value) (v reflect.Value, t reflect.Type, err gperr.Error) {
dstT := dst.Type()
for {
@@ -529,6 +498,12 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
default:
}
// check if (*T).Convertor is implemented
if dst.Addr().Type().Implements(parserType) {
parser := dst.Addr().Interface().(strutils.Parser)
return true, gperr.Wrap(parser.Parse(src))
}
if gi.ReflectIsNumeric(dst) || dst.Kind() == reflect.Bool {
err := gi.ReflectStrToNumBool(dst, src)
if err != nil {
@@ -537,12 +512,6 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
return true, nil
}
// check if (*T).Convertor is implemented
if dst.Addr().Type().Implements(parserType) {
parser := dst.Addr().Interface().(strutils.Parser)
return true, gperr.Wrap(parser.Parse(src))
}
// yaml like
var tmp any
switch dst.Kind() {

View File

@@ -1,6 +1,8 @@
package serialization
import (
"reflect"
"github.com/go-playground/validator/v10"
gperr "github.com/yusing/goutils/errs"
)
@@ -9,10 +11,6 @@ var validate = validator.New()
var ErrValidationError = gperr.New("validation error")
type CustomValidator interface {
Validate() gperr.Error
}
func Validator() *validator.Validate {
return validate
}
@@ -23,3 +21,40 @@ func MustRegisterValidation(tag string, fn validator.Func) {
panic(err)
}
}
type CustomValidator interface {
Validate() gperr.Error
}
var validatorType = reflect.TypeFor[CustomValidator]()
func ValidateWithCustomValidator(v reflect.Value) gperr.Error {
if v.Kind() == reflect.Pointer {
if v.IsNil() {
// return nil
return validateWithValidator(reflect.New(v.Type().Elem()))
}
if v.Type().Implements(validatorType) {
return v.Interface().(CustomValidator).Validate()
}
return validateWithValidator(v.Elem())
} else {
vt := v.Type()
if vt.PkgPath() != "" { // not a builtin type
if vt.Implements(validatorType) {
return v.Interface().(CustomValidator).Validate()
}
if v.CanAddr() {
return validateWithValidator(v.Addr())
}
}
}
return nil
}
func validateWithValidator(v reflect.Value) gperr.Error {
if v.Type().Implements(validatorType) {
return v.Interface().(CustomValidator).Validate()
}
return nil
}

View File

@@ -0,0 +1,34 @@
package serialization
import (
"testing"
"github.com/go-playground/validator/v10"
)
// Common helper functions
func ptr[T any](s T) *T {
return &s
}
// Common test function for MustRegisterValidation
func TestMustRegisterValidation(t *testing.T) {
// Test registering a custom validation
fn := func(fl validator.FieldLevel) bool {
return fl.Field().String() != "invalid"
}
// This should not panic
MustRegisterValidation("test_tag", fn)
// Verify the validation was registered
err := validate.VarWithValue("valid", "test", "test_tag")
if err != nil {
t.Errorf("Expected validation to pass, got error: %v", err)
}
err = validate.VarWithValue("invalid", "test", "test_tag")
if err == nil {
t.Error("Expected validation to fail")
}
}

View File

@@ -0,0 +1,126 @@
package serialization
import (
"reflect"
"testing"
gperr "github.com/yusing/goutils/errs"
)
// Test cases for when *T implements CustomValidator but T is passed in
type CustomValidatingInt int
func (c *CustomValidatingInt) Validate() gperr.Error {
if c == nil {
return gperr.New("pointer int cannot be nil")
}
if *c <= 0 {
return gperr.New("int must be positive")
}
if *c > 100 {
return gperr.New("int must be <= 100")
}
return nil
}
// Test cases for when T implements CustomValidator but *T is passed in
type CustomValidatingFloat float64
func (c CustomValidatingFloat) Validate() gperr.Error {
if c < 0 {
return gperr.New("float must be non-negative")
}
if c > 1000 {
return gperr.New("float must be <= 1000")
}
return nil
}
func TestValidateWithCustomValidator_PointerMethodButValuePassed(t *testing.T) {
tests := []struct {
name string
input CustomValidatingInt
wantErr bool
}{
{"custom validating int as value - valid", CustomValidatingInt(50), false},
{"custom validating int as value - zero", CustomValidatingInt(0), false},
{"custom validating int as value - negative", CustomValidatingInt(-5), false},
{"custom validating int as value - large", CustomValidatingInt(200), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_PointerMethodWithPointerPassed(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingInt
wantErr bool
}{
{"valid custom validating int pointer", ptr(CustomValidatingInt(50)), false},
{"nil custom validating int pointer", nil, true}, // Should fail because Validate() checks for nil
{"invalid custom validating int pointer - zero", ptr(CustomValidatingInt(0)), true},
{"invalid custom validating int pointer - negative", ptr(CustomValidatingInt(-5)), true},
{"invalid custom validating int pointer - too large", ptr(CustomValidatingInt(200)), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_ValueMethodButPointerPassed(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingFloat
wantErr bool
}{
{"valid custom validating float pointer", ptr(CustomValidatingFloat(50.5)), false},
{"nil custom validating float pointer", nil, false},
{"invalid custom validating float pointer - negative", ptr(CustomValidatingFloat(-5.5)), true},
{"invalid custom validating float pointer - too large", ptr(CustomValidatingFloat(2000.5)), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_ValueMethodWithValuePassed(t *testing.T) {
tests := []struct {
name string
input CustomValidatingFloat
wantErr bool
}{
{"valid custom validating float", CustomValidatingFloat(50.5), false},
{"invalid custom validating float - negative", CustomValidatingFloat(-5.5), true},
{"invalid custom validating float - too large", CustomValidatingFloat(2000.5), true},
{"valid custom validating float - boundary", CustomValidatingFloat(1000), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -0,0 +1,93 @@
package serialization
import (
"reflect"
"testing"
gperr "github.com/yusing/goutils/errs"
)
type CustomValidatingPointerString string
func (c *CustomValidatingPointerString) Validate() gperr.Error {
if c == nil {
return gperr.New("pointer string cannot be nil")
}
if *c == "" {
return gperr.New("string cannot be empty")
}
if len(*c) < 2 {
return gperr.New("string must be at least 2 characters")
}
return nil
}
func TestValidateWithCustomValidator_StringPointer(t *testing.T) {
tests := []struct {
name string
input *string
wantErr bool
}{
{"valid string pointer", ptr("hello"), false},
{"nil string pointer", nil, false},
{"empty string pointer", ptr(""), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingPointerStringValue(t *testing.T) {
tests := []struct {
name string
input CustomValidatingPointerString
wantErr bool
}{
{"custom validating pointer string as value - valid", CustomValidatingPointerString("hello"), false},
{"custom validating pointer string as value - empty", CustomValidatingPointerString(""), false},
{"custom validating pointer string as value - short", CustomValidatingPointerString("a"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingPointerStringPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingPointerString
wantErr bool
}{
{"valid custom validating pointer string", customStringPointerPtr(CustomValidatingPointerString("hello")), false},
{"nil custom validating pointer string", nil, true}, // Should fail because Validate() checks for nil
{"invalid custom validating pointer string - empty", customStringPointerPtr(CustomValidatingPointerString("")), true},
{"invalid custom validating pointer string - too short", customStringPointerPtr(CustomValidatingPointerString("a")), true},
{"valid custom validating pointer string - minimum length", customStringPointerPtr(CustomValidatingPointerString("ab")), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// Helper function to create CustomValidatingPointerString pointer
func customStringPointerPtr(s CustomValidatingPointerString) *CustomValidatingPointerString {
return &s
}

View File

@@ -0,0 +1,85 @@
package serialization
import (
"reflect"
"testing"
gperr "github.com/yusing/goutils/errs"
)
type CustomValidatingString string
func (c CustomValidatingString) Validate() gperr.Error {
if c == "" {
return gperr.New("string cannot be empty")
}
if len(c) < 2 {
return gperr.New("string must be at least 2 characters")
}
return nil
}
func TestValidateWithCustomValidator_String(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"regular string - no custom validation", "hello", false},
{"empty regular string - no custom validation", "", false},
{"short regular string - no custom validation", "a", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingString(t *testing.T) {
tests := []struct {
name string
input CustomValidatingString
wantErr bool
}{
{"valid custom validating string", CustomValidatingString("hello"), false},
{"invalid custom validating string - empty", CustomValidatingString(""), true},
{"invalid custom validating string - too short", CustomValidatingString("a"), true},
{"valid custom validating string - minimum length", CustomValidatingString("ab"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingStringPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingString
wantErr bool
}{
{"valid custom validating string pointer", ptr(CustomValidatingString("hello")), false},
{"nil custom validating string pointer", nil, true},
{"invalid custom validating string pointer - empty", ptr(CustomValidatingString("")), true},
{"invalid custom validating string pointer - too short", ptr(CustomValidatingString("a")), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -0,0 +1,69 @@
package serialization
import (
"reflect"
"testing"
gperr "github.com/yusing/goutils/errs"
)
type CustomValidatingPointerStruct struct {
Value string
}
func (c *CustomValidatingPointerStruct) Validate() gperr.Error {
if c == nil {
return gperr.New("pointer struct cannot be nil")
}
if c.Value == "" {
return gperr.New("value cannot be empty")
}
if len(c.Value) < 3 {
return gperr.New("value must be at least 3 characters")
}
return nil
}
func TestValidateWithCustomValidator_CustomValidatingPointerStructValue(t *testing.T) {
tests := []struct {
name string
input CustomValidatingPointerStruct
wantErr bool
}{
{"custom validating pointer struct as value - valid", CustomValidatingPointerStruct{Value: "hello"}, false},
{"custom validating pointer struct as value - empty", CustomValidatingPointerStruct{Value: ""}, false},
{"custom validating pointer struct as value - short", CustomValidatingPointerStruct{Value: "hi"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingPointerStructPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingPointerStruct
wantErr bool
}{
{"valid custom validating pointer struct", &CustomValidatingPointerStruct{Value: "hello"}, false},
{"nil custom validating pointer struct", nil, true}, // Should fail because Validate() checks for nil
{"invalid custom validating pointer struct - empty", &CustomValidatingPointerStruct{Value: ""}, true},
{"invalid custom validating pointer struct - too short", &CustomValidatingPointerStruct{Value: "hi"}, true},
{"valid custom validating pointer struct - minimum length", &CustomValidatingPointerStruct{Value: "abc"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -0,0 +1,66 @@
package serialization
import (
"reflect"
"testing"
gperr "github.com/yusing/goutils/errs"
)
type CustomValidatingStruct struct {
Value string
}
func (c CustomValidatingStruct) Validate() gperr.Error {
if c.Value == "" {
return gperr.New("value cannot be empty")
}
if len(c.Value) < 3 {
return gperr.New("value must be at least 3 characters")
}
return nil
}
func TestValidateWithCustomValidator_Struct(t *testing.T) {
tests := []struct {
name string
input CustomValidatingStruct
wantErr bool
}{
{"valid custom validating struct", CustomValidatingStruct{Value: "hello"}, false},
{"invalid custom validating struct - empty", CustomValidatingStruct{Value: ""}, true},
{"invalid custom validating struct - too short", CustomValidatingStruct{Value: "hi"}, true},
{"valid custom validating struct - minimum length", CustomValidatingStruct{Value: "abc"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingStructPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingStruct
wantErr bool
}{
{"valid custom validating struct pointer", &CustomValidatingStruct{Value: "hello"}, false},
{"nil custom validating struct pointer", nil, true},
{"invalid custom validating struct pointer - empty", &CustomValidatingStruct{Value: ""}, true},
{"invalid custom validating struct pointer - too short", &CustomValidatingStruct{Value: "hi"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@@ -0,0 +1 @@
package serialization