bsk-validations

A validator to BR informations

Usage no npm install needed!

<script type="module">
  import bskValidations from 'https://cdn.skypack.dev/bsk-validations';
</script>

README

validations-br

Easy validations ✅


Read this in other languages: Portuguese

npm Coverage Status License

Overview

Validations BR is a function tool belt for validating standard documents in Brazil and more.

Installation

With npm:

npm install validations-br

With yarn:

yarn add validations-br

Usage

useValidationsBR

Parâmetros

Key Type Description
type cnpj \| cpf \| cpf_cnpj \| cep \| email \| pis \| phone \| uf Type of data to be validated
value string Value that will be validated
import { useValidationsBR } from 'validations-br';
// const { useValidationsBR } = require('validations-br');

const cnpj = '09.015.844/0001-80';
const isValid = useValidationsBR('cnpj', cnpj);

Validate CNPJ

import { validateCNPJ } from 'validations-br';
// const { validateCNPJ } = require('validations-br');

const cnpj = '09.015.844/0001-80';
const isValid = validateCNPJ(cnpj);

Validate CPF

import { validateCPF } from 'validations-br';
// const { validateCPF } = require('validations-br');

const cpf = '370.785.180-04';
const isValid = validateCPF(cpf);

Validate CPF and CNPJ

import { validateCPFCNPJ } from 'validations-br';
// const { validateCPFCNPJ } = require('validations-br');


const cpf_cnpj = '09.015.844/0001-80';
const isValid = validateCPFCNPJ(cpf_cnpj);

Validate State Registration

import { validateIE } from 'validations-br';
// const { validateIE } = require('validations-br');

const ie = '535.371.880.779';
const isValid = validateIE(ie, 'sp');

Validate PIS

import { validatePIS } from 'validations-br';
// const { validatePIS } = require('validations-br');

const pis = '833.28281.34-7';
const isValid = validatePIS(pis);

Validate Phone

import { validatePhone } from 'validations-br';
// const { validatePhone } = require('validations-br');

const phone = '(14) 99767-9472';
const isValid = validatePhone(phone);

Validate CEP

import { validateCEP } from 'validations-br';
// const { validateCEP } = require('validations-br');

const cep = '17280-000';
const isValid = validateCEP(cep);

Validate UF

import { validateUF } from 'validations-br';
// const { validateUF } = require('validations-br');

const uf = 'SP';
const isValid = validateUF(uf);

Bonus

Validate E-mail

import { validateEmail } from 'validations-br';
// const { validateEmail } = require('validations-br');

const email = 'johndoe@gmail.com';
const isValid = validateEmail(email);

Integrations

With Yup

import { validateCNPJ } from "validations-br";
import * as Yup from "yup";

try {
  const schema = Yup.string().test(
    "is-cnpj",
    "CNPJ is not valid",
    (value) => validateCNPJ(value)
  );
  
  await schema.validate(cnpj);
} catch(err) {
  console.log(err.message);
}

With Joi

import { validateCNPJ } from "validations-br";
import Joi from "joi";

try {
  const schema = Joi.string().custom(validateCNPJ, "Validate CNPJ");

  const { value } = schema.validate(cnpj);

  if (!value) {
    throw Error("CNPJ is not valid");
  }
} catch(err) {
  console.log(err.message);
}

Contributing

Feel free to contribute to the project. We only ask that:

  • Fork it (https://github.com/reactivando/validations-br/fork)
  • Create your feature branch (git checkout -b feature/fooBar)
  • Write tests for the new feature or bug fix that you are solving
  • Commit your changes (git commit -am 'feat: add some fooBar')
  • Follow the commit conventions of conventional-changelog described in conventional-conventions
  • Push to the branch (git push origin feature/fooBar)
  • Ensure all tests pass before send the pull-request (Use: $ yarn test or $ npm run test)
  • Create a new Pull Request

Authors

License

MIT © Reactivando