Skip to content

Commit e914426

Browse files
Pradofranciscocardoso
authored andcommitted
Add CURP assert
1 parent b37a5d9 commit e914426

File tree

7 files changed

+119
-1
lines changed

7 files changed

+119
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The following set of extra asserts are provided by this package:
3232
- [Callback](#callback) (requires `callback`)
3333
- [CpfNumber](#cpfnumber) (requires `cpf`)
3434
- [CreditCard](#creditcard) (requires `creditcard`)
35+
- [CurpNumber](#curpnumber) (requires `curp`)
3536
- [Date](#date) (requires `moment` for format validation only)
3637
- [DateDiffGreaterThan](#datediffgreaterthan) (requires `moment`)
3738
- [DateDiffGreaterThanOrEqualTo](#datediffgreaterthanorequalto) (requires `moment`)
@@ -114,6 +115,9 @@ Tests if the value is valid CPF number.
114115
### CreditCard
115116
Tests if the value is a valid credit card number using the Luhn10 algorithm.
116117

118+
### CurpNumber
119+
Tests if the value is valid CURP number.
120+
117121
### Date
118122
Tests if the value is a valid date.
119123

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"bignumber.js": "^9.0.0",
4848
"cpf": "^2.0.1",
4949
"creditcard": "^0.1.2",
50+
"curp": "^1.2.3",
5051
"google-libphonenumber": "^1.0.18",
5152
"iban": "0.0.6",
5253
"isoc": "0.0.1",
@@ -66,6 +67,7 @@
6667
"bignumber.js": ">=7 <=9.0.0",
6768
"cpf": "^2.0.1",
6869
"creditcard": ">=0.0.1 <1.0.0",
70+
"curp": "^1.2.3",
6971
"google-libphonenumber": ">=1 <4",
7072
"iban": ">=0.0.6 <1.0.0",
7173
"isoc": ">=0.0.1 <1.0.0",

src/asserts/curp-number-assert.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const _ = require('lodash');
8+
const { Validator, Violation } = require('validator.js');
9+
let curp;
10+
11+
/**
12+
* Optional peer dependencies.
13+
*/
14+
15+
try {
16+
curp = require('curp');
17+
} catch (e) {
18+
// eslint-disable-next-line no-empty
19+
}
20+
21+
/**
22+
* Export `CurpNumber`.
23+
*/
24+
25+
module.exports = function curpNumberAssert() {
26+
if (!curp) {
27+
throw new Error('curp is not installed');
28+
}
29+
30+
/**
31+
* Class name.
32+
*/
33+
34+
this.__class__ = 'CurpNumber';
35+
36+
/**
37+
* Validation algorithm.
38+
*/
39+
40+
this.validate = function (value) {
41+
if (!_.isString(value)) {
42+
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
43+
}
44+
45+
if (!curp.validar(value)) {
46+
throw new Violation(this, value, { value: 'must_be_a_valid_curp_number' });
47+
}
48+
49+
return true;
50+
};
51+
52+
return this;
53+
};

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const Boolean = require('./asserts/boolean-assert.js');
1616
const Callback = require('./asserts/callback-assert');
1717
const CpfNumber = require('./asserts/cpf-number-assert');
1818
const CreditCard = require('./asserts/credit-card-assert.js');
19+
const CurpNumber = require('./asserts/curp-number-assert.js');
1920
const Date = require('./asserts/date-assert.js');
2021
const DateDiffGreaterThan = require('./asserts/date-diff-greater-than-assert.js');
2122
const DateDiffGreaterThanOrEqualTo = require('./asserts/date-diff-greater-than-or-equal-to-assert.js');
@@ -60,6 +61,7 @@ module.exports = {
6061
Callback,
6162
CpfNumber,
6263
CreditCard,
64+
CurpNumber,
6365
Date,
6466
DateDiffGreaterThan,
6567
DateDiffGreaterThanOrEqualTo,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const { Assert: BaseAssert, Violation } = require('validator.js');
8+
const CurpNumberAssert = require('../../src/asserts/curp-number-assert');
9+
10+
/**
11+
* Extend `Assert` with `CurpNumberAssert`.
12+
*/
13+
14+
const Assert = BaseAssert.extend({
15+
CurpNumber: CurpNumberAssert
16+
});
17+
18+
/**
19+
* Test `CurpNumberAssert`.
20+
*/
21+
22+
describe('CurpNumberAssert', () => {
23+
it('should throw an error if the input value is not a string', () => {
24+
try {
25+
Assert.curpNumber().validate();
26+
27+
fail();
28+
} catch (e) {
29+
expect(e).toBeInstanceOf(Violation);
30+
expect(e.show().assert).toBe('CurpNumber');
31+
expect(e.value).toBeUndefined();
32+
expect(e.violation.value).toBe('must_be_a_string');
33+
}
34+
});
35+
36+
it('should throw an error if `curp` is invalid', () => {
37+
try {
38+
Assert.curpNumber().validate('123');
39+
40+
fail();
41+
} catch (e) {
42+
expect(e).toBeInstanceOf(Violation);
43+
expect(e.value).toBe('123');
44+
expect(e.violation.value).toBe('must_be_a_valid_curp_number');
45+
}
46+
});
47+
48+
it('should accept a valid `curp`', () => {
49+
Assert.curpNumber().validate('LOOA531113HTCPBN07');
50+
});
51+
});

test/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
1414
it('should export all asserts', () => {
1515
const assertNames = Object.keys(asserts);
1616

17-
expect(assertNames).toHaveLength(38);
17+
expect(assertNames).toHaveLength(39);
1818
expect(assertNames).toEqual(
1919
expect.arrayContaining([
2020
'AbaRoutingNumber',
@@ -29,6 +29,7 @@ describe('validator.js-asserts', () => {
2929
'Callback',
3030
'CpfNumber',
3131
'CreditCard',
32+
'CurpNumber',
3233
'Date',
3334
'DateDiffGreaterThan',
3435
'DateDiffGreaterThanOrEqualTo',

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,11 @@ cssstyle@^2.3.0:
11411141
dependencies:
11421142
cssom "~0.3.6"
11431143

1144+
curp@^1.2.3:
1145+
version "1.2.3"
1146+
resolved "https://registry.yarnpkg.com/curp/-/curp-1.2.3.tgz#8db13da2d64073df1da9c1f94e673e829df28d32"
1147+
integrity sha512-o/NZE+1A1y77orlN8kBRa4+yG4m1owVI9V2KzEpHDV1z2k90rPCHuECYmXLyW4bcYOHdTf36EJuz6KWLn/hyKg==
1148+
11441149
data-urls@^2.0.0:
11451150
version "2.0.0"
11461151
resolved "https://registry.npmjs.org/data-urls/-/data-urls-2.0.0.tgz"

0 commit comments

Comments
 (0)