validation_optional_test.go
package utils
import "testing"
func TestValidateOptionalShortID(t *testing.T) {
t.Run("nil pointer returns nil", func(t *testing.T) {
if err := ValidateOptionalShortID(nil, "test_field"); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("empty string returns nil", func(t *testing.T) {
empty := ""
if err := ValidateOptionalShortID(&empty, "test_field"); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("valid ID returns nil", func(t *testing.T) {
valid := "abc123def456"
if err := ValidateOptionalShortID(&valid, "test_field"); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("invalid ID returns error", func(t *testing.T) {
bad := "too-short"
if err := ValidateOptionalShortID(&bad, "test_field"); err == nil {
t.Error("expected error for invalid ID")
}
})
}
func TestValidateOptionalStringLength(t *testing.T) {
t.Run("nil pointer returns nil", func(t *testing.T) {
if err := ValidateOptionalStringLength(nil, "test_field", 10); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("empty string returns nil", func(t *testing.T) {
empty := ""
if err := ValidateOptionalStringLength(&empty, "test_field", 10); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("short enough returns nil", func(t *testing.T) {
s := "hello"
if err := ValidateOptionalStringLength(&s, "test_field", 10); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("too long returns error", func(t *testing.T) {
s := "this string is way too long"
if err := ValidateOptionalStringLength(&s, "test_field", 5); err == nil {
t.Error("expected error for string too long")
}
})
}