-
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
1bef034
commit fb3f9df
Showing
5 changed files
with
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { Violation } = require('validator.js'); | ||
|
||
/** | ||
* Export `NullOrBooleanAssert`. | ||
*/ | ||
|
||
module.exports = function nullOrBooleanAssert() { | ||
/** | ||
* Class name. | ||
*/ | ||
|
||
this.__class__ = 'NullOrBoolean'; | ||
|
||
/** | ||
* Validation algorithm. | ||
*/ | ||
|
||
this.validate = value => { | ||
if (value !== null && typeof value !== 'boolean') { | ||
throw new Violation(this, value, { value: 'must_be_null_or_a_boolean' }); | ||
} | ||
|
||
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,59 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { Assert: BaseAssert, Violation } = require('validator.js'); | ||
const NullOrBooleanAssert = require('../../src/asserts/null-or-boolean-assert'); | ||
|
||
/** | ||
* Extend `Assert` with `NullOrBooleanAssert`. | ||
*/ | ||
|
||
const Assert = BaseAssert.extend({ | ||
NullOrBoolean: NullOrBooleanAssert | ||
}); | ||
|
||
/** | ||
* Test `NullOrBooleanAssert`. | ||
*/ | ||
|
||
describe('NullOrBooleanAssert', () => { | ||
it('should throw an error if the input value is not a `null` or a boolean', () => { | ||
const choices = [[], {}, 123, new Boolean(true), 'foo']; // eslint-disable-line no-new-wrappers | ||
|
||
choices.forEach(choice => { | ||
try { | ||
Assert.nullOrBoolean().validate(choice); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.violation.value).toBe('must_be_null_or_a_boolean'); | ||
} | ||
}); | ||
}); | ||
|
||
it('should expose `assert` equal to `NullOrBoolean`', () => { | ||
try { | ||
Assert.nullOrBoolean().validate({}); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e.show().assert).toBe('NullOrBoolean'); | ||
} | ||
}); | ||
|
||
it('should accept `null`', () => { | ||
Assert.nullOrBoolean().validate(null); | ||
}); | ||
|
||
it('should accept a `true` boolean value', () => { | ||
Assert.nullOrBoolean().validate(true); | ||
}); | ||
|
||
it('should accept a `false` boolean value', () => { | ||
Assert.nullOrBoolean().validate(false); | ||
}); | ||
}); |
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