@@ -7119,3 +7119,97 @@ func TestValidateStructRegisterCtx(t *testing.T) {
71197119 Equal (t , ctxVal , "testval" )
71207120 Equal (t , ctxSlVal , "slVal" )
71217121}
7122+
7123+ func TestHostnameValidation (t * testing.T ) {
7124+ tests := []struct {
7125+ param string
7126+ expected bool
7127+ }{
7128+ {"test.example.com" , true },
7129+ {"example.com" , true },
7130+ {"example24.com" , true },
7131+ {"test.example24.com" , true },
7132+ {"test24.example24.com" , true },
7133+ {"example" , true },
7134+ {"test.example.com." , false },
7135+ {"example.com." , false },
7136+ {"example24.com." , false },
7137+ {"test.example24.com." , false },
7138+ {"test24.example24.com." , false },
7139+ {"example." , false },
7140+ {"192.168.0.1" , false },
7141+ 7142+ {"2001:cdba:0000:0000:0000:0000:3257:9652" , false },
7143+ {"2001:cdba:0:0:0:0:3257:9652" , false },
7144+ {"2001:cdba::3257:9652" , false },
7145+ }
7146+
7147+ validate := New ()
7148+
7149+ for i , test := range tests {
7150+
7151+ errs := validate .Var (test .param , "hostname" )
7152+
7153+ if test .expected {
7154+ if ! IsEqual (errs , nil ) {
7155+ t .Fatalf ("Index: %d hostname failed Error: %s" , i , errs )
7156+ }
7157+ } else {
7158+ if IsEqual (errs , nil ) {
7159+ t .Fatalf ("Index: %d hostname failed Error: %s" , i , errs )
7160+ } else {
7161+ val := getError (errs , "" , "" )
7162+ if val .Tag () != "hostname" {
7163+ t .Fatalf ("Index: %d hostname failed Error: %s" , i , errs )
7164+ }
7165+ }
7166+ }
7167+ }
7168+ }
7169+
7170+ func TestFQDNValidation (t * testing.T ) {
7171+ tests := []struct {
7172+ param string
7173+ expected bool
7174+ }{
7175+ {"test.example.com" , true },
7176+ {"example.com" , true },
7177+ {"example24.com" , true },
7178+ {"test.example24.com" , true },
7179+ {"test24.example24.com" , true },
7180+ {"test.example.com." , true },
7181+ {"example.com." , true },
7182+ {"example24.com." , true },
7183+ {"test.example24.com." , true },
7184+ {"test24.example24.com." , true },
7185+ {"test24.example24.com.." , false },
7186+ {"example" , false },
7187+ {"192.168.0.1" , false },
7188+ 7189+ {"2001:cdba:0000:0000:0000:0000:3257:9652" , false },
7190+ {"2001:cdba:0:0:0:0:3257:9652" , false },
7191+ {"2001:cdba::3257:9652" , false },
7192+ }
7193+
7194+ validate := New ()
7195+
7196+ for i , test := range tests {
7197+
7198+ errs := validate .Var (test .param , "fqdn" )
7199+
7200+ if test .expected {
7201+ if ! IsEqual (errs , nil ) {
7202+ t .Fatalf ("Index: %d fqdn failed Error: %s" , i , errs )
7203+ }
7204+ } else {
7205+ if IsEqual (errs , nil ) {
7206+ t .Fatalf ("Index: %d fqdn failed Error: %s" , i , errs )
7207+ } else {
7208+ val := getError (errs , "" , "" )
7209+ if val .Tag () != "fqdn" {
7210+ t .Fatalf ("Index: %d fqdn failed Error: %s" , i , errs )
7211+ }
7212+ }
7213+ }
7214+ }
7215+ }
0 commit comments