Skip to content

Commit

Permalink
Add CURP assert
Browse files Browse the repository at this point in the history
  • Loading branch information
Prado authored and franciscocardoso committed Jul 2, 2024
1 parent b37a5d9 commit e914426
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following set of extra asserts are provided by this package:
- [Callback](#callback) (requires `callback`)
- [CpfNumber](#cpfnumber) (requires `cpf`)
- [CreditCard](#creditcard) (requires `creditcard`)
- [CurpNumber](#curpnumber) (requires `curp`)
- [Date](#date) (requires `moment` for format validation only)
- [DateDiffGreaterThan](#datediffgreaterthan) (requires `moment`)
- [DateDiffGreaterThanOrEqualTo](#datediffgreaterthanorequalto) (requires `moment`)
Expand Down Expand Up @@ -114,6 +115,9 @@ Tests if the value is valid CPF number.
### CreditCard
Tests if the value is a valid credit card number using the Luhn10 algorithm.

### CurpNumber
Tests if the value is valid CURP number.

### Date
Tests if the value is a valid date.

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"bignumber.js": "^9.0.0",
"cpf": "^2.0.1",
"creditcard": "^0.1.2",
"curp": "^1.2.3",
"google-libphonenumber": "^1.0.18",
"iban": "0.0.6",
"isoc": "0.0.1",
Expand All @@ -66,6 +67,7 @@
"bignumber.js": ">=7 <=9.0.0",
"cpf": "^2.0.1",
"creditcard": ">=0.0.1 <1.0.0",
"curp": "^1.2.3",
"google-libphonenumber": ">=1 <4",
"iban": ">=0.0.6 <1.0.0",
"isoc": ">=0.0.1 <1.0.0",
Expand Down
53 changes: 53 additions & 0 deletions src/asserts/curp-number-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

/**
* Module dependencies.
*/

const _ = require('lodash');
const { Validator, Violation } = require('validator.js');
let curp;

/**
* Optional peer dependencies.
*/

try {
curp = require('curp');
} catch (e) {
// eslint-disable-next-line no-empty
}

/**
* Export `CurpNumber`.
*/

module.exports = function curpNumberAssert() {
if (!curp) {
throw new Error('curp is not installed');
}

/**
* Class name.
*/

this.__class__ = 'CurpNumber';

/**
* Validation algorithm.
*/

this.validate = function (value) {
if (!_.isString(value)) {
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
}

if (!curp.validar(value)) {
throw new Violation(this, value, { value: 'must_be_a_valid_curp_number' });
}

return true;
};

return this;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Boolean = require('./asserts/boolean-assert.js');
const Callback = require('./asserts/callback-assert');
const CpfNumber = require('./asserts/cpf-number-assert');
const CreditCard = require('./asserts/credit-card-assert.js');
const CurpNumber = require('./asserts/curp-number-assert.js');
const Date = require('./asserts/date-assert.js');
const DateDiffGreaterThan = require('./asserts/date-diff-greater-than-assert.js');
const DateDiffGreaterThanOrEqualTo = require('./asserts/date-diff-greater-than-or-equal-to-assert.js');
Expand Down Expand Up @@ -60,6 +61,7 @@ module.exports = {
Callback,
CpfNumber,
CreditCard,
CurpNumber,
Date,
DateDiffGreaterThan,
DateDiffGreaterThanOrEqualTo,
Expand Down
51 changes: 51 additions & 0 deletions test/asserts/curp-number-assert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

/**
* Module dependencies.
*/

const { Assert: BaseAssert, Violation } = require('validator.js');
const CurpNumberAssert = require('../../src/asserts/curp-number-assert');

/**
* Extend `Assert` with `CurpNumberAssert`.
*/

const Assert = BaseAssert.extend({
CurpNumber: CurpNumberAssert
});

/**
* Test `CurpNumberAssert`.
*/

describe('CurpNumberAssert', () => {
it('should throw an error if the input value is not a string', () => {
try {
Assert.curpNumber().validate();

fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.show().assert).toBe('CurpNumber');
expect(e.value).toBeUndefined();
expect(e.violation.value).toBe('must_be_a_string');
}
});

it('should throw an error if `curp` is invalid', () => {
try {
Assert.curpNumber().validate('123');

fail();
} catch (e) {
expect(e).toBeInstanceOf(Violation);
expect(e.value).toBe('123');
expect(e.violation.value).toBe('must_be_a_valid_curp_number');
}
});

it('should accept a valid `curp`', () => {
Assert.curpNumber().validate('LOOA531113HTCPBN07');
});
});
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
it('should export all asserts', () => {
const assertNames = Object.keys(asserts);

expect(assertNames).toHaveLength(38);
expect(assertNames).toHaveLength(39);
expect(assertNames).toEqual(
expect.arrayContaining([
'AbaRoutingNumber',
Expand All @@ -29,6 +29,7 @@ describe('validator.js-asserts', () => {
'Callback',
'CpfNumber',
'CreditCard',
'CurpNumber',
'Date',
'DateDiffGreaterThan',
'DateDiffGreaterThanOrEqualTo',
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,11 @@ cssstyle@^2.3.0:
dependencies:
cssom "~0.3.6"

curp@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/curp/-/curp-1.2.3.tgz#8db13da2d64073df1da9c1f94e673e829df28d32"
integrity sha512-o/NZE+1A1y77orlN8kBRa4+yG4m1owVI9V2KzEpHDV1z2k90rPCHuECYmXLyW4bcYOHdTf36EJuz6KWLn/hyKg==

data-urls@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/data-urls/-/data-urls-2.0.0.tgz"
Expand Down

0 comments on commit e914426

Please sign in to comment.