3.0.3 • Published 18 days ago

clabe-validator v3.0.3

Weekly downloads
865
License
MIT
Repository
github
Last release
18 days ago

CLABE Validator

JavaScript library to analyze or create a CLABE number for a Mexican bank account

License:MIT npm Hits Build

CLABE (Clave Bancaria Estandarizada Spanish for "standardized banking code") is a banking standard from the Mexican Bank Association (Asociación de Bancos de México ABM) for uniform numbering of bank accounts.  CLABE numbers are 18 digits long.  See: https://en.wikipedia.org/wiki/CLABE

A) Online Form

Try it out: https://centerkey.com/clabe

B) Setup

Web browser

Include in a web page:

<script src=clabe.min.js></script>

or from the jsdelivr.com CDN:

<script src=https://cdn.jsdelivr.net/npm/clabe-validator@3.0/dist/clabe.min.js></script>

Node.js server

Install package for node:

$ npm install clabe-validator

Import package:

import { clabe } from 'clabe-validator';

C) Validator Usage

Pass the CLABE number as an 18-character string into clabe.validate(clabeNum).

1. Example JavaScript code

const clabeNum =   '002010077777777771';
const clabeCheck = clabe.validate(clabeNum);
console.log(clabeCheck.ok ? '¡Que bueno!' : '¡Muy mal!');
console.log('Your bank: ' + clabeCheck.bank);

2. Example JSON result for a valid CLABE number

{
   ok:       true,
   formatOk: true,
   error:    null,
   message:  'Valid',
   clabe:    '002010077777777771',
   tag:      'BANAMEX',
   bank:     'Banco Nacional de México',
   city:     'Aguascalientes MX-AGU',
   multiple: false,
   total:    1,
   account:  '07777777777',
   code:     { bank: '002', city: '010' },
   checksum: 1,
}

3. Example JSON result for an invalid CLABE number

{
   ok:       false,
   formatOk: true,
   error:    'invalid-city',
   message:  'Invalid city code: 000',
}

The formatOk field indicates if the CLABE's length and checksum are both valid (even if the bank code or city code are unknown).

4. Possible errors

Error codeError messageFormat Ok
invalid-lengthMust be exactly 18 digits longfalse
invalid-charactersMust be only numeric digits (no letters)false
invalid-checksumInvalid checksum, last digit should be: DIGITfalse
invalid-bankInvalid bank code: CODEtrue
invalid-cityInvalid city code: CODEtrue

D) Calculator Usage

Pass the bank code, city code, and account number into clabe.calculate(bankCode, cityCode, accountNumber) and get the 18-character CLABE number back.

const clabeNum = clabe.calculate(2, 10, 7777777777);
console.log(clabeNum === '002010077777777771');  //true

E) TypeScript Declarations

See the TypeScript declarations at the top of the clabe.ts file.

The clabe.validate(clabeNum: string) function returns a ClabeCheck object:

type ClabeCheck = {
   ok:       boolean,        //todo está bien
   formatOk: boolean,        //valid length and checksum
   error:    string | null,  //failure code, example: 'invalid-city'
   message:  string,         //displayable status information
   clabe:    string | null,  //full 18-digit number
   tag:      string | null,  //bank short name, example: 'BANAMEX'
   bank:     string | null,  //bank long name, example: 'Banco Nacional'
   city:     string | null,  //branch or plaza name
   multiple: boolean,        //more than one city share the same code
   total:    number,         //number of cities
   account:  string,         //11-digit zero-padded bank account number
   code:     { bank: string, city: string },  //3-digit codes
   checksum: number | null,  //control digit (0 to 9)
   };

Example TypeScript usage with explicit types:

import { clabe, ClabeCheck } from 'clabe-validator';

const clabeNum:   string =     '002010077777777771';
const clabeCheck: ClabeCheck = clabe.validate(clabeNum);  //{ ok: true, error: null, ... }
const bankCode:   string =     clabeCheck.code.bank;      //'002'

F) Contributor Notes

To be a contributor, fork the project and run the commands npm install and npm test on your local clone.  Make your edits and rerun the tests.

Pull requests welcome.  Since the pacakge version number is updated during the release process, leave the version number unchanged.

G) Genesis

The origin of this project goes back to when I needed to send money to Guanajuato, Mexico to pay nurses who were providing medical care of a relative.  I was initially unable to transfer funds because the money transfer service reported that the CLABE number I supplied was invalid.  Through a little sleuthing and a lot of luck, I discovered that a financial services company had accidentally omitted the very last modulo operation in their CLABE checksum calculation.  The result was that valid Mexican bank account numbers with certain combinations of digits were erroneously rejected.

This project was created to fix the checksum bug.  It is an open source community project and is not supported by any company.

H) Build Environment

Check out the runScriptsConfig section in package.json for an interesting approach to organizing build tasks.

CLI Build Tools for package.json

  • 🎋 add-dist-header:  Prepend a one-line banner comment (with license notice) to distribution files
  • 📄 copy-file-util:  Copy or rename a file with optional package version number
  • 📂 copy-folder-util:  Recursively copy files from one folder to another folder
  • 🪺 recursive-exec:  Run a command on each file in a folder and its subfolders
  • 🔍 replacer-util:  Find and replace strings or template outputs in text files
  • 🔢 rev-web-assets:  Revision web asset filenames with cache busting content hash fingerprints
  • 🚆 run-scripts-util:  Organize npm package.json scripts into groups of easy to manage commands
  • 🚦 w3c-html-validator:  Check the markup validity of HTML files using the W3C validator

Feel free to submit questions at: github.com/center-key/clabe-validator/issues

CLABE Validator code is open source under the MIT License, and the documentation is published under the CC BY-SA 4.0 license.

3.0.3

18 days ago

3.0.2

3 months ago

3.0.1

6 months ago

3.0.0

10 months ago

2.2.0

11 months ago

2.1.10

11 months ago

2.1.9

12 months ago

2.1.8

1 year ago

2.1.7

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

2.1.4

2 years ago

2.1.2

2 years ago

2.1.3

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.7.4

3 years ago

1.7.3

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.4

3 years ago

1.6.5

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.8

4 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.4

5 years ago

1.3.5

5 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago