Skip to content

Commit 58a258e

Browse files
committed
Added an object() verifier to typester.
1 parent 9bd530c commit 58a258e

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ Here's the list of core _verifiers_ you can use in Typester:
4949

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

52+
* `object()`
5253
* `number()`
5354
* `positiveNumber()`
5455
* `negativeNumber()`
5556
* `integerNumber()`
5657
* `nonEmptyString()`
5758
* `nonEmptyArray()`
5859

59-
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`.
60+
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()`.
6061

6162

6263
## Custom verifiers

lib/verifiers/NonEmptyVerifier.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ NonEmptyVerifier.prototype.nonEmptyArray = function() {
1313
if(this.argValue.length == 0) throw new ValidationError(this.argName + ' argument must be a non-empty array.');
1414
};
1515

16+
NonEmptyVerifier.prototype.object = function() {
17+
if(typeof(this.argValue) != 'object') throw new ValidationError(this.argName + ' argument must be an object.');
18+
};
19+
1620
module.exports = NonEmptyVerifier;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typester",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Fluent type verification for JavaScript",
55
"main": "lib/typester.js",
66
"dependencies": {

spec/test/non-empty-verifier.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('non-empty-verifier', function() {
1111
}
1212

1313
func.bind(func, true).should.throw(TypeError);
14-
func.bind(func, true).should.throw('str argument must be a String');
14+
func.bind(func, true).should.throw('str argument must be a String.');
1515
});
1616

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

2323
func.bind(func, '').should.throw(ValidationError);
24-
func.bind(func, '').should.throw('str argument must be a non-empty string');
24+
func.bind(func, '').should.throw('str argument must be a non-empty string.');
2525
func.bind(func, 'x').should.not.throw();
2626
});
2727
});
@@ -34,7 +34,7 @@ describe('non-empty-verifier', function() {
3434
}
3535

3636
func.bind(func, true).should.throw(TypeError);
37-
func.bind(func, true).should.throw('list argument must be a Array');
37+
func.bind(func, true).should.throw('list argument must be a Array.');
3838
});
3939

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

4646
func.bind(func, new Array()).should.throw(ValidationError);
4747
func.bind(func, []).should.throw(ValidationError);
48-
func.bind(func, []).should.throw('list argument must be a non-empty array');
48+
func.bind(func, []).should.throw('list argument must be a non-empty array.');
4949
func.bind(func, [1]).should.not.throw();
5050
});
5151
});
52+
53+
describe('object', function() {
54+
it('throws a type error if a function, string, or number is provided', function() {
55+
function func(obj) {
56+
using(arguments)
57+
.verify('obj').object();
58+
}
59+
60+
func.bind(func, {}).should.not.throw(ValidationError);
61+
func.bind(func, function() {}).should.throw(ValidationError);
62+
func.bind(func, 10).should.throw(ValidationError);
63+
func.bind(func, 'x').should.throw(ValidationError);
64+
func.bind(func, 'x').should.throw('obj argument must be an object.');
65+
});
66+
});
5267
});

0 commit comments

Comments
 (0)