@@ -3,7 +3,9 @@ package validator
33import (
44 "bytes"
55 "context"
6+ "net"
67 "database/sql"
8+ "runtime"
79 "database/sql/driver"
810 "encoding/base64"
911 "encoding/json"
@@ -12,11 +14,9 @@ import (
1214 "image"
1315 "image/jpeg"
1416 "image/png"
15- "net"
1617 "os"
1718 "path/filepath"
1819 "reflect"
19- "runtime"
2020 "strings"
2121 "testing"
2222 "time"
@@ -792,7 +792,7 @@ func TestStructPartial(t *testing.T) {
792792
793793 // the following should all return no errors as everything is valid in
794794 // the default state
795- errs := validate .StructPartialCtx (t . Context (), tPartial , p1 ... )
795+ errs := validate .StructPartialCtx (context . Background (), tPartial , p1 ... )
796796 Equal (t , errs , nil )
797797
798798 errs = validate .StructPartial (tPartial , p2 ... )
@@ -802,7 +802,7 @@ func TestStructPartial(t *testing.T) {
802802 errs = validate .StructPartial (tPartial .SubSlice [0 ], p3 ... )
803803 Equal (t , errs , nil )
804804
805- errs = validate .StructExceptCtx (t . Context (), tPartial , p1 ... )
805+ errs = validate .StructExceptCtx (context . Background (), tPartial , p1 ... )
806806 Equal (t , errs , nil )
807807
808808 errs = validate .StructExcept (tPartial , p2 ... )
@@ -1040,7 +1040,7 @@ func TestCrossStructLteFieldValidation(t *testing.T) {
10401040 AssertError (t , errs , "Test.Float" , "Test.Float" , "Float" , "Float" , "ltecsfield" )
10411041 AssertError (t , errs , "Test.Array" , "Test.Array" , "Array" , "Array" , "ltecsfield" )
10421042
1043- errs = validate .VarWithValueCtx (t . Context (), 1 , "" , "ltecsfield" )
1043+ errs = validate .VarWithValueCtx (context . Background (), 1 , "" , "ltecsfield" )
10441044 NotEqual (t , errs , nil )
10451045 AssertError (t , errs , "" , "" , "" , "" , "ltecsfield" )
10461046
@@ -2233,7 +2233,7 @@ func TestSQLValue2Validation(t *testing.T) {
22332233 AssertError (t , errs , "" , "" , "" , "" , "required" )
22342234
22352235 val .Name = "Valid Name"
2236- errs = validate .VarCtx (t . Context (), val , "required" )
2236+ errs = validate .VarCtx (context . Background (), val , "required" )
22372237 Equal (t , errs , nil )
22382238
22392239 val .Name = "errorme"
@@ -2953,25 +2953,23 @@ func TestUnixAddrValidation(t *testing.T) {
29532953}
29542954
29552955func TestUnixDomainSocketExistsValidation (t * testing.T ) {
2956+ if runtime .GOOS == "windows" {
2957+ t .Skip ("Unix domain sockets are not supported on Windows" )
2958+ }
2959+
29562960 validate := New ()
29572961
2958- // Test with empty string - should fail
29592962 errs := validate .Var ("" , "uds_exists" )
29602963 NotEqual (t , errs , nil )
29612964 AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
29622965
2963- // Test with non-existent path - should fail
29642966 errs = validate .Var ("/tmp/nonexistent.sock" , "uds_exists" )
29652967 NotEqual (t , errs , nil )
29662968 AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
29672969
2968- // Test with a real socket file
2969- // Create a temporary socket file for testing
2970- // Use /tmp directly to avoid path length issues on macOS
29712970 sockPath := "/tmp/test_validator.sock"
2972-
2973- // Create a Unix domain socket
2974- listener , err := net .Listen ("unix" , sockPath )
2971+ var lc net.ListenConfig
2972+ listener , err := lc .Listen (t .Context (), "unix" , sockPath )
29752973 if err != nil {
29762974 t .Fatalf ("Failed to create test socket: %v" , err )
29772975 }
@@ -2981,57 +2979,43 @@ func TestUnixDomainSocketExistsValidation(t *testing.T) {
29812979 defer func () {
29822980 _ = os .Remove (sockPath )
29832981 }()
2984-
2985- // Test with existing socket - should pass
29862982 errs = validate .Var (sockPath , "uds_exists" )
29872983 Equal (t , errs , nil )
29882984
2989- // Test with regular file (not a socket) - should fail
29902985 regularFile := "/tmp/test_validator_regular.txt"
29912986 if err := os .WriteFile (regularFile , []byte ("test" ), 0644 ); err != nil {
29922987 t .Fatalf ("Failed to create regular file: %v" , err )
29932988 }
29942989 defer func () {
29952990 _ = os .Remove (regularFile )
29962991 }()
2997-
29982992 errs = validate .Var (regularFile , "uds_exists" )
29992993 NotEqual (t , errs , nil )
30002994 AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
30012995
3002- // Test with directory - should fail
30032996 dirPath := "/tmp/test_validator_dir"
30042997 if err := os .Mkdir (dirPath , 0755 ); err != nil && ! os .IsExist (err ) {
30052998 t .Fatalf ("Failed to create directory: %v" , err )
30062999 }
30073000 defer func () {
30083001 _ = os .RemoveAll (dirPath )
30093002 }()
3010-
30113003 errs = validate .Var (dirPath , "uds_exists" )
30123004 NotEqual (t , errs , nil )
30133005 AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
30143006
3015- // Linux-specific test for abstract sockets
30163007 if runtime .GOOS == "linux" {
3017- // Create an abstract socket (prefixed with @)
30183008 abstractSockName := "@test_abstract_socket_" + fmt .Sprintf ("%d" , time .Now ().UnixNano ())
3019-
3020- // In Go, abstract sockets are created by using a null byte prefix
3021- // but for testing, we need to use the actual implementation
3022- abstractListener , err := net .Listen ("unix" , "\x00 " + abstractSockName [1 :])
3009+ var lc net.ListenConfig
3010+ abstractListener , err := lc .Listen (t .Context (), "unix" , "\x00 " + abstractSockName [1 :])
30233011 if err != nil {
30243012 t .Fatalf ("Failed to create abstract socket: %v" , err )
30253013 }
30263014 defer func () {
30273015 _ = abstractListener .Close ()
30283016 }()
3029-
3030- // Test with existing abstract socket - should pass
30313017 errs = validate .Var (abstractSockName , "uds_exists" )
30323018 Equal (t , errs , nil )
3033-
3034- // Test with non-existent abstract socket - should fail
30353019 errs = validate .Var ("@nonexistent_abstract_socket" , "uds_exists" )
30363020 NotEqual (t , errs , nil )
30373021 AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
@@ -9840,7 +9824,7 @@ func TestStructFiltered(t *testing.T) {
98409824
98419825 // the following should all return no errors as everything is valid in
98429826 // the default state
9843- errs := validate .StructFilteredCtx (t . Context (), tPartial , p1 )
9827+ errs := validate .StructFilteredCtx (context . Background (), tPartial , p1 )
98449828 Equal (t , errs , nil )
98459829
98469830 errs = validate .StructFiltered (tPartial , p2 )
@@ -10253,7 +10237,7 @@ func TestValidateStructRegisterCtx(t *testing.T) {
1025310237
1025410238 validate .RegisterStructValidationCtx (slFn , Test {})
1025510239
10256- ctx := context .WithValue (t . Context (), & ctxVal , "testval" )
10240+ ctx := context .WithValue (context . Background (), & ctxVal , "testval" )
1025710241 ctx = context .WithValue (ctx , & ctxSlVal , "slVal" )
1025810242 errs := validate .StructCtx (ctx , tst )
1025910243 Equal (t , errs , nil )
@@ -13688,7 +13672,7 @@ func TestValidate_ValidateMapCtx(t *testing.T) {
1368813672 for _ , tt := range tests {
1368913673 t .Run (tt .name , func (t * testing.T ) {
1369013674 validate := New ()
13691- if got := validate .ValidateMapCtx (t . Context (), tt .args .data , tt .args .rules ); len (got ) != tt .want {
13675+ if got := validate .ValidateMapCtx (context . Background (), tt .args .data , tt .args .rules ); len (got ) != tt .want {
1369213676 t .Errorf ("ValidateMapCtx() = %v, want %v" , got , tt .want )
1369313677 }
1369413678 })
@@ -13759,7 +13743,7 @@ func TestValidate_ValidateMapCtxWithKeys(t *testing.T) {
1375913743 for _ , tt := range tests {
1376013744 t .Run (tt .name , func (t * testing.T ) {
1376113745 validate := New ()
13762- errs := validate .ValidateMapCtx (t . Context (), tt .args .data , tt .args .rules )
13746+ errs := validate .ValidateMapCtx (context . Background (), tt .args .data , tt .args .rules )
1376313747 NotEqual (t , errs , nil )
1376413748 Equal (t , len (errs ), tt .want )
1376513749 for key , err := range errs {
@@ -13781,7 +13765,7 @@ func TestValidate_VarWithKey(t *testing.T) {
1378113765
1378213766func TestValidate_VarWithKeyCtx (t * testing.T ) {
1378313767 validate := New ()
13784- errs := validate .VarWithKeyCtx (t . Context (), "age" , 15 , "required,gt=16" )
13768+ errs := validate .VarWithKeyCtx (context . Background (), "age" , 15 , "required,gt=16" )
1378513769 NotEqual (t , errs , nil )
1378613770 AssertError (t , errs , "age" , "age" , "age" , "age" , "gt" )
1378713771
0 commit comments