@@ -8622,66 +8622,92 @@ func TestRgb(t *testing.T) {
8622
8622
func TestEmail (t * testing.T ) {
8623
8623
validate := New ()
8624
8624
8625
-
8626
- errs := validate .Var (s , "email" )
8627
- Equal (t , errs , nil )
8628
-
8629
- s = "Dörte@Sörensen.example.com"
8630
- errs = validate .Var (s , "email" )
8631
- Equal (t , errs , nil )
8632
-
8633
- s = "θσερ@εχαμπλε.ψομ"
8634
- errs = validate .Var (s , "email" )
8635
- Equal (t , errs , nil )
8636
-
8637
- s = "юзер@екзампл.ком"
8638
- errs = validate .Var (s , "email" )
8639
- Equal (t , errs , nil )
8640
-
8641
- s = "उपयोगकर्ता@उदाहरण.कॉम"
8642
- errs = validate .Var (s , "email" )
8643
- Equal (t , errs , nil )
8644
-
8645
- s = "用户@例子.广告"
8646
- errs = validate .Var (s , "email" )
8647
- Equal (t , errs , nil )
8648
-
8649
- s = "mail@domain_with_underscores.org"
8650
- errs = validate .Var (s , "email" )
8651
- NotEqual (t , errs , nil )
8652
- AssertError (t , errs , "" , "" , "" , "" , "email" )
8653
-
8654
- s = ""
8655
- errs = validate .Var (s , "email" )
8656
- NotEqual (t , errs , nil )
8657
- AssertError (t , errs , "" , "" , "" , "" , "email" )
8658
-
8659
- s = "test@email"
8660
- errs = validate .Var (s , "email" )
8661
- NotEqual (t , errs , nil )
8662
- AssertError (t , errs , "" , "" , "" , "" , "email" )
8663
-
8664
- s = "test@email."
8665
- errs = validate .Var (s , "email" )
8666
- NotEqual (t , errs , nil )
8667
- AssertError (t , errs , "" , "" , "" , "" , "email" )
8668
-
8669
- s = "@email.com"
8670
- errs = validate .Var (s , "email" )
8671
- NotEqual (t , errs , nil )
8672
- AssertError (t , errs , "" , "" , "" , "" , "email" )
8673
-
8674
- s = `"test test"@email.com`
8675
- errs = validate .Var (s , "email" )
8676
- Equal (t , errs , nil )
8625
+ tests := []struct {
8626
+ input string
8627
+ expectError bool
8628
+ }{
8629
+ {
8630
+
8631
+ expectError : false ,
8632
+ },
8633
+ {
8634
+ input : "Dörte@Sörensen.example.com" ,
8635
+ expectError : false ,
8636
+ },
8637
+ {
8638
+ input : "θσερ@εχαμπλε.ψομ" ,
8639
+ expectError : false ,
8640
+ },
8641
+ {
8642
+ input : "юзер@екзампл.ком" ,
8643
+ expectError : false ,
8644
+ },
8645
+ {
8646
+ input : "उपयोगकर्ता@उदाहरण.कॉम" ,
8647
+ expectError : false ,
8648
+ },
8649
+ {
8650
+ input : "用户@例子.广告" ,
8651
+ expectError : false ,
8652
+ },
8653
+ {
8654
+
8655
+ expectError : false ,
8656
+ },
8657
+ {
8658
+
8659
+ expectError : false ,
8660
+ },
8661
+ {
8662
+ input : "mail@domain_with_underscores.org" ,
8663
+ expectError : true ,
8664
+ },
8665
+ {
8666
+ // empty is not a valid email
8667
+ input : "" ,
8668
+ expectError : true ,
8669
+ },
8670
+ {
8671
+ input : "test@email" ,
8672
+ expectError : true ,
8673
+ },
8674
+ {
8675
+ input : "test@email." ,
8676
+ expectError : true ,
8677
+ },
8678
+ {
8679
+ input : "@email.com" ,
8680
+ expectError : true ,
8681
+ },
8682
+ {
8683
+ input : `"test test"@email.com` ,
8684
+ expectError : false ,
8685
+ },
8686
+ {
8687
+ input : `"@email.com` ,
8688
+ expectError : true ,
8689
+ },
8690
+ {
8691
+ // per RFC 952, TLD can contain number, but there is no valid TLD as of June 2024 that ends with number.
8692
+
8693
+ expectError : true ,
8694
+ },
8695
+ }
8677
8696
8678
- s = `"@email.com`
8679
- errs = validate .Var (s , "email" )
8680
- NotEqual (t , errs , nil )
8681
- AssertError (t , errs , "" , "" , "" , "" , "email" )
8697
+ for _ , test := range tests {
8698
+ t .Run (fmt .Sprintf ("Email: %s" , test .input ), func (t * testing.T ) {
8699
+ errs := validate .Var (test .input , "email" )
8700
+ if test .expectError {
8701
+ AssertError (t , errs , "" , "" , "" , "" , "email" )
8702
+ } else {
8703
+ Equal (t , errs , nil )
8704
+ }
8705
+ })
8706
+ }
8682
8707
8708
+ // boolean is not a valid email
8683
8709
i := true
8684
- errs = validate .Var (i , "email" )
8710
+ errs : = validate .Var (i , "email" )
8685
8711
NotEqual (t , errs , nil )
8686
8712
AssertError (t , errs , "" , "" , "" , "" , "email" )
8687
8713
}
0 commit comments