next-CNPJ 1.1.0

dotnet add package next-CNPJ --version 1.1.0
                    
NuGet\Install-Package next-CNPJ -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="next-CNPJ" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="next-CNPJ" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="next-CNPJ" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add next-CNPJ --version 1.1.0
                    
#r "nuget: next-CNPJ, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package next-CNPJ@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=next-CNPJ&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=next-CNPJ&version=1.1.0
                    
Install as a Cake Tool

next-CNPJ

.NET library for robust CNPJ validation with support for the new alphanumeric format according to NTC 2025.001.

πŸ“‹ About

The next-CNPJ library provides complete CNPJ (Cadastro Nacional da Pessoa JurΓ­dica) validation, including support for the new alphanumeric format that will come into effect. It implements all NTC 2025.001 rules, including calculation and validation of check digits, automatic normalization, and format identification.

✨ Features

  • βœ… Numeric CNPJ validation (traditional format)
  • βœ… Alphanumeric CNPJ validation (new NTC 2025.001 format)
  • βœ… Automatic check digit calculation and validation
  • βœ… Automatic normalization (removes formatting: dots, slashes, hyphens)
  • βœ… Automatic format identification (numeric or alphanumeric)
  • βœ… Excluded letters configuration (I, O, U, Q, F according to ENCAT)
  • βœ… Support for CNPJ with or without formatting
  • βœ… Automatic lowercase to uppercase conversion
  • βœ… Rejection of CNPJ with all identical characters (e.g., 00000000000000)
  • βœ… Detailed results with descriptive error messages

πŸš€ Installation

Install the package via NuGet:

dotnet add package next-CNPJ

Or via Package Manager Console:

Install-Package next-CNPJ

πŸ“– Basic Usage

Simple Validation

using next_CNPJ.Core.Services;

var validator = new CnpjValidator();

// Simple validation - returns true/false
bool isValid = validator.IsValid("11222333000181");
Console.WriteLine($"Valid CNPJ: {isValid}"); // true

Detailed Validation

using next_CNPJ.Core.Services;
using next_CNPJ.Core.Domain;

var validator = new CnpjValidator();

// Detailed validation - returns complete information
var result = validator.Validate("11222333000181");

if (result.IsValid)
{
    Console.WriteLine($"Valid CNPJ!");
    Console.WriteLine($"Format: {result.Format}"); // Numeric
    Console.WriteLine($"Normalized CNPJ: {result.NormalizedCnpj}"); // 11222333000181
}
else
{
    Console.WriteLine($"Error: {result.ErrorMessage}");
}

Validation with Formatting

The library accepts CNPJ with or without formatting:

var validator = new CnpjValidator();

// All these forms are accepted:
validator.IsValid("11222333000181");           // Without formatting
validator.IsValid("11.222.333/0001-81");      // With traditional formatting
validator.IsValid("12.ABC.345/01DE-35");      // Alphanumeric format with formatting

πŸ”€ Alphanumeric CNPJ

Alphanumeric CNPJ Validation

The new alphanumeric format allows letters in the root (positions 1-8) or in the order (positions 9-12):

var validator = new CnpjValidator();

// Example of valid alphanumeric CNPJ
var result = validator.Validate("12ABC34501DE35");

if (result.IsValid)
{
    Console.WriteLine($"Format: {result.Format}"); // Alphanumeric
    Console.WriteLine($"Normalized CNPJ: {result.NormalizedCnpj}"); // 12ABC34501DE35
}

Excluded Letters

By default, the letters I, O, U, Q, F are excluded according to ENCAT technical specification:

var validator = new CnpjValidator();

// CNPJ with excluded letter (I) - invalid
var result = validator.Validate("12IBC34501DE35");
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.ErrorMessage); // "The root segment contains the letter 'I' which is not allowed. Excluded letters: I, O, U, Q, F."

Custom Configuration

You can customize excluded letters or allow all letters:

using next_CNPJ.Core.Domain;

var config = new CnpjConfiguration
{
    ExcludedLetters = new[] { 'I', 'O' }, // Only I and O excluded
    AllowExcludedLetters = false
};

var validator = new CnpjValidator();
var result = validator.Validate("12IBC34501DE35", config);

To allow all letters (including normally excluded ones):

var config = new CnpjConfiguration
{
    AllowExcludedLetters = true // Allows all letters
};

var result = validator.Validate("12IBC34501DE35", config);

Invalid CNPJ Patterns

The library rejects CNPJs with all identical characters (including all zeros):

var validator = new CnpjValidator();

// CNPJ with all zeros - invalid
var result = validator.Validate("00000000000000");
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.ErrorMessage); // "CNPJ invΓ‘lido: todos os caracteres sΓ£o iguais."

// CNPJ with all same digits - invalid
var result2 = validator.Validate("11111111111111");
Console.WriteLine(result2.IsValid); // false
Console.WriteLine(result2.ErrorMessage); // "CNPJ invΓ‘lido: todos os caracteres sΓ£o iguais."

πŸ” Format Identification

You can identify the CNPJ format before validating:

using next_CNPJ.Core.Services;

var identifier = new CnpjFormatIdentifier();

// Identify format
var format = identifier.IdentifyFormat("12ABC34501DE35");
Console.WriteLine(format); // Alphanumeric

// Quick checks
bool isAlphanumeric = identifier.IsAlphanumeric("12ABC34501DE35"); // true
bool isNumeric = identifier.IsNumeric("11222333000181"); // true

πŸ“š API Reference

ICnpjValidator

Main interface for CNPJ validation.

Methods
  • bool IsValid(string? cnpj, CnpjConfiguration? config = null)

    • Validates a CNPJ and returns true if valid, false otherwise.
  • CnpjValidationResult Validate(string? cnpj, CnpjConfiguration? config = null)

    • Validates a CNPJ and returns a CnpjValidationResult object with detailed information.

CnpjValidationResult

Validation result with the following properties:

  • bool IsValid - Indicates if the CNPJ is valid
  • CnpjFormat Format - Identified format (Numeric or Alphanumeric)
  • string? ErrorMessage - Error message (null if valid)
  • string NormalizedCnpj - Normalized CNPJ (without formatting)

ICnpjFormatIdentifier

Interface for CNPJ format identification.

Methods
  • CnpjFormat IdentifyFormat(string? cnpj) - Identifies the CNPJ format
  • bool IsAlphanumeric(string? cnpj) - Checks if it is alphanumeric
  • bool IsNumeric(string? cnpj) - Checks if it is numeric

CnpjConfiguration

Configuration for custom validation.

Properties
  • char[] ExcludedLetters - Letters that should not be accepted (default: I, O, U, Q, F)
  • bool AllowExcludedLetters - Allows excluded letters even if they are in the list (default: false)

πŸ’‘ Use Cases

1. Web Form Validation

public class CnpjValidationService
{
    private readonly ICnpjValidator _validator;

    public CnpjValidationService(ICnpjValidator validator)
    {
        _validator = validator;
    }

    public ValidationResult ValidateUserInput(string cnpj)
    {
        var result = _validator.Validate(cnpj);
        
        if (!result.IsValid)
        {
            return new ValidationResult
            {
                IsValid = false,
                ErrorMessage = result.ErrorMessage
            };
        }

        return new ValidationResult
        {
            IsValid = true,
            NormalizedCnpj = result.NormalizedCnpj,
            Format = result.Format.ToString()
        };
    }
}

2. Batch Processing

public void ValidateBatch(IEnumerable<string> cnpjList)
{
    var validator = new CnpjValidator();
    var results = new List<CnpjValidationResult>();

    foreach (var cnpj in cnpjList)
    {
        var result = validator.Validate(cnpj);
        results.Add(result);
        
        if (result.IsValid)
        {
            Console.WriteLine($"βœ“ {cnpj} - {result.Format}");
        }
        else
        {
            Console.WriteLine($"βœ— {cnpj} - {result.ErrorMessage}");
        }
    }
}

3. API Integration

[HttpPost("validate")]
public IActionResult ValidateCnpj([FromBody] CnpjRequest request)
{
    var validator = new CnpjValidator();
    var result = validator.Validate(request.Cnpj);

    if (result.IsValid)
    {
        return Ok(new
        {
            isValid = true,
            format = result.Format.ToString(),
            normalizedCnpj = result.NormalizedCnpj
        });
    }

    return BadRequest(new
    {
        isValid = false,
        error = result.ErrorMessage
    });
}

4. Normalization for Storage

public string NormalizeCnpjForStorage(string cnpj)
{
    var validator = new CnpjValidator();
    var result = validator.Validate(cnpj);
    
    if (result.IsValid)
    {
        // Always store normalized (without formatting)
        return result.NormalizedCnpj;
    }
    
    throw new ArgumentException($"Invalid CNPJ: {result.ErrorMessage}");
}

πŸ§ͺ Testing

The library includes a complete test suite. To run:

dotnet test

Tests cover:

  • Numeric CNPJ validation (traditional format)
  • Alphanumeric CNPJ validation (new format)
  • Check digit calculation
  • Format identification
  • Normalization
  • Error handling (including rejection of all zeros and identical characters)
  • Custom configurations

πŸ“¦ Library Structure

next-CNPJ/
└── next-CNPJ/
    └── Core/
        β”œβ”€β”€ Domain/
        β”‚   β”œβ”€β”€ CnpjConfiguration.cs      # Validation configuration
        β”‚   β”œβ”€β”€ CnpjFormat.cs              # Format enum
        β”‚   └── CnpjValidationResult.cs   # Validation result
        β”œβ”€β”€ Services/
        β”‚   β”œβ”€β”€ CnpjValidator.cs          # Validator implementation
        β”‚   β”œβ”€β”€ ICnpjValidator.cs         # Validator interface
        β”‚   β”œβ”€β”€ CnpjFormatIdentifier.cs   # Format identifier
        β”‚   └── ICnpjFormatIdentifier.cs  # Format identifier interface
        └── Utilities/
            β”œβ”€β”€ AsciiConverter.cs          # ASCII conversion for calculation
            β”œβ”€β”€ CnpjNormalizer.cs          # CNPJ normalization
            └── DigitVerifierCalculator.cs # Check digit calculation

πŸ”— References

  • NTC 2025.001 - Technical specification for the new CNPJ format
  • ENCAT - Excluded letters specification

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🀝 Contributing

Contributions are welcome! Please open an issue or pull request.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 208 1/23/2026
1.0.2 121 1/19/2026
1.0.1 119 1/19/2026
1.0.0 118 1/19/2026