Skip to content

Commit

Permalink
Merge pull request hapijs#147 from wpreul/master
Browse files Browse the repository at this point in the history
Adding positive/negative options for number
  • Loading branch information
Eran Hammer committed Nov 15, 2013
2 parents 8b91630 + 3279c60 commit f930a81
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,33 @@ internals.Number.prototype.integer = function () {

return this;
};


internals.Number.prototype.negative = function () {

this._test('negative', function (value, state, options) {

if (value < 0) {
return null;
}

return Any.error('number.negative', { value: value }, state);
});

return this;
};


internals.Number.prototype.positive = function () {

this._test('positive', function (value, state, options) {

if (value > 0) {
return null;
}

return Any.error('number.positive', { value: value }, state);
});

return this;
};
127 changes: 127 additions & 0 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,133 @@ describe('Joi.number', function () {
done();
});

it('should handle combination of min and positive', function (done) {

var rule = Joi.number().min(-3).positive();
Validate(rule, [
[1, true],
[-2, false],
[8, true],
[null, false]
]);
done();
});

it('should handle combination of max and positive', function (done) {

var rule = Joi.number().max(5).positive();
Validate(rule, [
[4, true],
[-2, false],
[8, false],
[null, false]
]);
done();
});

it('should handle combination of min and negative', function (done) {

var rule = Joi.number().min(-3).negative();
Validate(rule, [
[4, false],
[-2, true],
[-4, false],
[null, false]
]);
done();
});

it('should handle combination of negative and positive', function (done) {

var rule = Joi.number().negative().positive();
Validate(rule, [
[4, false],
[-2, false],
[0, false],
[null, false]
]);
done();
});

it('should handle combination of negative and allow', function (done) {

var rule = Joi.number().negative().allow(1);
Validate(rule, [
[1, true],
[-10, true],
[8, false],
[0, false],
[null, false]
]);
done();
});

it('should handle combination of positive and allow', function (done) {

var rule = Joi.number().positive().allow(-1);
Validate(rule, [
[1, true],
[-1, true],
[8, true],
[-10, false],
[null, false]
]);
done();
});

it('should handle combination of positive, allow, and nullOk', function (done) {

var rule = Joi.number().positive().allow(-1).nullOk();
Validate(rule, [
[1, true],
[-1, true],
[8, true],
[-10, false],
[null, true]
]);
done();
});

it('should handle combination of negative, allow, and nullOk', function (done) {

var rule = Joi.number().negative().allow(1).nullOk();
Validate(rule, [
[1, true],
[-10, true],
[8, false],
[0, false],
[null, true]
]);
done();
});

it('should handle combination of positive, allow, nullOk, and deny', function (done) {

var rule = Joi.number().positive().allow(-1).nullOk().deny(1);
Validate(rule, [
[1, false],
[-1, true],
[8, true],
[-10, false],
[null, true]
]);
done();
});

it('should handle combination of negative, allow, nullOk, and deny', function (done) {

var rule = Joi.number().negative().allow(1).nullOk().deny(-5);
Validate(rule, [
[1, true],
[-10, true],
[-5, false],
[8, false],
[0, false],
[null, true]
]);
done();
});

it('should handle combination of min, max, and allow', function (done) {

var rule = Joi.number().min(8).max(10).allow(1);
Expand Down

0 comments on commit f930a81

Please sign in to comment.