Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

validation-br

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validation-br - npm Package Compare versions

Comparing version 1.4.2 to 1.4.4

dist/data/ValidationBRError.d.ts

24

dist/cnh.js

@@ -57,4 +57,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fake = exports.mask = exports.validate = exports.validateOrFail = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -67,9 +71,6 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('CNH não informado');
const cnh = utils_1.clearValue(value, 9);
const invalidList = utils_1.invalidListGenerator(9);
if (invalidList.includes(cnh)) {
throw new Error('CNH não pode ser uma sequência de números iguais');
}
const cnh = utils_1.clearValue(value, 9, {
trimAtRight: true,
rejectEmpty: true,
});
const sum1 = utils_1.sumElementsByMultipliers(cnh.substring(0, 9), [2, 3, 4, 5, 6, 7, 8, 9, 10]);

@@ -90,5 +91,10 @@ const dv1 = utils_1.sumToDV(sum1);

exports.validateOrFail = (value) => {
const cnh = utils_1.clearValue(value, 11);
const cnh = utils_1.clearValue(value, 11, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
rejectEqualSequence: true,
});
if (exports.dv(cnh) !== cnh.substring(9, 11)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -95,0 +101,0 @@ return true;

@@ -53,11 +53,14 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");
exports.dv = (value) => {
const cnpj = utils_1.clearValue(value, 12);
const blackList = utils_1.invalidListGenerator(12);
if (blackList.includes(cnpj)) {
throw new Error('CNPJ é obrigatório');
}
const cnpj = utils_1.clearValue(value, 12, {
trimAtRight: true,
rejectEmpty: true,
});
const sum1 = utils_1.sumElementsByMultipliers(cnpj.substring(0, 12), '543298765432');

@@ -96,5 +99,10 @@ const dv1 = utils_1.sumToDV(sum1);

exports.validateOrFail = (value) => {
const cnpj = utils_1.clearValue(value, 14);
const cnpj = utils_1.clearValue(value, 14, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
rejectEqualSequence: true,
});
if (exports.dv(cnpj) !== cnpj.substring(12, 14)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -101,0 +109,0 @@ return true;

@@ -61,4 +61,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -73,9 +77,6 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('Número não informado');
const cpf = utils_1.clearValue(value, 9);
const invalidList = utils_1.invalidListGenerator(9);
if (invalidList.includes(cpf)) {
throw new Error('Número não pode ser uma sequência de números iguais');
}
const cpf = utils_1.clearValue(value, 9, {
trimAtRight: true,
rejectEmpty: true,
});
const sum1 = utils_1.sumElementsByMultipliers(cpf, [10, 9, 8, 7, 6, 5, 4, 3, 2]);

@@ -116,5 +117,10 @@ const dv1 = utils_1.sumToDV(sum1);

exports.validateOrFail = (value) => {
const cpf = utils_1.clearValue(value, 11);
const cpf = utils_1.clearValue(value, 11, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
rejectEqualSequence: true,
});
if (exports.dv(cpf) !== cpf.substring(9, 11)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -121,0 +127,0 @@ return true;

@@ -81,4 +81,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._getSubCourt = exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -90,5 +94,3 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('Número do processo é obrigatório');
const judicialProcess = utils_1.clearValue(value, 18);
const judicialProcess = utils_1.clearValue(value, 18, { trimAtRight: true, rejectEmpty: true });
const num = judicialProcess.substring(0, 7);

@@ -134,3 +136,7 @@ const yearAndCourt = judicialProcess.substring(7, 14);

exports.validateOrFail = (value) => {
const judicialProcess = utils_1.clearValue(value, 20);
const judicialProcess = utils_1.clearValue(value, 20, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
});
const processWithoutDV = utils_1.removeFromPosition(judicialProcess, 7, 9);

@@ -141,3 +147,3 @@ if (processWithoutDV.substring(11, 12) === '0') {

if (exports.dv(processWithoutDV) !== judicialProcess.substring(7, 9)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -144,0 +150,0 @@ return true;

@@ -63,4 +63,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -75,19 +79,8 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('Número do protocolo é obrigatório');
const nup = utils_1.clearValue(value, 15); // não pega os 2 últimos dígitos
const nup = utils_1.clearValue(value, 15, { rejectEmpty: true, trimAtRight: true });
const nupReverse = nup.split('').reverse().join('');
const sumToDV = (sum) => {
const rest = 11 - (sum % 11);
const exceptions = [
{ rest: 11, dv: 1 },
{ rest: 10, dv: 0 },
];
const inExceptions = exceptions.find((item) => item.rest === rest);
return !inExceptions ? rest : inExceptions.dv;
};
const sum1 = utils_1.sumElementsByMultipliers(nupReverse, [...Array(15)].map((_, i) => i + 2));
const dv1 = sumToDV(sum1);
const dv1 = _specificSumToDV(sum1);
const sum2 = utils_1.sumElementsByMultipliers(dv1 + nupReverse, [...Array(16)].map((_, i) => i + 2));
const dv2 = sumToDV(sum2);
const dv2 = _specificSumToDV(sum2);
return `${dv1}${dv2}`;

@@ -125,5 +118,8 @@ };

exports.validateOrFail = (value) => {
const nup = utils_1.clearValue(value, 17);
if (exports.dv(nup) !== nup.substr(-2, 2)) {
throw new Error('Dígito verificador inválido');
const nup = utils_1.clearValue(value, 17, {
rejectEmpty: true,
rejectHigherLength: true,
});
if (exports.dv(nup) !== nup.substring(15, 17)) {
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -148,2 +144,11 @@ return true;

exports.default = exports.validate;
function _specificSumToDV(sum) {
const rest = 11 - (sum % 11);
const exceptions = [
{ rest: 11, dv: 1 },
{ rest: 10, dv: 0 },
];
const inExceptions = exceptions.find((item) => item.rest === rest);
return !inExceptions ? rest : inExceptions.dv;
}
//# sourceMappingURL=nup17.js.map

@@ -43,4 +43,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -55,9 +59,6 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('PIS não informado');
const pis = utils_1.clearValue(value, 10);
const invalidList = utils_1.invalidListGenerator(10);
if (invalidList.includes(pis)) {
throw new Error('PIS não pode ser uma sequência de números iguais');
}
const pis = utils_1.clearValue(value, 10, {
trimAtRight: true,
rejectEmpty: true,
});
const sum = utils_1.sumElementsByMultipliers(pis, [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]);

@@ -95,5 +96,10 @@ return String(utils_1.sumToDV(sum));

exports.validateOrFail = (value) => {
const pis = utils_1.clearValue(value, 11);
const pis = utils_1.clearValue(value, 11, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
rejectEqualSequence: true,
});
if (exports.dv(pis) !== pis.substring(10, 11)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -100,0 +106,0 @@ return true;

@@ -45,4 +45,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -58,3 +62,3 @@ /**

if (!value)
throw new Error('Código de Rastreamento não informado');
throw ValidationBRError_1.default.EMPTY_VALUE;
const postalCode = String(value)

@@ -109,3 +113,3 @@ .replace(/[^0-9]+/gi, '')

if (exports.dv(value.substring(2, 11)) !== postalCode.substring(8, 9)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -112,0 +116,0 @@ return true;

@@ -37,4 +37,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -49,9 +53,7 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('Renavam não informado');
const renavam = utils_1.clearValue(value, 10); // só 10 para remover o DV
const invalidList = utils_1.invalidListGenerator(10);
if (invalidList.includes(renavam)) {
throw new Error('RENAVAM não pode ser uma sequência de números iguais');
}
const renavam = utils_1.clearValue(value, 10, {
fillZerosAtLeft: true,
trimAtRight: true,
rejectEmpty: true,
});
const sum1 = utils_1.sumElementsByMultipliers(renavam, [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]) * 10;

@@ -90,5 +92,10 @@ const dv1 = sum1 % 11 >= 10 ? 0 : sum1 % 11;

exports.validateOrFail = (value) => {
const renavam = utils_1.clearValue(value, 11);
const renavam = utils_1.clearValue(value, 11, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
rejectEqualSequence: true,
});
if (exports.dv(renavam) !== renavam.substring(10, 11)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -95,0 +102,0 @@ return true;

@@ -55,4 +55,8 @@ "use strict";

*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = exports.validateOrFail = exports.fake = exports.mask = exports.dv = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
const utils_1 = require("./utils");

@@ -67,9 +71,7 @@ /**

exports.dv = (value) => {
if (!value)
throw new Error('CPF não informado');
const titulo = utils_1.clearValue(value, 10);
const invalidList = utils_1.invalidListGenerator(10);
if (invalidList.includes(titulo)) {
throw new Error('Título não pode ser uma sequência de números iguais');
}
const titulo = utils_1.clearValue(value, 10, {
fillZerosAtLeft: true,
trimAtRight: true,
rejectEmpty: true,
});
const sum1 = utils_1.sumElementsByMultipliers(titulo.substring(0, 8), [2, 3, 4, 5, 6, 7, 8, 9]);

@@ -88,3 +90,2 @@ const dv1 = sum1 % 11 >= 10 ? 0 : sum1 % 11;

exports.mask = (value) => utils_1.applyMask(value, '0000.0000.0000');
// const mask = (value) => applyMask(value, '0000.0000.0000');
/**

@@ -113,5 +114,10 @@ * fake()

exports.validateOrFail = (value) => {
const titulo = utils_1.clearValue(value, 12);
const titulo = utils_1.clearValue(value, 12, {
fillZerosAtLeft: true,
rejectEmpty: true,
rejectHigherLength: true,
rejectEqualSequence: true,
});
if (exports.dv(titulo) !== titulo.substring(10, 12)) {
throw new Error('Dígito verificador inválido');
throw ValidationBRError_1.default.INVALID_DV;
}

@@ -118,0 +124,0 @@ return true;

@@ -67,3 +67,3 @@ /**

*/
export declare function clearValue(value: string | number, length?: number | null): string;
export declare function clearValue(value: string | number, length?: number | null, options?: ClearValueOptions): string;
/**

@@ -124,1 +124,12 @@ * insertAtPosition()

export declare function randomLetter(): string;
/**
* Opções do clearValue
*/
interface ClearValueOptions {
fillZerosAtLeft?: boolean;
trimAtRight?: boolean;
rejectEmpty?: boolean;
rejectHigherLength?: boolean;
rejectEqualSequence?: boolean;
}
export {};
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomLetter = exports.applyMask = exports.removeFromPosition = exports.insertAtPosition = exports.clearValue = exports.fakeNumber = exports.sumElementsByMultipliers = exports.invalidListGenerator = exports.sumToDV = void 0;
const ValidationBRError_1 = __importDefault(require("./data/ValidationBRError"));
/**

@@ -87,7 +91,24 @@ * Calcula o DV verificador a partir das regras do MOD11:

*/
function clearValue(value, length = null) {
const clearedValue = String(value).replace(/([/.-]+)/gi, '');
if (!length || clearedValue.length === length)
return clearedValue;
return clearedValue.padStart(length, '0').substring(0, length);
function clearValue(value, length = null, options) {
let clearedValue = String(value).replace(/([/.-]+)/gi, '');
if (options) {
if (options.rejectEmpty === true && clearedValue.length === 0) {
throw ValidationBRError_1.default.EMPTY_VALUE;
}
if (options.rejectHigherLength === true && length && clearedValue.length > length) {
throw ValidationBRError_1.default.MAX_LEN_EXCEDEED;
}
if (options.rejectEqualSequence === true && length) {
const invalidList = invalidListGenerator(length);
if (invalidList.includes(clearedValue)) {
throw ValidationBRError_1.default.SEQUENCE_REPEATED;
}
}
// if (!length || clearedValue.length === length) return clearedValue
if (length && options.fillZerosAtLeft)
clearedValue = clearedValue.padStart(length, '0');
if (length && options.trimAtRight)
clearedValue = clearedValue.substring(0, length);
}
return clearedValue;
}

@@ -146,3 +167,3 @@ exports.clearValue = clearValue;

const maskLen = clearValue(mask).length;
let masked = clearValue(value, maskLen);
let masked = clearValue(value, maskLen, { fillZerosAtLeft: true, trimAtRight: true });
const specialChars = ['/', '-', '.', '(', ')', ' '];

@@ -149,0 +170,0 @@ for (let position = 0; position < mask.length; position += 1) {

{
"name": "validation-br",
"description": "Brazilian personal documents validation - cpf, cnpj, titulo, pis/pasep, cnh, renavam, processos judiciais, protocolo federal, código de rastreamento dos correios",
"version": "1.4.2",
"version": "1.4.4",
"main": "dist/index.js",

@@ -6,0 +6,0 @@ "types": "./dist/types.d.ts",

@@ -410,21 +410,27 @@ # validation-br

![Testes](https://user-images.githubusercontent.com/100168/193415244-22fea27a-2a50-42bc-ab71-d21fdc7e465b.png)
![Testes passando com 100% de cobertura](https://user-images.githubusercontent.com/100168/210075927-e13d8fad-f252-40a5-ad04-326a3198c0cb.png)
# Changelog
- **30/12/2022**:
- 1.4.4
- Correção de bug quando o documento válido tinha caracteres adicionais
- Refatoração de `clearValue()` para comportar configurações opcionais
- **01/10/2022**:
- 1.4.1
- Correção na importação principal dos módulos
- Refatoração do isJudicialProcess para permitir 100% de cobertura dos testes
- Inclusão de mais testes unitários para atingir 100% de cobertura
- 1.4.1
- Correção na importação principal dos módulos
- Refatoração do isJudicialProcess para permitir 100% de cobertura dos testes
- Inclusão de mais testes unitários para atingir 100% de cobertura
- **10/01/2022**:
- 1.1.0 - Adicionado NUP17 - Número Unificado de Protocolo de 17 dígitos do Governo Federal
- 1.1.0 - Adicionado NUP17 - Número Unificado de Protocolo de 17 dígitos do Governo Federal
- **09/01/2022**:
- 1.0.0 - Biblioteca convertida para Typescript e testes convertidos para Jest
- 1.0.0 - Biblioteca convertida para Typescript e testes convertidos para Jest
- **08/01/2022**:
- 0.21.1 - Adicionadas as funções isRenavam e isJudicialProcess
- 0.21.1 - Adicionadas as funções isRenavam e isJudicialProcess
- **16/09/2021**:
- 0.5.0 - Adicionadas as funções isCPF, isCNPJ e isTituloEleitor
- 0.7.0 - Adicionadas as funções isPostalCode e isCNH
- 0.8.0 - Adicionada a função isPIS
- 0.5.0 - Adicionadas as funções isCPF, isCNPJ e isTituloEleitor
- 0.7.0 - Adicionadas as funções isPostalCode e isCNH
- 0.8.0 - Adicionada a função isPIS

@@ -431,0 +437,0 @@ # Referências

@@ -23,3 +23,4 @@ {

},
"exclude": ["./tsconfig.json"],
"include": ["./src/**/*"]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc