// Copyright (c) 2025-2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package validator import ( "strings" "testing" "go.probo.inc/probo/pkg/gid" ) func TestURL(t *testing.T) { tests := []struct { name string value any wantError bool }{ {"valid http URL", "http://example.com", false}, {"valid https URL", "https://example.com", false}, {"valid URL with path", "https://example.com/path", false}, {"invalid scheme", "ftp://example.com", true}, {"no scheme", "example.com", true}, {"no host", "https://", true}, {"empty string", "", false}, // Empty is allowed {"nil pointer", (*string)(nil), false}, {"non-string", 123, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := URL()(tt.value) if (err != nil) != tt.wantError { t.Errorf("URL() error = %v, wantError %v", err, tt.wantError) } if err != nil && err.Code != ErrorCodeInvalidURL { t.Errorf("Expected error code %s, got %s", ErrorCodeInvalidURL, err.Code) } }) } } func TestHTTPSUrl(t *testing.T) { t.Run("valid https URL", func(t *testing.T) { str := "https://example.com" err := HTTPSUrl()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid https URL with path", func(t *testing.T) { str := "https://example.com/path/to/resource" err := HTTPSUrl()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid https URL with query", func(t *testing.T) { str := "https://api.example.com/v1/users?page=1" err := HTTPSUrl()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("invalid - http scheme", func(t *testing.T) { str := "http://example.com" err := HTTPSUrl()(&str) if err == nil { t.Fatal("expected validation error for http") } else if err.Message != "URL must use https scheme" { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("invalid - ftp scheme", func(t *testing.T) { str := "ftp://example.com" err := HTTPSUrl()(&str) if err == nil { t.Error("expected validation error for ftp") } }) t.Run("invalid - no scheme", func(t *testing.T) { str := "example.com" err := HTTPSUrl()(&str) if err == nil { t.Error("expected validation error for missing scheme") } }) t.Run("invalid - no host", func(t *testing.T) { str := "https://" err := HTTPSUrl()(&str) if err == nil { t.Error("expected validation error for missing host") } }) t.Run("empty string", func(t *testing.T) { str := "" err := HTTPSUrl()(&str) if err != nil { t.Errorf("expected no error for empty string, got: %v", err) } }) t.Run("nil pointer", func(t *testing.T) { var str *string err := HTTPSUrl()(str) if err != nil { t.Errorf("expected no error for nil, got: %v", err) } }) } func TestOrigin(t *testing.T) { tests := []struct { name string value any wantError bool }{ {"valid https origin", "https://example.com", false}, {"valid http origin", "http://example.com", false}, {"valid with port", "http://localhost:3000", false}, {"valid https with port", "https://example.com:8443", false}, {"valid with trailing slash", "https://example.com/", false}, {"invalid - has path", "https://example.com/path", true}, {"invalid - has query", "https://example.com?q=1", true}, {"invalid - has fragment", "https://example.com#section", true}, {"invalid - has userinfo", "https://user:pass@example.com", true}, {"invalid - no scheme", "example.com", true}, {"invalid - ftp scheme", "ftp://example.com", true}, {"invalid - no host", "https://", true}, {"empty string", "", false}, {"nil pointer", (*string)(nil), false}, {"non-string", 123, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := Origin()(tt.value) if (err != nil) != tt.wantError { t.Errorf("Origin() error = %v, wantError %v", err, tt.wantError) } if err != nil && err.Code != ErrorCodeInvalidFormat { t.Errorf("Expected error code %s, got %s", ErrorCodeInvalidFormat, err.Code) } }) } } func TestSlug(t *testing.T) { tests := []struct { name string value any maxLen int wantError bool wantCode ErrorCode }{ {"valid simple slug", "analytics", 100, false, ""}, {"valid with hyphens", "my-category", 100, false, ""}, {"valid multi-segment", "my-cool-category", 100, false, ""}, {"valid single char", "a", 100, false, ""}, {"valid digits only", "123", 100, false, ""}, {"valid mixed", "cat2", 100, false, ""}, {"valid digit-hyphen-alpha", "1-a", 100, false, ""}, {"invalid - uppercase", "Analytics", 100, true, ErrorCodeInvalidFormat}, {"invalid - leading hyphen", "-analytics", 100, true, ErrorCodeInvalidFormat}, {"invalid - trailing hyphen", "analytics-", 100, true, ErrorCodeInvalidFormat}, {"invalid - consecutive hyphens", "my--category", 100, true, ErrorCodeInvalidFormat}, {"invalid - underscore", "my_category", 100, true, ErrorCodeInvalidFormat}, {"invalid - spaces", "my category", 100, true, ErrorCodeInvalidFormat}, {"invalid - special chars", "my@category", 100, true, ErrorCodeInvalidFormat}, {"invalid - dot", "my.category", 100, true, ErrorCodeInvalidFormat}, {"too long", "abcdefghijk", 10, true, ErrorCodeTooLong}, {"exactly max length", "abcdefghij", 10, false, ""}, {"empty string", "", 100, false, ""}, {"nil pointer", (*string)(nil), 100, false, ""}, {"non-string", 123, 100, true, ErrorCodeInvalidFormat}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := Slug(tt.maxLen)(tt.value) if (err != nil) != tt.wantError { t.Errorf("Slug(%d) error = %v, wantError %v", tt.maxLen, err, tt.wantError) } if err != nil && tt.wantCode != "" && err.Code != tt.wantCode { t.Errorf("Expected error code %s, got %s", tt.wantCode, err.Code) } }) } } func TestDomain(t *testing.T) { t.Run("valid domain", func(t *testing.T) { str := "example.com" err := Domain()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid subdomain", func(t *testing.T) { str := "api.example.com" err := Domain()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid nested subdomain", func(t *testing.T) { str := "api.v1.example.com" err := Domain()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid domain with hyphens", func(t *testing.T) { str := "my-api.example-site.com" err := Domain()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("single word domain", func(t *testing.T) { str := "localhost" err := Domain()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("invalid - starts with hyphen", func(t *testing.T) { str := "-example.com" err := Domain()(&str) if err == nil { t.Error("expected validation error for domain starting with hyphen") } }) t.Run("invalid - ends with hyphen", func(t *testing.T) { str := "example-.com" err := Domain()(&str) if err == nil { t.Error("expected validation error for domain ending with hyphen") } }) t.Run("invalid - contains underscore", func(t *testing.T) { str := "example_site.com" err := Domain()(&str) if err == nil { t.Error("expected validation error for underscore") } }) t.Run("invalid - contains spaces", func(t *testing.T) { str := "example site.com" err := Domain()(&str) if err == nil { t.Error("expected validation error for spaces") } }) t.Run("invalid - empty label", func(t *testing.T) { str := "example..com" err := Domain()(&str) if err == nil { t.Error("expected validation error for empty label") } }) t.Run("invalid - too long", func(t *testing.T) { str := strings.Repeat("a", 254) err := Domain()(&str) if err == nil { t.Fatal("expected validation error for domain too long") } else if err.Message != "domain name too long (max 253 characters)" { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("empty string", func(t *testing.T) { str := "" err := Domain()(&str) if err != nil { t.Errorf("expected no error for empty string, got: %v", err) } }) t.Run("nil pointer", func(t *testing.T) { var str *string err := Domain()(str) if err != nil { t.Errorf("expected no error for nil, got: %v", err) } }) } func TestGID(t *testing.T) { // Create a valid GID for testing tenantID := gid.TenantID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}) validGID := gid.New(tenantID, 100) t.Run("valid GID type - no entity type validation", func(t *testing.T) { err := GID()(validGID) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid GID type - with matching entity type", func(t *testing.T) { err := GID(100)(validGID) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid GID type - with multiple entity types", func(t *testing.T) { err := GID(100, 200, 300)(validGID) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("invalid - wrong entity type", func(t *testing.T) { err := GID(200)(validGID) if err == nil { t.Fatal("expected validation error for wrong entity type") } else if err.Code != ErrorCodeInvalidGID { t.Errorf("expected error code %s, got %s", ErrorCodeInvalidGID, err.Code) } else if err.Message != "GID has invalid entity type" { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("invalid - wrong entity type with multiple options", func(t *testing.T) { err := GID(200, 300)(validGID) if err == nil { t.Error("expected validation error for wrong entity type") } }) t.Run("valid - entity type matches one of multiple options", func(t *testing.T) { err := GID(99, 100, 101)(validGID) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("nil GID pointer", func(t *testing.T) { var gidPtr *gid.GID err := GID()(gidPtr) if err != nil { t.Errorf("expected no error for nil GID pointer, got: %v", err) } }) t.Run("valid GID pointer", func(t *testing.T) { err := GID()(&validGID) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid GID pointer with entity type validation", func(t *testing.T) { err := GID(100)(&validGID) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("non-GID type", func(t *testing.T) { err := GID()(123) if err == nil { t.Fatal("expected validation error for non-GID type") } else if err.Message != "value must be a GID" { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("string type not supported", func(t *testing.T) { err := GID()("some-string") if err == nil { t.Fatal("expected validation error for string type") } else if err.Message != "value must be a GID" { t.Errorf("unexpected error message: %s", err.Message) } }) }