-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
618dd20
commit 4f692fe
Showing
7 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters