-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from offirgolan/number-validator
number validator
- Loading branch information
Showing
5 changed files
with
328 additions
and
6 deletions.
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,75 @@ | ||
/** | ||
* Copyright 2015, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
|
||
import Ember from 'ember'; | ||
import Base from 'ember-cp-validations/validators/base'; | ||
|
||
const { | ||
isEmpty | ||
} = Ember; | ||
|
||
export default Base.extend({ | ||
validate(value, options) { | ||
let numValue = Number(value); | ||
let optionKeys = Object.keys(options); | ||
|
||
if (typeof value === 'string' && (isEmpty(value) || !options.allowString)) { | ||
return this.createErrorMessage('notANumber', value, options); | ||
} | ||
|
||
if (!this.isNumber(numValue)) { | ||
return this.createErrorMessage('notANumber', value, options); | ||
} | ||
|
||
if (options.integer && !this.isInteger(numValue)) { | ||
return this.createErrorMessage('notAnInteger', value, options); | ||
} | ||
|
||
for (let i = 0; i < optionKeys.length; i++) { | ||
let type = optionKeys[i]; | ||
let m = this._validateType(type, options, numValue); | ||
if (typeof m === 'string') { | ||
return m; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
|
||
_validateType(type, options, value) { | ||
let expected = options[type]; | ||
let actual = value; | ||
|
||
if (type === 'is' && actual !== expected) { | ||
return this.createErrorMessage('equalTo', value, options); | ||
} else if (type === 'lt' && actual >= expected) { | ||
return this.createErrorMessage('lessThan', value, options); | ||
} else if (type === 'lte' && actual > expected) { | ||
return this.createErrorMessage('lessThanOrEqualTo', value, options); | ||
} else if (type === 'gt' && actual <= expected) { | ||
return this.createErrorMessage('greaterThan', value, options); | ||
} else if (type === 'gte' && actual < expected) { | ||
return this.createErrorMessage('greaterThanOrEqualTo', value, options); | ||
} else if (type === 'positive' && actual < 0) { | ||
return this.createErrorMessage('positive', value, options); | ||
} else if (type === 'odd' && actual % 2 === 0) { | ||
return this.createErrorMessage('odd', value, options); | ||
} else if (type === 'even' && actual % 2 !== 0) { | ||
return this.createErrorMessage('even', value, options); | ||
} | ||
|
||
return true; | ||
}, | ||
|
||
/* Use polyfills instead of Number.isNaN or Number.isInteger to support IE & Safari */ | ||
|
||
isNumber(value) { | ||
return typeof value === "number" && !isNaN(value); | ||
}, | ||
|
||
isInteger(value) { | ||
return typeof value === "number" && isFinite(value) && Math.floor(value) === value; | ||
} | ||
}); |
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,24 @@ | ||
Validates that your attributes have only numeric values. | ||
|
||
## Options | ||
* `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 | ||
* `odd` (**Boolean**): Number must be odd | ||
* `even` (**Boolean**): Number must be even | ||
* `is` (**Number**): Number must be equal to this value | ||
* `lt` (**Number**): Number must be less than this value | ||
* `lte` (**Number**): Number must be less than or equal to this value | ||
* `gt` (**Number**): Number must be greater than this value | ||
* `gte` (**Number**): Number must be greater than or equal to this value | ||
|
||
```javascript | ||
// Examples | ||
validator('number') // Simple check if the value is a number | ||
validator('number', { | ||
allowString: true, | ||
integer: true, | ||
gt: 5, | ||
lte: 100 | ||
}) | ||
``` |
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,222 @@ | ||
/** | ||
* Copyright 2015, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
|
||
import Ember from 'ember'; | ||
import { | ||
moduleFor, test | ||
} | ||
from 'ember-qunit'; | ||
|
||
var options, validator, message; | ||
var set = Ember.set; | ||
|
||
moduleFor('validator:number', 'Unit | Validator | number', { | ||
needs: ['validator:messages'], | ||
setup: function() { | ||
validator = this.subject(); | ||
} | ||
}); | ||
|
||
test('no options', function(assert) { | ||
assert.expect(2); | ||
|
||
message = validator.validate(undefined, {}); | ||
assert.equal(message, 'This field must be a number'); | ||
|
||
message = validator.validate(22, {}); | ||
assert.equal(message, true); | ||
}); | ||
|
||
test('allow string', function(assert) { | ||
assert.expect(6); | ||
|
||
options = { | ||
allowString: true | ||
}; | ||
|
||
message = validator.validate('22', options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate('22.22', options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate('test', options); | ||
assert.equal(message, 'This field must be a number'); | ||
|
||
message = validator.validate('', options); | ||
assert.equal(message, 'This field must be a number'); | ||
|
||
options.allowString = false; | ||
|
||
message = validator.validate('22', options); | ||
assert.equal(message, 'This field must be a number'); | ||
|
||
message = validator.validate('22.22', options); | ||
assert.equal(message, 'This field must be a number'); | ||
|
||
|
||
}); | ||
|
||
test('integer', function(assert) { | ||
assert.expect(3); | ||
|
||
options = { | ||
integer: true | ||
}; | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(22.22, options); | ||
assert.equal(message, 'This field must be an integer'); | ||
|
||
message = validator.validate(-2.2, options); | ||
assert.equal(message, 'This field must be an integer'); | ||
}); | ||
|
||
test('is', function(assert) { | ||
assert.expect(2); | ||
|
||
options = { | ||
is: 22 | ||
}; | ||
|
||
message = validator.validate(1, options); | ||
assert.equal(message, 'This field must be equal to 22'); | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, true); | ||
}); | ||
|
||
test('lt', function(assert) { | ||
assert.expect(3); | ||
|
||
options = { | ||
lt: 22 | ||
}; | ||
|
||
message = validator.validate(21, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, 'This field must be less than 22'); | ||
|
||
message = validator.validate(23, options); | ||
assert.equal(message, 'This field must be less than 22'); | ||
}); | ||
|
||
test('lte', function(assert) { | ||
assert.expect(3); | ||
|
||
options = { | ||
lte: 22 | ||
}; | ||
|
||
message = validator.validate(21, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(23, options); | ||
assert.equal(message, 'This field must be less than or equal to 22'); | ||
}); | ||
|
||
test('gt', function(assert) { | ||
assert.expect(3); | ||
|
||
options = { | ||
gt: 22 | ||
}; | ||
|
||
message = validator.validate(21, options); | ||
assert.equal(message, 'This field must be greater than 22'); | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, 'This field must be greater than 22'); | ||
|
||
message = validator.validate(23, options); | ||
assert.equal(message, true); | ||
}); | ||
|
||
test('gte', function(assert) { | ||
assert.expect(3); | ||
|
||
options = { | ||
gte: 22 | ||
}; | ||
|
||
message = validator.validate(21, options); | ||
assert.equal(message, 'This field must be greater than or equal to 22'); | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(23, options); | ||
assert.equal(message, true); | ||
}); | ||
|
||
test('positive', function(assert) { | ||
assert.expect(4); | ||
|
||
options = { | ||
positive: true | ||
}; | ||
|
||
message = validator.validate(-1, options); | ||
assert.equal(message, 'This field must be positive'); | ||
|
||
message = validator.validate(-144, options); | ||
assert.equal(message, 'This field must be positive'); | ||
|
||
message = validator.validate(0, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, true); | ||
}); | ||
|
||
test('odd', function(assert) { | ||
assert.expect(4); | ||
|
||
options = { | ||
odd: true | ||
}; | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, 'This field must be odd'); | ||
|
||
message = validator.validate(-144, options); | ||
assert.equal(message, 'This field must be odd'); | ||
|
||
message = validator.validate(21, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(-21, options); | ||
assert.equal(message, true); | ||
}); | ||
|
||
test('even', function(assert) { | ||
assert.expect(5); | ||
|
||
options = { | ||
even: true | ||
}; | ||
|
||
message = validator.validate(22, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(-22, options); | ||
assert.equal(message, true); | ||
|
||
message = validator.validate(22.22, options); | ||
assert.equal(message, 'This field must be even'); | ||
|
||
message = validator.validate(21, options); | ||
assert.equal(message, 'This field must be even'); | ||
|
||
message = validator.validate(-33, options); | ||
assert.equal(message, 'This field must be even'); | ||
}); |