2.0.3 • Published 10 months ago

validate-rfc v2.0.3

Weekly downloads
57
License
MIT
Repository
github
Last release
10 months ago

Validate RFC

npm.io npm.io npm.io

A simple and lightweight library to validate Mexican RFCs (Tax ID).

Install

NodeJS

Use NPM:

$ npm install --save validate-rfc

Or YARN:

$ yarn add validate-rfc

Browser

Add the script to your project:

<!-- Latest version -->
<script src="https://cdn.jsdelivr.net/gh/manuelmhtr/validate-rfc@latest/dist/index.js" type="text/javascript"></script>

<!-- Or specify a version -->
<script src="https://cdn.jsdelivr.net/gh/manuelmhtr/validate-rfc@v2.0.2/dist/index.js" type="text/javascript"></script>

<!-- This will export a global function "validateRfc": -->
<script type="text/javascript">
  var data = validateRfc('mhtr93041179a');
  console.log(data);
</script>

API

The library only exposes a single function (.validateRfc).

.validateRfc(rfc)

Checks whether a string is a valid RFC and returns validation details.

Parameters

ParameterTypeDescription
rfcStringThe RFC to be validated.
optionsObjectSettings (Optional).
options.omitVerificationDigitBooleanWhen true, the Check digit is omitted from the validation (Default: false). This option is useful since there are some RFCs with an invalid Check digit that are recognized as valid by the Mexican authorities (See Known issues).

Response

It returns a plain object with the values:

ParameterTypeDescription
isValidBooleanIndicates if the string is a valid RFC.
rfcStringThe formatted RFC (uppercase, with no white spaces or symbols). Returns null when input is an invalid RFC.
typeStringThe classification of the provided RFC. Values can be person for a legal person, company for companies, generic for the generic RFC "XAXX010101000" ir foreign for the RFC "XEXX010101000" which is used by foreign people. Returns null when input is an invalid RFC.
errorsArrayStringIn case the RFC is invalid, the reasons why the RFC is invalid will be listed here.

Possible errors values and they description are:

ErrorDescripción
INVALID_FORMATThe format is invalid, that means, the string does not meet with the required length or expected structure. Eg: XYZ because clearly is not an RFC.
INVALID_DATEThe string may have the correct format, but digits generate an invalid date. Eg: MHTR815511A70 because it refers to month 55.
INVALID_VERIFICATION_DIGITThe string has a valid format, but the last character (check digit) is invalid. Eg: MHTR810511A79 ends with 9 but it is expected to end with 2.
FORBIDDEN_WORDThe string contains one of the inconvenient words that cannot be included in an RFC. Eg: FETO930411792 the initials make the word FETO (fetus, LOL).

Example

const validateRfc = require('validate-rfc');

const response = validateRfc('mhtr93041179a');
console.log(response);

/*
Prints:

{
  isValid: true,
  rfc: 'MHTR93041179A',
  type: 'person'
}

*/

const response = validateRfc('This is not an RFC');
console.log(response);

/*
Prints:

{
  isValid: false,
  rfc: null,
  type: null,
  errors: ['INVALID_FORMAT']
}

*/

Tests

Run the test with the command:

$ yarn test

Known issues

Check digit mismatches

Some RFCs registered by the SAT (Mexican taxing bureau) does not pass the Check digit validation. For instance, LME060822IH5 is a valid RFC and able to be invoices according the SAT official validator, however its Check digit should be 3 rather than 5. Therefore in those cases, this library returns isValid as false and the error INVALID_VERIFICATION_DIGIT.

Solutions: 1. If you find one of these cases, add it to the list of valid RFCs. Upper-cased and sorted alphabetically. By adding them to the list you are supporting the documentation of such cases and preventing from returning an error the next time. 2. Use the option omitVerificationDigit to ignore the Check digit validation. It has the advantage of being a faster solution and covers all the cases, but many invalid RFCs will pass the validation.

References:

Used by

Used by tens of successful teams:

ZenfiYoteprestoDeAcero

Related

Licencia

MIT