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 @@ -53,6 +53,7 @@ The following set of extra asserts are provided by this package:
5353- [ NullOrString] ( #nullorstring )
5454- [ Phone] ( #phone ) (requires ` google-libphonenumber ` )
5555- [ PlainObject] ( #plainobject )
56+ - [ RfcNumber] ( #rfcnumber ) (requires ` validate-rfc ` )
5657- [ TaxpayerIdentificationNumber] ( #taxpayeridentificationnumber ) (_ TIN_ , requires ` tin-validator ` )
5758- [ UkModulusChecking] ( #ukmoduluschecking ) (requires ` uk-modulus-checking ` )
5859- [ Uri] ( #uri ) (requires ` urijs ` )
@@ -222,6 +223,9 @@ Tests if the phone is valid and optionally if it belongs to the given country. T
222223### PlainObject
223224Tests if the value is a plain object.
224225
226+ ### RfcNumber
227+ Tests if the value is a valid RFC number.
228+
225229### TaxpayerIdentificationNumber
226230Tests if the value is a valid Taxpayer Identification Number (_ TIN_ ) as defined by the [ U.S. IRS] ( http://www.irs.gov/Individuals/International-Taxpayers/Taxpayer-Identification-Numbers-TIN ) .
227231
Original file line number Diff line number Diff line change 5757 "uk-modulus-checking" : " 0.0.3" ,
5858 "uphold-scripts" : " ^0.5.0" ,
5959 "urijs" : " ^1.17.1" ,
60+ "validate-rfc" : " ^2.0.3" ,
6061 "validator" : " ^13.7.0"
6162 },
6263 "engines" : {
7576 "tin-validator" : " >=1.0.0 <2.0.0" ,
7677 "uk-modulus-checking" : " 0.0.2" ,
7778 "urijs" : " >=1 <2" ,
79+ "validate-rfc" : " ^2.0.3" ,
7880 "validator" : " >=3 <6"
7981 },
8082 "pre-commit" : [
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 validateRfc ;
10+
11+ /**
12+ * Optional peer dependencies.
13+ */
14+
15+ try {
16+ validateRfc = require ( 'validate-rfc' ) ;
17+ } catch ( e ) {
18+ // eslint-disable-next-line no-empty
19+ }
20+
21+ /**
22+ * Export `RfcNumber`.
23+ */
24+
25+ module . exports = function rfcNumberAssert ( ) {
26+ if ( ! validateRfc ) {
27+ throw new Error ( 'validate-rfc is not installed' ) ;
28+ }
29+
30+ /**
31+ * Class name.
32+ */
33+
34+ this . __class__ = 'RfcNumber' ;
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 ( ! validateRfc ( value ) . isValid ) {
46+ throw new Violation ( this , value , { value : 'must_be_a_valid_rfc_number' } ) ;
47+ }
48+
49+ return true ;
50+ } ;
51+
52+ return this ;
53+ } ;
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ const NullOrDate = require('./asserts/null-or-date-assert.js');
3737const NullOrString = require ( './asserts/null-or-string-assert.js' ) ;
3838const Phone = require ( './asserts/phone-assert.js' ) ;
3939const PlainObject = require ( './asserts/plain-object-assert.js' ) ;
40+ const RfcNumber = require ( './asserts/rfc-number-assert.js' ) ;
4041const TaxpayerIdentificationNumber = require ( './asserts/taxpayer-identification-number-assert.js' ) ;
4142const UkModulusChecking = require ( './asserts/uk-modulus-checking-assert.js' ) ;
4243const Uri = require ( './asserts/uri-assert.js' ) ;
@@ -82,6 +83,7 @@ module.exports = {
8283 NullOrString,
8384 Phone,
8485 PlainObject,
86+ RfcNumber,
8587 TaxpayerIdentificationNumber,
8688 UkModulusChecking,
8789 Uri,
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 RfcNumberAssert = require ( '../../src/asserts/rfc-number-assert' ) ;
9+
10+ /**
11+ * Extend `Assert` with `RfcNumberAssert`.
12+ */
13+
14+ const Assert = BaseAssert . extend ( {
15+ RfcNumber : RfcNumberAssert
16+ } ) ;
17+
18+ /**
19+ * Test `RfcNumberAssert`.
20+ */
21+
22+ describe ( 'RfcNumberAssert' , ( ) => {
23+ it ( 'should throw an error if the input value is not a string' , ( ) => {
24+ try {
25+ Assert . rfcNumber ( ) . validate ( ) ;
26+
27+ fail ( ) ;
28+ } catch ( e ) {
29+ expect ( e ) . toBeInstanceOf ( Violation ) ;
30+ expect ( e . show ( ) . assert ) . toBe ( 'RfcNumber' ) ;
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 `rfc` is invalid' , ( ) => {
37+ try {
38+ Assert . rfcNumber ( ) . 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_rfc_number' ) ;
45+ }
46+ } ) ;
47+
48+ it ( 'should accept a valid `rfc`' , ( ) => {
49+ Assert . rfcNumber ( ) . validate ( 'mhtr93041179a' ) ;
50+ } ) ;
51+ } ) ;
Original file line number Diff line number Diff 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 ( 39 ) ;
17+ expect ( assertNames ) . toHaveLength ( 40 ) ;
1818 expect ( assertNames ) . toEqual (
1919 expect . arrayContaining ( [
2020 'AbaRoutingNumber' ,
@@ -50,6 +50,7 @@ describe('validator.js-asserts', () => {
5050 'NullOrString' ,
5151 'Phone' ,
5252 'PlainObject' ,
53+ 'RfcNumber' ,
5354 'TaxpayerIdentificationNumber' ,
5455 'UkModulusChecking' ,
5556 'Uri' ,
Original file line number Diff line number Diff line change @@ -4812,6 +4812,11 @@ v8-to-istanbul@^8.1.0:
48124812 convert-source-map "^1.6.0"
48134813 source-map "^0.7.3"
48144814
4815+ validate-rfc@^2.0.3 :
4816+ version "2.0.3"
4817+ resolved "https://registry.yarnpkg.com/validate-rfc/-/validate-rfc-2.0.3.tgz#fc91a02ab0d7f513a25ed4f53d9d6a69a38ad04f"
4818+ integrity sha512-WS7CyAz/sfzx6DryOPZvprqz7uxHcQh1yhzDunNX1vidSmprfN7Og66VRpCyU21U82Vzkof5/bOIOKan3dEXLA==
4819+
48154820validator.js@^2.0.0 :
48164821 version "2.0.4"
48174822 resolved "https://registry.npmjs.org/validator.js/-/validator.js-2.0.4.tgz"
You can’t perform that action at this time.
0 commit comments