Skip to content

Commit

Permalink
Add RFC assert
Browse files Browse the repository at this point in the history
  • Loading branch information
Prado authored and franciscocardoso committed Jul 8, 2024
1 parent 618dd20 commit 4f692fe
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 @@ -53,6 +53,7 @@ The following set of extra asserts are provided by this package:
- [NullOrString](#nullorstring)
- [Phone](#phone) (requires `google-libphonenumber`)
- [PlainObject](#plainobject)
- [RfcNumber](#rfcnumber) (requires `validate-rfc`)
- [TaxpayerIdentificationNumber](#taxpayeridentificationnumber) (_TIN_, requires `tin-validator`)
- [UkModulusChecking](#ukmoduluschecking) (requires `uk-modulus-checking`)
- [Uri](#uri) (requires `urijs`)
Expand Down Expand Up @@ -222,6 +223,9 @@ Tests if the phone is valid and optionally if it belongs to the given country. T
### PlainObject
Tests if the value is a plain object.

### RfcNumber
Tests if the value is a valid RFC number.

### TaxpayerIdentificationNumber
Tests 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).

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"uk-modulus-checking": "0.0.3",
"uphold-scripts": "^0.5.0",
"urijs": "^1.17.1",
"validate-rfc": "^2.0.3",
"validator": "^13.7.0"
},
"engines": {
Expand All @@ -75,6 +76,7 @@
"tin-validator": ">=1.0.0 <2.0.0",
"uk-modulus-checking": "0.0.2",
"urijs": ">=1 <2",
"validate-rfc": "^2.0.3",
"validator": ">=3 <6"
},
"pre-commit": [
Expand Down
53 changes: 53 additions & 0 deletions src/asserts/rfc-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 validateRfc;

/**
* Optional peer dependencies.
*/

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

/**
* Export `RfcNumber`.
*/

module.exports = function rfcNumberAssert() {
if (!validateRfc) {
throw new Error('validate-rfc is not installed');
}

/**
* Class name.
*/

this.__class__ = 'RfcNumber';

/**
* Validation algorithm.
*/

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

if (!validateRfc(value).isValid) {
throw new Violation(this, value, { value: 'must_be_a_valid_rfc_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 @@ -37,6 +37,7 @@ const NullOrDate = require('./asserts/null-or-date-assert.js');
const NullOrString = require('./asserts/null-or-string-assert.js');
const Phone = require('./asserts/phone-assert.js');
const PlainObject = require('./asserts/plain-object-assert.js');
const RfcNumber = require('./asserts/rfc-number-assert.js');
const TaxpayerIdentificationNumber = require('./asserts/taxpayer-identification-number-assert.js');
const UkModulusChecking = require('./asserts/uk-modulus-checking-assert.js');
const Uri = require('./asserts/uri-assert.js');
Expand Down Expand Up @@ -82,6 +83,7 @@ module.exports = {
NullOrString,
Phone,
PlainObject,
RfcNumber,
TaxpayerIdentificationNumber,
UkModulusChecking,
Uri,
Expand Down
51 changes: 51 additions & 0 deletions test/asserts/rfc-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 RfcNumberAssert = require('../../src/asserts/rfc-number-assert');

/**
* Extend `Assert` with `RfcNumberAssert`.
*/

const Assert = BaseAssert.extend({
RfcNumber: RfcNumberAssert
});

/**
* Test `RfcNumberAssert`.
*/

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

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

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

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

it('should accept a valid `rfc`', () => {
Assert.rfcNumber().validate('mhtr93041179a');
});
});
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(39);
expect(assertNames).toHaveLength(40);
expect(assertNames).toEqual(
expect.arrayContaining([
'AbaRoutingNumber',
Expand Down Expand Up @@ -50,6 +50,7 @@ describe('validator.js-asserts', () => {
'NullOrString',
'Phone',
'PlainObject',
'RfcNumber',
'TaxpayerIdentificationNumber',
'UkModulusChecking',
'Uri',
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4812,6 +4812,11 @@ v8-to-istanbul@^8.1.0:
convert-source-map "^1.6.0"
source-map "^0.7.3"

validate-rfc@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/validate-rfc/-/validate-rfc-2.0.3.tgz#fc91a02ab0d7f513a25ed4f53d9d6a69a38ad04f"
integrity sha512-WS7CyAz/sfzx6DryOPZvprqz7uxHcQh1yhzDunNX1vidSmprfN7Og66VRpCyU21U82Vzkof5/bOIOKan3dEXLA==

validator.js@^2.0.0:
version "2.0.4"
resolved "https://registry.npmjs.org/validator.js/-/validator.js-2.0.4.tgz"
Expand Down

0 comments on commit 4f692fe

Please sign in to comment.