-
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
890116b
commit c1ae7d9
Showing
5 changed files
with
154 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,52 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
const { Violation } = require('validator.js'); | ||
|
||
/** | ||
* Export `CallbackAssert`. | ||
*/ | ||
|
||
module.exports = function(fn, customClass) { | ||
/** | ||
* Class name. | ||
*/ | ||
|
||
this.__class__ = customClass || 'Callback'; | ||
|
||
if (!_.isFunction(fn)) { | ||
throw new Error('Callback must be instantiated with a function'); | ||
} | ||
|
||
/** | ||
* Fn. | ||
*/ | ||
|
||
this.fn = fn; | ||
|
||
/** | ||
* Validation algorithm. | ||
*/ | ||
|
||
this.validate = function(value) { | ||
let result; | ||
|
||
try { | ||
result = this.fn(value); | ||
} catch (error) { | ||
throw new Violation(this, value, { error }); | ||
} | ||
|
||
if (result !== true) { | ||
throw new Violation(this, value, { result }); | ||
} | ||
|
||
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,90 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
const { Assert: BaseAssert, Violation } = require('validator.js'); | ||
const CallbackAssert = require('../../src/asserts/callback-assert'); | ||
|
||
/** | ||
* Extend `Assert` with `CallbackAssert`. | ||
*/ | ||
|
||
const Assert = BaseAssert.extend({ | ||
Callback: CallbackAssert | ||
}); | ||
|
||
/** | ||
* Test `CallbackAssert`. | ||
*/ | ||
|
||
describe('CallbackAssert', () => { | ||
it('should throw an error if `value` is missing', () => { | ||
try { | ||
Assert.callback().validate(); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e.message).toEqual('Callback must be instantiated with a function'); | ||
} | ||
}); | ||
|
||
it('should throw an error if `value` is not a function', () => { | ||
try { | ||
Assert.callback().validate('foobar'); | ||
|
||
fail(); | ||
} catch (e) { | ||
expect(e.message).toEqual('Callback must be instantiated with a function'); | ||
} | ||
}); | ||
|
||
it('should throw an error if the given function is invalid', () => { | ||
try { | ||
// eslint-disable-next-line no-undef | ||
Assert.callback(() => thisFunctionDoesNotExist()).validate('foobar'); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.show().assert).toEqual('Callback'); | ||
expect(e.show().value).toEqual('foobar'); | ||
expect(e.show().violation).not.toBeUndefined(); | ||
expect(e.show().violation.error).toBeInstanceOf(ReferenceError); | ||
expect(e.show().violation.error.message).toEqual('thisFunctionDoesNotExist is not defined'); | ||
} | ||
}); | ||
|
||
it('should throw an error if the callback function returns `false`', () => { | ||
try { | ||
Assert.callback(value => value === 'foobiz').validate('foobar'); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.show().assert).toEqual('Callback'); | ||
expect(e.show().value).toEqual('foobar'); | ||
expect(e.show().violation.result).toBeFalsy(); | ||
} | ||
}); | ||
|
||
it('should expose `assert` equal to `Callback`', () => { | ||
try { | ||
Assert.callback(value => value === 'foobiz').validate('foobar'); | ||
} catch (e) { | ||
expect(e.show().assert).toEqual('Callback'); | ||
} | ||
}); | ||
|
||
it('should have a `class` option and expose it as `assert`', () => { | ||
try { | ||
Assert.callback(value => value === 'foobiz', 'CustomClass').validate('foobar'); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Violation); | ||
expect(e.show().assert).toEqual('CustomClass'); | ||
expect(e.show().value).toEqual('foobar'); | ||
expect(e.show().violation.result).toBeFalsy(); | ||
} | ||
}); | ||
|
||
it('should not throw an error if the callback function returns `true`', () => { | ||
Assert.callback(value => value === 'foobar').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