Skip to content

Commit

Permalink
Added an object() verifier to typester.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchambers committed Oct 23, 2014
1 parent 9bd530c commit 58a258e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ Here's the list of core _verifiers_ you can use in Typester:

and here's the list of _validating-verifiers_ that come with Typester:

* `object()`
* `number()`
* `positiveNumber()`
* `negativeNumber()`
* `integerNumber()`
* `nonEmptyString()`
* `nonEmptyArray()`

Validating verifiers are usually some additional check over and above a simple `isA()` invocation. The `number()` verifier is worth mentioning, as it enhances `isA(Number)` by ensuring failure for `NaN`, `NEGATIVE_INFINITY` and `POSITIVE_INFINITY`.
Validating verifiers are usually some additional check over and above a simple `isA()` invocation. The `object()` and `number()` verifiers are worth mentioning, as these enhance `isA(Object)` and `isA(Number)` respectively. By ensuring that `typeof(object) == true` in the case of `object()`, and by ensuring failure for `NaN`, `NEGATIVE_INFINITY` and `POSITIVE_INFINITY` in the case of `number()`.


## Custom verifiers
Expand Down
4 changes: 4 additions & 0 deletions lib/verifiers/NonEmptyVerifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ NonEmptyVerifier.prototype.nonEmptyArray = function() {
if(this.argValue.length == 0) throw new ValidationError(this.argName + ' argument must be a non-empty array.');
};

NonEmptyVerifier.prototype.object = function() {
if(typeof(this.argValue) != 'object') throw new ValidationError(this.argName + ' argument must be an object.');
};

module.exports = NonEmptyVerifier;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typester",
"version": "0.2.0",
"version": "0.3.0",
"description": "Fluent type verification for JavaScript",
"main": "lib/typester.js",
"dependencies": {
Expand Down
23 changes: 19 additions & 4 deletions spec/test/non-empty-verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('non-empty-verifier', function() {
}

func.bind(func, true).should.throw(TypeError);
func.bind(func, true).should.throw('str argument must be a String');
func.bind(func, true).should.throw('str argument must be a String.');
});

it('throws a ValidationError if an empty string is provided', function() {
Expand All @@ -21,7 +21,7 @@ describe('non-empty-verifier', function() {
}

func.bind(func, '').should.throw(ValidationError);
func.bind(func, '').should.throw('str argument must be a non-empty string');
func.bind(func, '').should.throw('str argument must be a non-empty string.');
func.bind(func, 'x').should.not.throw();
});
});
Expand All @@ -34,7 +34,7 @@ describe('non-empty-verifier', function() {
}

func.bind(func, true).should.throw(TypeError);
func.bind(func, true).should.throw('list argument must be a Array');
func.bind(func, true).should.throw('list argument must be a Array.');
});

it('throws a ValidationError if an empty array is provided', function() {
Expand All @@ -45,8 +45,23 @@ describe('non-empty-verifier', function() {

func.bind(func, new Array()).should.throw(ValidationError);
func.bind(func, []).should.throw(ValidationError);
func.bind(func, []).should.throw('list argument must be a non-empty array');
func.bind(func, []).should.throw('list argument must be a non-empty array.');
func.bind(func, [1]).should.not.throw();
});
});

describe('object', function() {
it('throws a type error if a function, string, or number is provided', function() {
function func(obj) {
using(arguments)
.verify('obj').object();
}

func.bind(func, {}).should.not.throw(ValidationError);
func.bind(func, function() {}).should.throw(ValidationError);
func.bind(func, 10).should.throw(ValidationError);
func.bind(func, 'x').should.throw(ValidationError);
func.bind(func, 'x').should.throw('obj argument must be an object.');
});
});
});

0 comments on commit 58a258e

Please sign in to comment.