├── .github ├── FUNDING.yml └── workflows │ └── tests.yml ├── go.mod ├── go.sum ├── cnpf.go ├── LICENSE ├── README.md └── cnpf_test.go /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [cuducos] 2 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cuducos/go-cnpf 2 | 3 | go 1.14 4 | 5 | require ( 6 | github.com/cuducos/go-cnpj v0.1.2 7 | github.com/cuducos/go-cpf v0.0.1 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 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 | github.com/cuducos/go-cpf v0.0.1 h1:LCyzmLSMKv49Qu+M3sozypOvT3WfZfj1/10fUUkdrdw= 4 | github.com/cuducos/go-cpf v0.0.1/go.mod h1:+K4ER/oqiFbNHrTgg4ZZsAGdkP5C6Cj2+Dwo4Lv9p+A= 5 | -------------------------------------------------------------------------------- /cnpf.go: -------------------------------------------------------------------------------- 1 | package cnpf 2 | 3 | import ( 4 | cnpj "github.com/cuducos/go-cnpj" 5 | cpf "github.com/cuducos/go-cpf" 6 | ) 7 | 8 | // Unmask removes any non-alphanumeric character (like punctuation) from a CPF 9 | // or CNPJ number 10 | func Unmask(n string) string { 11 | return cnpj.Unmask(n) 12 | } 13 | 14 | // Mask returns the CPF or CNPJ number formatted 15 | func Mask(n string) string { 16 | u := Unmask(n) 17 | if len(u) == 11 { 18 | return cpf.Mask(u) 19 | } 20 | if len(u) == 14 { 21 | return cnpj.Mask(u) 22 | } 23 | return n 24 | } 25 | 26 | // IsValid checks whether a number is a valid CPF or CNPJ number 27 | func IsValid(n string) bool { 28 | return cpf.IsValid(n) || cnpj.IsValid(n) 29 | } 30 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | 7 | build: 8 | name: Build 9 | runs-on: ubuntu-latest 10 | steps: 11 | 12 | - name: Set up Go 1.14 13 | uses: actions/setup-go@v1 14 | with: 15 | go-version: 1.14 16 | id: go 17 | 18 | - name: Check out code into the Go module directory 19 | uses: actions/checkout@v2 20 | 21 | - name: Get dependencies 22 | run: | 23 | go get -v -t -d ./... 24 | if [ -f Gopkg.toml ]; then 25 | curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh 26 | dep ensure 27 | fi 28 | 29 | - name: Build 30 | run: go test 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Eduardo Cuducos 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go CNPF [![Tests](https://github.com/cuducos/go-cnpf/workflows/Tests/badge.svg)]() [![GoDoc](https://godoc.org/github.com/cuducos/go-cnpf?status.svg)](https://godoc.org/github.com/cuducos/go-cnpf) ![Go version](https://img.shields.io/github/go-mod/go-version/cuducos/go-cnpj) 2 | 3 | A Go module to validate CPF and CNPJ numbers (Brazilian people and companies unique identifier for the Federal Revenue). 4 | 5 | > The pseudo-acronym _CNPF_ is a sort of tong-twister and a common typo when developers discuss the implementation of objects that could hold either a CPF or a CNPJ numbers. 6 | 7 | ```go 8 | package main 9 | 10 | import "github.com/cuducos/go-cnpf" 11 | 12 | 13 | func main() { 14 | // these return true 15 | cnpf.IsValid("23858488135") 16 | cnpf.IsValid("238.584.881-35") 17 | cnpf.IsValid("11222333000181") 18 | cnpf.IsValid("11.222.333/0001-81") 19 | cnpj.IsValid("12.ABC.345/01DE-35") 20 | cnpj.IsValid("12ABC34501DE35") 21 | 22 | // these return false 23 | cnpf.IsValid("111.111.111-11") 24 | cnpf.IsValid("11.111.111/1111-11") 25 | cnpf.IsValid("123.456.769/01") 26 | cnpf.IsValid("12.345.678 9012-34") 27 | cnpf.IsValid("ABC.DEF.GHI-JK") 28 | cnpf.IsValid("AB.CDE.FGH/IJKL-MN") 29 | cnpf.IsValid("123") 30 | 31 | // these return 11111111111, 11111111111111 and 12ABC34501DE35 32 | cnpf.Unmask("111.111.111-11") 33 | cnpf.Unmask("11.111.111/1111-11") 34 | cnpj.Unmask("12.ABC.345/01DE-35") 35 | 36 | // this return 111.111.111-11, 11.111.111/1111-11 and 12.ABC.345/01DE-35 37 | cnpf.Mask("11111111111") 38 | cnpf.Mask("11111111111111") 39 | cnpj.Mask("12ABC34501DE35") 40 | } 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). 44 | 45 | Based on [Go CPF](https://github.com/cuducos/go-cpf) and [Go CNPJ](https://github.com/cuducos/go-cnpj) ❤️ 46 | -------------------------------------------------------------------------------- /cnpf_test.go: -------------------------------------------------------------------------------- 1 | package cnpf 2 | 3 | import "fmt" 4 | 5 | func ExampleIsValid_unmaskedValidCpf() { 6 | fmt.Println(IsValid("23858488135")) 7 | // Output: true 8 | } 9 | 10 | func ExampleIsValid_maskedValidCpf() { 11 | fmt.Println(IsValid("238.584.881-35")) 12 | // Output: true 13 | } 14 | 15 | func ExampleIsValid_unmaskedValidCnpjWithNewFormat() { 16 | fmt.Println(IsValid("12ABC34501DE35")) 17 | // Output: true 18 | } 19 | 20 | func ExampleIsValid_maskedValidCnpj() { 21 | fmt.Println(IsValid("11.222.333/0001-81")) 22 | // Output: true 23 | } 24 | 25 | func ExampleIsValid_maskedValidCnpjWithNewFormat() { 26 | fmt.Println(IsValid("12.ABC.345/01DE-35")) 27 | // Output: true 28 | } 29 | 30 | func ExampleIsValid_maskedInvalidCpf() { 31 | fmt.Println(IsValid("111.111.111-11")) 32 | // Output: false 33 | } 34 | 35 | func ExampleIsValid_maskedInvalidCnpj() { 36 | fmt.Println(IsValid("11.111.111/1111-11")) 37 | // Output: false 38 | } 39 | 40 | func ExampleIsValid_maskedUnknownStringForCpf() { 41 | fmt.Println(IsValid("123.456.769/01")) 42 | // Output: false 43 | } 44 | 45 | func ExampleIsValid_maskedUnknownStringCnpj() { 46 | fmt.Println(IsValid("12.345.678 9012-34")) 47 | // Output: false 48 | } 49 | 50 | func ExampleIsValid_maskedLettersCpf() { 51 | fmt.Println(IsValid("ABC.DEF.GHI-JK")) 52 | // Output: false 53 | } 54 | 55 | func ExampleIsValid_maskedLettersCnpj() { 56 | fmt.Println(IsValid("AB.CDE.FGH/IJKL-MN")) 57 | // Output: false 58 | } 59 | 60 | func ExampleIsValid_IncompleteNumber() { 61 | fmt.Println(IsValid("123")) 62 | // Output: false 63 | } 64 | 65 | func ExampleUnmask_maskedCpf() { 66 | fmt.Println(Unmask("111.111.111-11")) 67 | // Output: 11111111111 68 | } 69 | 70 | func ExampleUnmask_maskedCnpj() { 71 | fmt.Println(Unmask("11.111.111/1111-11")) 72 | // Output: 11111111111111 73 | } 74 | 75 | func ExampleUnmask_maskedCnpjWithNewFormat() { 76 | fmt.Println(Unmask("12.ABC.345/01DE-35")) 77 | // Output: 12ABC34501DE35 78 | } 79 | 80 | func ExampleMask_unmaskedCpf() { 81 | fmt.Println(Mask("11111111111")) 82 | // Output: 111.111.111-11 83 | } 84 | 85 | func ExampleMask_unmaskedCnpj() { 86 | fmt.Println(Mask("11111111111111")) 87 | // Output: 11.111.111/1111-11 88 | } 89 | 90 | func ExampleMask_unmaskedCnpjWithNewFormat() { 91 | fmt.Println(Mask("12ABC34501DE35")) 92 | // Output: 12.ABC.345/01DE-35 93 | } 94 | --------------------------------------------------------------------------------