cpf_cnpj_validator 1.0.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 93

CPF/CNPJ Validator #

A Flutter plugin to validate CPF/CNPJ numbers from Brazil.

pub package

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  cpf_cnpj_validator: ^1.0.5

Usage CPF #

// Import package  
import 'package:cpf_cnpj_validator/cpf_validator.dart';  
  
CPFValidator.isValid("334.616.710-02") // true
CPFValidator.isValid("334.616.710-01") // false
CPFValidator.isValid("35999906032") // true
CPFValidator.isValid("35999906031") // false
CPFValidator.isValid("033461671002") // false

// If you dont want the validation method to strip the values
// Just use false in the second argument
CPFValidator.isValid("334.616.710-02", false) // false
CPFValidator.isValid("35999906032@mail", false) // false


// Other utility methods
CPFValidator.format("33461671002") // Result: 334.616.710-02
CPFValidator.strip("334.616.710-02") // Result: 33461671002

// Generate a raw CPF number, without format
CPFValidator.generate() // Result: 33461671002

// Generate a formatted CPF number
CPFValidator.generate(true) // Result: 334.616.710-02 

Usage CNPJ #

// Import package  
import 'package:cpf_cnpj_validator/cnpj_validator.dart';  
  
CNPJValidator.isValid("12.175.094/0001-19") // true
CNPJValidator.isValid("12.175.094/0001-18") // false
CNPJValidator.isValid("17942159000128") // true
CNPJValidator.isValid("17942159000127") // false
CNPJValidator.isValid("017942159000128") // false

// If you dont want the validation method to strip the values
// Just use false in the second argument
CNPJValidator.isValid("12.175.094/0001-19", false) // false
CNPJValidator.isValid("17942159000128@mail", false) // false

// Other utility methods
CNPJValidator.format("85137090000110") // Result: 85.137.090/0001-10
CNPJValidator.strip("85.137.090/0001-10") // Result: 85137090000110

// Generate a raw CNPJ number, without format
CNPJValidator.generate() // Result: 85137090000110

// Generate a formatted CNPJ number
CNPJValidator.generate(true) // Result: 85.137.090/0001-10 

Credits #

This plugin is a rewrite to Dart from an existing Javascript project with the same purpose. All the credits go to the person who created the projects in Javascript.

If you guys like this plugin, make sure to star the original projects. I'll leave the links below:

https://github.com/fnando/cpf

https://github.com/fnando/cnpj

Contributing #

If you wish to contribute to this project, I encourage you to open a pull request.

v1.0.0 #

This release includes:

  • Ability to validate CPF and CNPJ numbers.
  • Ability to format, strip and create random CPF and CNPJ numbers.

v1.0.1 #

  • Fixing README.md

v1.0.2 #

  • Fixing README.md

v1.0.3 #

  • Fixing README.md

v1.0.4 #

  • Adding Example
  • Changing description

v1.0.5 #

  • Adding new parameter to control whether or not the validation method should strip the values

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:cpf_cnpj_validator/cpf_validator.dart';
import 'package:cpf_cnpj_validator/cnpj_validator.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text("CPF is valid: ${CPFValidator.isValid("334.616.710-02")}"),
            Text("CNPJ is valid: ${CNPJValidator.isValid("12.175.094/0001-19")}"),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cpf_cnpj_validator: ^1.0.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cpf_cnpj_validator/cnpj_validator.dart';
import 'package:cpf_cnpj_validator/cpf_validator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
92
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
88
Overall:
Weighted score of the above. [more]
93
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Document public APIs. (-1 points)

18 out of 18 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/cpf_validator.dart.

Run flutter format to format lib/cpf_validator.dart.

Maintenance suggestions

Package is getting outdated. (-12.05 points)

The package was last published 58 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
flutter_test 0.0.0
Transitive dependencies
archive 2.0.11 2.0.13
args 1.5.2 1.6.0
async 2.4.0 2.4.1
boolean_selector 1.0.5 2.0.0
charcode 1.1.2 1.1.3
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3 2.1.4
image 2.1.4 2.1.12
matcher 0.12.6
meta 1.1.8
path 1.6.4
pedantic 1.8.0+1 1.9.0
petitparser 2.4.0 3.0.2
quiver 2.0.5 2.1.3
sky_engine 0.0.99
source_span 1.5.5 1.7.0
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.11 0.2.15
typed_data 1.1.6
vector_math 2.0.8
xml 3.5.0 4.1.0