Skip to content

Commit e078e96

Browse files
committed
Fixed nilness issues
1 parent a42e2b6 commit e078e96

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

examples/model/customer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *Customer) Valid() (bool, []validate.ValidationError) {
3535

3636
//Showing use of a public validation method (extra validations outside of the struct built-in validations)
3737
ok, err := validate.IsValidSocial(c.SocialSecurityNumber)
38-
if !ok {
38+
if !ok && err != nil {
3939
errs = append(errs, validate.ValidationError{
4040
Key: "SocialSecurityNumber",
4141
Message: err.Error(),

extra_validations_test.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,32 @@ func TestIsValidSocialErrorResponses(t *testing.T) {
7575
var err error
7676

7777
testSocialNumber := "" //Empty
78-
if _, err = IsValidSocial(testSocialNumber); err.Error() != "social is empty" {
78+
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social is empty" {
7979
t.Fatal("Error response was not as expected - Value: " + err.Error())
8080
}
8181

8282
testSocialNumber = "2123" //Not long enough
83-
if _, err = IsValidSocial(testSocialNumber); err.Error() != "social is not nine digits in length" {
83+
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social is not nine digits in length" {
8484
t.Fatal("Error response was not as expected - Value: " + err.Error())
8585
}
8686

8787
testSocialNumber = "000-00-0000" //All zeros
88-
if _, err = IsValidSocial(testSocialNumber); err.Error() != "social section was found invalid (cannot be 000 or 666)" {
88+
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social section was found invalid (cannot be 000 or 666)" {
8989
t.Fatal("Error response was not as expected - Value: " + err.Error())
9090
}
9191

9292
testSocialNumber = "1234-1-2342" //Invalid Pattern
93-
if _, err = IsValidSocial(testSocialNumber); err.Error() != "social does not match the regex pattern" {
93+
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social does not match the regex pattern" {
9494
t.Fatal("Error response was not as expected - Value: " + err.Error())
9595
}
9696

9797
testSocialNumber = "000-00-2345" //Invalid section
98-
if _, err = IsValidSocial(testSocialNumber); err.Error() != "social section was found invalid (cannot be 000 or 666)" {
98+
if _, err = IsValidSocial(testSocialNumber); err != nil && err.Error() != "social section was found invalid (cannot be 000 or 666)" {
9999
t.Fatal("Error response was not as expected - Value: " + err.Error())
100100
}
101101

102102
for _, ssn := range blacklistedSocials { //Blacklisted
103-
if _, err = IsValidSocial(ssn); err.Error() != "social was found to be blacklisted" {
103+
if _, err = IsValidSocial(ssn); err != nil && err.Error() != "social was found to be blacklisted" {
104104
t.Fatal("Error response was not as expected - Value: " + err.Error())
105105
}
106106
}
@@ -322,33 +322,33 @@ func TestIsValidEmailErrorResponses(t *testing.T) {
322322
var err error
323323

324324
email := "test"
325-
if _, err = IsValidEmail(email, false); err.Error() != "email length is invalid" {
325+
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email length is invalid" {
326326
t.Fatal("Error response was not as expected - Value: " + err.Error())
327327
}
328328

329329
email = "test@"
330-
if _, err = IsValidEmail(email, false); err.Error() != "email is not a valid address format" {
330+
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email is not a valid address format" {
331331
t.Fatal("Error response was not as expected - Value: " + err.Error())
332332
}
333333

334334
email = "test.some"
335-
if _, err = IsValidEmail(email, false); err.Error() != "email is not a valid address format" {
335+
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email is not a valid address format" {
336336
t.Fatal("Error response was not as expected - Value: " + err.Error())
337337
}
338338

339339
email = "@this.com"
340-
if _, err = IsValidEmail(email, false); err.Error() != "email is not a valid address format" {
340+
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email is not a valid address format" {
341341
t.Fatal("Error response was not as expected - Value: " + err.Error())
342342
}
343343

344344
email = "1234567890123456789012345678901234567890123456789012345678901234567890@this.com"
345-
if _, err = IsValidEmail(email, false); err.Error() != "email length is invalid" {
345+
if _, err = IsValidEmail(email, false); err != nil && err.Error() != "email length is invalid" {
346346
t.Fatal("Error response was not as expected - Value: " + err.Error())
347347
}
348348

349349
//Test all blacklisted hosts and errors
350350
for _, host := range blacklistedDomains {
351-
if _, err = IsValidEmail("someone@"+host, false); err.Error() != "email domain is not accepted" {
351+
if _, err = IsValidEmail("someone@"+host, false); err != nil && err.Error() != "email domain is not accepted" {
352352
t.Fatal("Error response was not as expected - Value: " + err.Error())
353353
}
354354
}
@@ -358,7 +358,7 @@ func TestIsValidEmailErrorResponses(t *testing.T) {
358358

359359
//email domain invalid/cannot receive mail: lookup gmail.conn on 169.254.169.254:53: no such host
360360
//if err.Error() != "email domain invalid/cannot receive mail: lookup gmail.conn: no such host" {
361-
if !strings.Contains(err.Error(), "email domain invalid/cannot receive mail:") {
361+
if err != nil && !strings.Contains(err.Error(), "email domain invalid/cannot receive mail:") {
362362
t.Fatal("Error response was not as expected - Value: " + err.Error())
363363
}
364364
}
@@ -397,7 +397,7 @@ func TestIsValidEnum(t *testing.T) {
397397
testAcceptedValues := []string{"123"}
398398
if ok, err = IsValidEnum(testEnumValue, &testAcceptedValues, false); ok {
399399
t.Fatal("This should have failed - value is not found", testEnumValue, testAcceptedValues, err)
400-
} else if err.Error() != "value "+testEnumValue+" is not allowed" {
400+
} else if err != nil && err.Error() != "value "+testEnumValue+" is not allowed" {
401401
t.Fatal("error message was not as expected", err.Error())
402402
}
403403

@@ -458,7 +458,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
458458

459459
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
460460
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
461-
} else if err.Error() != "country code length is invalid" {
461+
} else if err != nil && err.Error() != "country code length is invalid" {
462462
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
463463
}
464464

@@ -467,7 +467,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
467467

468468
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
469469
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
470-
} else if err.Error() != "country code length is invalid" {
470+
} else if err != nil && err.Error() != "country code length is invalid" {
471471
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
472472
}
473473

@@ -476,7 +476,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
476476

477477
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
478478
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
479-
} else if err.Error() != "country code 32 is not accepted" {
479+
} else if err != nil && err.Error() != "country code 32 is not accepted" {
480480
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
481481
}
482482

@@ -485,7 +485,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
485485

486486
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
487487
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
488-
} else if err.Error() != "phone number length is invalid" {
488+
} else if err != nil && err.Error() != "phone number length is invalid" {
489489
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
490490
}
491491

@@ -495,7 +495,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
495495

496496
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
497497
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
498-
} else if err.Error() != "phone number must be ten digits" {
498+
} else if err != nil && err.Error() != "phone number must be ten digits" {
499499
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
500500
}
501501

@@ -505,7 +505,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
505505

506506
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
507507
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
508-
} else if err.Error() != "phone number must be either eight or ten digits" {
508+
} else if err != nil && err.Error() != "phone number must be either eight or ten digits" {
509509
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
510510
}
511511

@@ -515,7 +515,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
515515

516516
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
517517
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
518-
} else if err.Error() != "phone number must be ten digits" {
518+
} else if err != nil && err.Error() != "phone number must be ten digits" {
519519
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
520520
}
521521

@@ -525,7 +525,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
525525

526526
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
527527
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
528-
} else if err.Error() != "phone number NPA cannot start with 1" {
528+
} else if err != nil && err.Error() != "phone number NPA cannot start with 1" {
529529
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
530530
}
531531

@@ -535,7 +535,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
535535

536536
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
537537
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
538-
} else if err.Error() != "phone number NPA cannot start with 0" {
538+
} else if err != nil && err.Error() != "phone number NPA cannot start with 0" {
539539
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
540540
}
541541

@@ -545,7 +545,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
545545

546546
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
547547
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
548-
} else if err.Error() != "phone number NPA cannot start with 1" {
548+
} else if err != nil && err.Error() != "phone number NPA cannot start with 1" {
549549
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
550550
}
551551

@@ -555,7 +555,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
555555

556556
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
557557
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
558-
} else if err.Error() != "phone number NPA cannot start with 0" {
558+
} else if err != nil && err.Error() != "phone number NPA cannot start with 0" {
559559
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
560560
}
561561

@@ -565,7 +565,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
565565

566566
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
567567
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
568-
} else if err.Error() != "phone number NPA cannot start with 555" {
568+
} else if err != nil && err.Error() != "phone number NPA cannot start with 555" {
569569
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
570570
}
571571

@@ -575,7 +575,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
575575

576576
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
577577
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
578-
} else if err.Error() != "phone number NXX cannot start with 1" {
578+
} else if err != nil && err.Error() != "phone number NXX cannot start with 1" {
579579
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
580580
}
581581

@@ -585,7 +585,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
585585

586586
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
587587
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
588-
} else if err.Error() != "phone number NXX cannot start with 0" {
588+
} else if err != nil && err.Error() != "phone number NXX cannot start with 0" {
589589
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
590590
}
591591

@@ -595,7 +595,7 @@ func TestIsValidPhoneNumber(t *testing.T) {
595595

596596
if ok, err = IsValidPhoneNumber(phone, countryCode); ok {
597597
t.Fatal("This should have failed - phone is invalid for USA", phone, countryCode, err)
598-
} else if err.Error() != "phone number NXX cannot be X11" {
598+
} else if err != nil && err.Error() != "phone number NXX cannot be X11" {
599599
t.Fatal("error message was not as expected", phone, countryCode, err.Error())
600600
}
601601

0 commit comments

Comments
 (0)