-
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.
Add
NullOrAssert
to allow any assert to be nullable
- Loading branch information
1 parent
d916e15
commit c736b90
Showing
5 changed files
with
156 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,41 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Export `NullOrAssert`. | ||
*/ | ||
|
||
module.exports = function nullOrAssert(assert) { | ||
/** | ||
* Class name. | ||
*/ | ||
|
||
this.__class__ = 'NullOr'; | ||
|
||
if (typeof assert !== 'object') { | ||
throw new Error('Assert must be an object.'); | ||
} | ||
|
||
if (typeof assert.validate !== 'function') { | ||
throw new Error('Assert must have a validate function.'); | ||
} | ||
|
||
/** | ||
* Nullable assert. | ||
*/ | ||
|
||
this.assert = assert; | ||
|
||
/** | ||
* Validation algorithm. | ||
*/ | ||
|
||
this.validate = value => { | ||
if (value === null) { | ||
return true; | ||
} | ||
|
||
return this.assert.validate(value); | ||
}; | ||
|
||
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,100 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { Assert: BaseAssert, Violation } = require('validator.js'); | ||
const NullOrAssert = require('../../src/asserts/null-or-assert'); | ||
const UuidAssert = require('../../src/asserts/uuid-assert'); | ||
|
||
/** | ||
* Extend `Assert` with `NullOrAssert`. | ||
*/ | ||
|
||
const Assert = BaseAssert.extend({ | ||
NullOr: NullOrAssert, | ||
Uuid: UuidAssert | ||
}); | ||
|
||
/** | ||
* Test `NullOrAssert`. | ||
*/ | ||
|
||
describe('NullOrAssert', () => { | ||
it('should throw an error if the specified assert is missing', () => { | ||
try { | ||
Assert.nullOr('foo').validate(); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Error); | ||
expect(e.message).toBe('Assert must be an object.'); | ||
} | ||
}); | ||
|
||
it('should throw an error if the specified assert is not valid', () => { | ||
try { | ||
Assert.nullOr('foo').validate('bar'); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Error); | ||
expect(e.message).toBe('Assert must be an object.'); | ||
} | ||
}); | ||
|
||
it('should throw an error if the specified assert has no `validate` function', () => { | ||
try { | ||
Assert.nullOr({}).validate(123); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Error); | ||
expect(e.message).toBe('Assert must have a validate function.'); | ||
} | ||
}); | ||
|
||
it('should throw an error if the specified assert has a `validate` property that is not a function', () => { | ||
try { | ||
Assert.nullOr({ validate: true }).validate(123); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Error); | ||
expect(e.message).toBe('Assert must have a validate function.'); | ||
} | ||
}); | ||
|
||
it('should throw an error if the value is not null and is not valid for the specified assert', () => { | ||
try { | ||
Assert.nullOr(Assert.string()).validate(123); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.show().assert).toBe('IsString'); | ||
expect(e.violation.value).toBe('must_be_a_string'); | ||
} | ||
}); | ||
|
||
it('should include the arguments of the specified assert', () => { | ||
try { | ||
Assert.nullOr(Assert.uuid(4)).validate('foobar'); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Object); | ||
expect(e.show().assert).toBe('Uuid'); | ||
expect(e.violation.version).toBe(4); | ||
} | ||
}); | ||
|
||
it('should accept a null value', () => { | ||
Assert.nullOr(Assert.string()).validate(null); | ||
}); | ||
|
||
it('should accept a value that is valid for the specified assert', () => { | ||
Assert.nullOr(Assert.string()).validate('foobar'); | ||
}); | ||
}); |
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