Skip to content

Commit

Permalink
Implements phone validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagompalte committed Jan 3, 2024
1 parent 005e8d5 commit 8905b78
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ portuguese.

BR Doc é um pacote para validação, tanto do formato quanto dos dígitos,
de documentos brasileiros, como CPF, CNPJ, CEP, CNH, PIS/PASEP, RENAVAM, placa
veicular e RG (só SP e RJ).
veicular e RG (só SP e RJ), telefone e celular.

Aceito PRs de todas as formas. Está permitido escrever em português,
também. :)
Expand All @@ -33,6 +33,7 @@ Principais funções:
- `func IsMercosulPlate(doc string) bool`
- `func IsCNS(doc string) bool`
- `func IsRG(doc string, uf FederativeUnit) bool`
- `func IsPhone(phone string) (isValid bool, uf FederativeUnit)`

## Coisas a fazer

Expand All @@ -43,6 +44,7 @@ Principais funções:
- [x] validação de RENAVAM (obrigado @leogregianin!)
- [x] validação de placa veicular
- [x] validação de CNS (obrigado @renatosuero!)
- [x] validação de telefone e celular (obrigado @tiagompalte!)
- [ ] validação de RG
- [x] SP (obrigado @robas!)
- [x] RJ (obrigado @robas!)
Expand All @@ -59,7 +61,6 @@ submitted for inclusion in the work by you shall be in the public
domain, without any additional terms or conditions.

[1]: ./LICENSE

[badge-1-img]: https://img.shields.io/github/license/paemuri/brdoc?style=flat-square
[badge-1-link]: https://github.com/paemuri/brdoc/blob/master/LICENSE
[badge-2-img]: https://img.shields.io/badge/go.dev-reference-007d9c?style=flat-square&logo=go&logoColor=white
Expand Down
94 changes: 94 additions & 0 deletions phone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package brdoc

import "regexp"

var phoneRegexp = regexp.MustCompile(`^(?:(?:\+|00)?(55)\s?)?(?:\(?([1-9][0-9])\)?\s?)(?:((?:9\s?\d|[2-9])\d{3})\-?(\d{4}))$`)

var mapDDD = map[string]FederativeUnit{
"61": DF,
"62": GO,
"64": GO,
"65": MT,
"66": MT,
"67": MS,
"82": AL,
"71": BA,
"73": BA,
"74": BA,
"75": BA,
"77": BA,
"85": CE,
"88": CE,
"98": MA,
"99": MA,
"83": PB,
"81": PE,
"87": PE,
"86": PI,
"89": PI,
"84": RN,
"79": SE,
"68": AC,
"96": AP,
"92": AM,
"97": AM,
"91": PA,
"93": PA,
"94": PA,
"69": RO,
"95": RR,
"63": TO,
"27": ES,
"28": ES,
"31": MG,
"32": MG,
"33": MG,
"34": MG,
"35": MG,
"37": MG,
"38": MG,
"21": RJ,
"22": RJ,
"24": RJ,
"11": SP,
"12": SP,
"13": SP,
"14": SP,
"15": SP,
"16": SP,
"17": SP,
"18": SP,
"19": SP,
"41": PR,
"42": PR,
"43": PR,
"44": PR,
"45": PR,
"46": PR,
"51": RS,
"53": RS,
"54": RS,
"55": RS,
"47": SC,
"48": SC,
"49": SC,
}

// IsPhone verifies if `phone` is a phone number valid and return UF from DDD.
func IsPhone(phone string) (isValid bool, uf FederativeUnit) {
if !phoneRegexp.MatchString(phone) {
isValid = false
return
}

groups := phoneRegexp.FindStringSubmatch(phone)

groupDDD := 1
if len(groups) == 5 {
groupDDD = 2
}

uf, isValid = mapDDD[groups[groupDDD]]

return
}
35 changes: 35 additions & 0 deletions phone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package brdoc

import "testing"

func TestIsPhone(t *testing.T) {
for i, tc := range []struct {
name string
expectedValid bool
expectedUf FederativeUnit
v string
}{
{"Valid_ShouldReturnTrueUfSP", true, SP, "+55(11)999999999"},
{"Valid_ShouldReturnTrueUfSP", true, SP, "55(11)999999999"},
{"Valid_ShouldReturnTrueUfPR", true, PR, "(41)999999999"},
{"Valid_ShouldReturnTrueUfDF", true, DF, "(61)32222222"},
{"Valid_ShouldReturnTrueUfAC", true, AC, "(68) 99988-1234"},
{"Valid_ShouldReturnTrueUfPE", true, PE, "8198988888"},
{"Valid_ShouldReturnTrueUfPE", true, PE, "558198988888"},
{"Valid_ShouldReturnFalseUfAC", true, SP, "12 9999-9999"},
{"InvalidDDI_ShouldReturnFalseUfAC", false, AC, "+01(11)999999999"},
{"InvalidDDD_ShouldReturnFalseUfAC", false, AC, "(01)999999999"},
{"InvalidDDD_ShouldReturnFalseUfAC", false, AC, "55(01)999999999"},
{"InvalidPattern_ShouldReturnFalseUfAC", false, AC, "11 9999 9999"},
{"InvalidPattern_ShouldReturnFalseUfAC", false, AC, "11 9 9999 9999"},
{"InvalidPattern_ShouldReturnFalseUfAC", false, AC, "11.99999.9999"},
{"InvalidPattern_ShouldReturnFalseUfAC", false, AC, "11 99999/9999"},
{"InvalidPattern_ShouldReturnFalseUfAC", false, AC, "(11)9999999-99"},
} {
t.Run(testName(i, tc.name), func(t *testing.T) {
isValid, uf := IsPhone(tc.v)
assertEqual(t, tc.expectedValid, isValid)
assertEqual(t, tc.expectedUf, uf)
})
}
}

0 comments on commit 8905b78

Please sign in to comment.