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

@@ -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")
}
}