🇧🇷 CPF and CNPJ validation in Go

Adds support for alphanumeric CNPJ (July/2026)

+52 -32
+8 -2
README.md
··· 16 16 cnpf.IsValid("238.584.881-35") 17 17 cnpf.IsValid("11222333000181") 18 18 cnpf.IsValid("11.222.333/0001-81") 19 + cnpj.IsValid("12.ABC.345/01DE-35") 20 + cnpj.IsValid("12ABC34501DE35") 19 21 20 22 // these return false 21 23 cnpf.IsValid("111.111.111-11") ··· 26 28 cnpf.IsValid("AB.CDE.FGH/IJKL-MN") 27 29 cnpf.IsValid("123") 28 30 29 - // these returns 11111111111 and 11111111111111 31 + // these return 11111111111, 11111111111111 and 12ABC34501DE35 30 32 cnpf.Unmask("111.111.111-11") 31 33 cnpf.Unmask("11.111.111/1111-11") 34 + cnpj.Unmask("12.ABC.345/01DE-35") 32 35 33 - // this returns 111.111.111-11 and 11.111.111/1111-11 36 + // this return 111.111.111-11, 11.111.111/1111-11 and 12.ABC.345/01DE-35 34 37 cnpf.Mask("11111111111") 35 38 cnpf.Mask("11111111111111") 39 + cnpj.Mask("12ABC34501DE35") 36 40 } 37 41 ``` 42 + > [!IMPORTANT] 43 + > Starting in July 2026 [the CNPJ number will be alphanumeric](https://www.gov.br/receitafederal/pt-br/acesso-a-informacao/acoes-e-programas/programas-e-atividades/cnpj-alfanumerico). This package **already supports the new format**. If you **do not** want to support the new format, tag this package to [`v0.0.1`](https://github.com/cuducos/go-cnpf/releases/tag/v0.0.1). 38 44 39 45 Based on [Go CPF](https://github.com/cuducos/go-cpf) and [Go CNPJ](https://github.com/cuducos/go-cnpj) ❤️
+7 -6
cnpf.go
··· 1 1 package cnpf 2 2 3 3 import ( 4 - cpf "github.com/cuducos/go-cpf" 5 - cnpj "github.com/cuducos/go-cnpj" 4 + cnpj "github.com/cuducos/go-cnpj" 5 + cpf "github.com/cuducos/go-cpf" 6 6 ) 7 7 8 - //Unmask removes any non-digit (numeric) from a CPF or CNPJ number 8 + // Unmask removes any non-alphanumeric character (like punctuation) from a CPF 9 + // or CNPJ number 9 10 func Unmask(n string) string { 10 - return cpf.Unmask(n) 11 + return cnpj.Unmask(n) 11 12 } 12 13 13 - //Mask returns the CPF or CNPJ number formatted 14 + // Mask returns the CPF or CNPJ number formatted 14 15 func Mask(n string) string { 15 16 u := Unmask(n) 16 17 if len(u) == 11 { ··· 22 23 return n 23 24 } 24 25 25 - //IsValid checks whether a number is a valid CPF or CNPJ number 26 + // IsValid checks whether a number is a valid CPF or CNPJ number 26 27 func IsValid(n string) bool { 27 28 return cpf.IsValid(n) || cnpj.IsValid(n) 28 29 }
+34 -21
cnpf_test.go
··· 7 7 // Output: true 8 8 } 9 9 10 - func ExampleIsValid_maskedValidCpf(){ 10 + func ExampleIsValid_maskedValidCpf() { 11 11 fmt.Println(IsValid("238.584.881-35")) 12 12 // Output: true 13 13 } 14 14 15 - func ExampleIsValid_nmaskedValidCnpj(){ 16 - fmt.Println(IsValid("11222333000181")) 15 + func ExampleIsValid_unmaskedValidCnpjWithNewFormat() { 16 + fmt.Println(IsValid("12ABC34501DE35")) 17 17 // Output: true 18 18 } 19 19 20 - func ExampleIsValid_maskedValidCnpj(){ 20 + func ExampleIsValid_maskedValidCnpj() { 21 21 fmt.Println(IsValid("11.222.333/0001-81")) 22 22 // Output: true 23 23 } 24 24 25 - func ExampleIsValid_maskedInvalidCpf(){ 25 + func ExampleIsValid_maskedValidCnpjWithNewFormat() { 26 + fmt.Println(IsValid("12.ABC.345/01DE-35")) 27 + // Output: true 28 + } 29 + 30 + func ExampleIsValid_maskedInvalidCpf() { 26 31 fmt.Println(IsValid("111.111.111-11")) 27 32 // Output: false 28 33 } 29 34 30 - func ExampleIsValid_maskedInvalidCnpj(){ 35 + func ExampleIsValid_maskedInvalidCnpj() { 31 36 fmt.Println(IsValid("11.111.111/1111-11")) 32 37 // Output: false 33 38 } 34 39 35 - func ExampleIsValid_maskedUnknownStringForCpf(){ 40 + func ExampleIsValid_maskedUnknownStringForCpf() { 36 41 fmt.Println(IsValid("123.456.769/01")) 37 42 // Output: false 38 43 } 39 44 40 - func ExampleIsValid_maskedUnknownStringCnpj(){ 45 + func ExampleIsValid_maskedUnknownStringCnpj() { 41 46 fmt.Println(IsValid("12.345.678 9012-34")) 42 47 // Output: false 43 48 } 44 49 45 - func ExampleIsValid_maskedLettersCpf(){ 46 - fmt.Println(IsValid("ABC.DEF.GHI-JK")) 50 + func ExampleIsValid_maskedLettersCpf() { 51 + fmt.Println(IsValid("ABC.DEF.GHI-JK")) 47 52 // Output: false 48 53 } 49 54 50 - func ExampleIsValid_maskedLettersCnpj(){ 51 - fmt.Println(IsValid("AB.CDE.FGH/IJKL-MN")) 55 + func ExampleIsValid_maskedLettersCnpj() { 56 + fmt.Println(IsValid("AB.CDE.FGH/IJKL-MN")) 52 57 // Output: false 53 58 } 54 59 55 - func ExampleIsValid_IncompleteNumber(){ 60 + func ExampleIsValid_IncompleteNumber() { 56 61 fmt.Println(IsValid("123")) 57 62 // Output: false 58 63 } 59 64 60 - 61 - func ExampleUnmask_maskedCpf(){ 62 - fmt.Println(Unmask("111.111.111-11")) 65 + func ExampleUnmask_maskedCpf() { 66 + fmt.Println(Unmask("111.111.111-11")) 63 67 // Output: 11111111111 64 68 } 65 69 66 - func ExampleUnmask_maskedCnpj(){ 70 + func ExampleUnmask_maskedCnpj() { 67 71 fmt.Println(Unmask("11.111.111/1111-11")) 68 72 // Output: 11111111111111 69 73 } 70 74 75 + func ExampleUnmask_maskedCnpjWithNewFormat() { 76 + fmt.Println(Unmask("12.ABC.345/01DE-35")) 77 + // Output: 12ABC34501DE35 78 + } 71 79 72 - func ExampleMask_unmaskedCpf(){ 73 - fmt.Println(Mask("11111111111")) 80 + func ExampleMask_unmaskedCpf() { 81 + fmt.Println(Mask("11111111111")) 74 82 // Output: 111.111.111-11 75 83 } 76 84 77 - func ExampleMask_unmaskedCnpj(){ 78 - fmt.Println(Mask("11111111111111") ) 85 + func ExampleMask_unmaskedCnpj() { 86 + fmt.Println(Mask("11111111111111")) 79 87 // Output: 11.111.111/1111-11 80 88 } 89 + 90 + func ExampleMask_unmaskedCnpjWithNewFormat() { 91 + fmt.Println(Mask("12ABC34501DE35")) 92 + // Output: 12.ABC.345/01DE-35 93 + }
+1 -1
go.mod
··· 3 3 go 1.14 4 4 5 5 require ( 6 - github.com/cuducos/go-cnpj v0.0.1 6 + github.com/cuducos/go-cnpj v0.1.2 7 7 github.com/cuducos/go-cpf v0.0.1 8 8 )
+2 -2
go.sum
··· 1 - github.com/cuducos/go-cnpj v0.0.1 h1:qx3CbujuZaugm/1HE0sN65BoqAO8bzRZtP7qEvvwMT8= 2 - github.com/cuducos/go-cnpj v0.0.1/go.mod h1:Oe0M530eHl9eXExbNWriqTD9kMv618ScjlGtEey/f4Y= 1 + github.com/cuducos/go-cnpj v0.1.2 h1:EKfO9AJPjxBh/Pc8S+SMSeXvQ7kLwhDoF6te4md58OA= 2 + github.com/cuducos/go-cnpj v0.1.2/go.mod h1:Oe0M530eHl9eXExbNWriqTD9kMv618ScjlGtEey/f4Y= 3 3 github.com/cuducos/go-cpf v0.0.1 h1:LCyzmLSMKv49Qu+M3sozypOvT3WfZfj1/10fUUkdrdw= 4 4 github.com/cuducos/go-cpf v0.0.1/go.mod h1:+K4ER/oqiFbNHrTgg4ZZsAGdkP5C6Cj2+Dwo4Lv9p+A=