Skip to content

Commit

Permalink
Merge pull request #64 from jakesjews/master
Browse files Browse the repository at this point in the history
add allowBlank option to number validator
  • Loading branch information
offirgolan committed Nov 3, 2015
2 parents f66d7d4 + a7fe3df commit 2f52935
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/validators/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default Base.extend({
let numValue = Number(value);
let optionKeys = Object.keys(options);

if (options.allowBlank && isEmpty(value)) {
return true;
}

if (typeof value === 'string' && (isEmpty(value) || !options.allowString)) {
return this.createErrorMessage('notANumber', value, options);
}
Expand Down
1 change: 1 addition & 0 deletions docs/docs/validators/number.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Validates that your attributes have only numeric values.

## Options
* `allowBlank` (**Boolean**): If true, skips validation if the value is empty
* `allowString` (**Boolean**): If true, validator will accept string representation of a number
* `integer` (**Boolean**): Number must be an integer
* `positive` (**Boolean**): Number must be greater than 0
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/validators/number-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,20 @@ test('even', function(assert) {
message = validator.validate(-33, options);
assert.equal(message, 'This field must be even');
});

test('allowBlank', function(assert) {
assert.expect(3);

options = {
allowBlank: true
};

message = validator.validate(null, options);
assert.equal(message, true);

message = validator.validate(undefined, options);
assert.equal(message, true);

message = validator.validate('', options);
assert.equal(message, true);
});

0 comments on commit 2f52935

Please sign in to comment.