Skip to content

๐Ÿ†” Swifty CPF and CNPJ Validator with customizable rules

License

Notifications You must be signed in to change notification settings

fpg1503/CPF-CNPJ-Validator

Repository files navigation

CPF-CNPJ-Validator

CI Status Version License Platform Carthage compatible

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.

Usage

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).

BooleanValidator

let success = BooleanValidator().validate(cpf: "12345678909")

StatusValidator

let cpf = "1234567890912345678909"
let status = validator.validate(cpf: cpf)
//Status is .wrongLength

Validation Options

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

Validation Statuses

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)

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

For Swift 4.0+ use v1.0

CocoaPods

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

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.

Author

Francesco Perrotti-Garcia (@fpg1503)

Contributing

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.

License

CPF-CNPJ-Validator is available under the MIT license. See the LICENSE file for more info.

About

๐Ÿ†” Swifty CPF and CNPJ Validator with customizable rules

Resources

License

Stars

Watchers

Forks

Packages

No packages published