File tree Expand file tree Collapse file tree 7 files changed +119
-1
lines changed Expand file tree Collapse file tree 7 files changed +119
-1
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ The following set of extra asserts are provided by this package:
32
32
- [ Callback] ( #callback ) (requires ` callback ` )
33
33
- [ CpfNumber] ( #cpfnumber ) (requires ` cpf ` )
34
34
- [ CreditCard] ( #creditcard ) (requires ` creditcard ` )
35
+ - [ CurpNumber] ( #curpnumber ) (requires ` curp ` )
35
36
- [ Date] ( #date ) (requires ` moment ` for format validation only)
36
37
- [ DateDiffGreaterThan] ( #datediffgreaterthan ) (requires ` moment ` )
37
38
- [ DateDiffGreaterThanOrEqualTo] ( #datediffgreaterthanorequalto ) (requires ` moment ` )
@@ -114,6 +115,9 @@ Tests if the value is valid CPF number.
114
115
### CreditCard
115
116
Tests if the value is a valid credit card number using the Luhn10 algorithm.
116
117
118
+ ### CurpNumber
119
+ Tests if the value is valid CURP number.
120
+
117
121
### Date
118
122
Tests if the value is a valid date.
119
123
Original file line number Diff line number Diff line change 47
47
"bignumber.js" : " ^9.0.0" ,
48
48
"cpf" : " ^2.0.1" ,
49
49
"creditcard" : " ^0.1.2" ,
50
+ "curp" : " ^1.2.3" ,
50
51
"google-libphonenumber" : " ^1.0.18" ,
51
52
"iban" : " 0.0.6" ,
52
53
"isoc" : " 0.0.1" ,
66
67
"bignumber.js" : " >=7 <=9.0.0" ,
67
68
"cpf" : " ^2.0.1" ,
68
69
"creditcard" : " >=0.0.1 <1.0.0" ,
70
+ "curp" : " ^1.2.3" ,
69
71
"google-libphonenumber" : " >=1 <4" ,
70
72
"iban" : " >=0.0.6 <1.0.0" ,
71
73
"isoc" : " >=0.0.1 <1.0.0" ,
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ const Boolean = require('./asserts/boolean-assert.js');
16
16
const Callback = require ( './asserts/callback-assert' ) ;
17
17
const CpfNumber = require ( './asserts/cpf-number-assert' ) ;
18
18
const CreditCard = require ( './asserts/credit-card-assert.js' ) ;
19
+ const CurpNumber = require ( './asserts/curp-number-assert.js' ) ;
19
20
const Date = require ( './asserts/date-assert.js' ) ;
20
21
const DateDiffGreaterThan = require ( './asserts/date-diff-greater-than-assert.js' ) ;
21
22
const DateDiffGreaterThanOrEqualTo = require ( './asserts/date-diff-greater-than-or-equal-to-assert.js' ) ;
@@ -60,6 +61,7 @@ module.exports = {
60
61
Callback,
61
62
CpfNumber,
62
63
CreditCard,
64
+ CurpNumber,
63
65
Date,
64
66
DateDiffGreaterThan,
65
67
DateDiffGreaterThanOrEqualTo,
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ describe('validator.js-asserts', () => {
14
14
it ( 'should export all asserts' , ( ) => {
15
15
const assertNames = Object . keys ( asserts ) ;
16
16
17
- expect ( assertNames ) . toHaveLength ( 38 ) ;
17
+ expect ( assertNames ) . toHaveLength ( 39 ) ;
18
18
expect ( assertNames ) . toEqual (
19
19
expect . arrayContaining ( [
20
20
'AbaRoutingNumber' ,
@@ -29,6 +29,7 @@ describe('validator.js-asserts', () => {
29
29
'Callback' ,
30
30
'CpfNumber' ,
31
31
'CreditCard' ,
32
+ 'CurpNumber' ,
32
33
'Date' ,
33
34
'DateDiffGreaterThan' ,
34
35
'DateDiffGreaterThanOrEqualTo' ,
Original file line number Diff line number Diff line change @@ -1141,6 +1141,11 @@ cssstyle@^2.3.0:
1141
1141
dependencies :
1142
1142
cssom "~0.3.6"
1143
1143
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
+
1144
1149
data-urls@^2.0.0 :
1145
1150
version "2.0.0"
1146
1151
resolved "https://registry.npmjs.org/data-urls/-/data-urls-2.0.0.tgz"
You can’t perform that action at this time.
0 commit comments