A Swift library to validate CPF and CNPJ. Automatically strips formatting, covers valid numbers, wrong length, number sequences, and common fake numbers. Options can be passed to allow invalid number sequences, that's useful for testing.
You can use two types of validators: StatusValidator
(if you're interested in what's wrong) and BooleanValidator
(if you only care if a given number is valid or not).
let success = BooleanValidator().validate(cpf: "12345678909")
let cpf = "1234567890912345678909"
let status = validator.validate(cpf: cpf)
//Status is .wrongLength
You can also pass options to the validate method. The options available are:
- Add Leading Zeros
- Ignore Remaining Characters
- Interpret Only Numbers
- Allow Repeated Patterns
- Allow Common Numbers
By default no options are given
The possible validation statuses are:
- Valid
- Wrong Length (i.e.
111
) - Repeated Pattern (i.e.
111.111.111-11
) - Common Number (i.e.
123.456.789-09
) - Invalid (invalid verification digits)
To run the example project, clone the repo, and run pod install
from the Example directory first.
For Swift 4.0+ use v1.0
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate CPF-CNPJ-Validator into your Xcode project using CocoaPods, specify it in your Podfile
:
use_frameworks!
pod 'CPF-CNPJ-Validator'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate CPF-CNPJ-Validator into your Xcode project using Carthage, specify it in your Cartfile
:
github "fpg1503/CPF-CNPJ-Validator" ~> 1.0
Run carthage update
to build the framework and drag the built CPF-CNPJ-Validator.framework
into your Xcode project.
Francesco Perrotti-Garcia (@fpg1503)
All contributions are welcome. Feel free to open Issues and PRs. In case of doubts read our CONTRIBUTING.md, open an issue or tweet me @fpg1503.
CPF-CNPJ-Validator is available under the MIT license. See the LICENSE file for more info.