🇧🇷 CPF validation in Go

Better API: Validate -> IsValid

Thanks @danielfireman

+9 -9
+6 -5
cpf.go
··· 31 31 return r 32 32 } 33 33 34 - //Validate check if Cpf is in a valid format 35 - func (c Cpf) Validate() bool { 34 + //IsValid checks whether Cpf number is valid or not 35 + func (c Cpf) IsValid() bool { 36 36 u := c.Unmask() 37 37 38 - if len(u) != 11 { 38 + if len(u) != 11 { 39 39 return false 40 40 } 41 41 ··· 49 49 ds[i] = c 50 50 s[c] = member 51 51 } 52 - 53 - if len(m) == 1 { 52 + 53 + //If all digits are the same, the Cpf is not valid 54 + if len(s) == 1 { 54 55 return false 55 56 } 56 57
+3 -4
cpf_test.go
··· 23 23 } 24 24 } 25 25 26 - func TestValidate(t *testing.T) { 26 + func TestIsValid(t *testing.T) { 27 27 for _, tc := range []struct { 28 28 cpf Cpf 29 29 expected bool ··· 35 35 {Cpf("123.456.769/01"), false}, 36 36 {Cpf("ABC.DEF.GHI-JK"), false}, 37 37 } { 38 - if got := tc.cpf.Validate(); tc.expected != got { 39 - t.Errorf("Cpf(%v).Validate() = %v; want %v", tc.cpf, got, tc.expected) 38 + if got := tc.cpf.IsValid(); tc.expected != got { 39 + t.Errorf("Cpf(%v).IsValid() = %v; want %v", tc.cpf, got, tc.expected) 40 40 } 41 41 } 42 - 43 42 }