Skip to content

Commit

Permalink
feat: replace integer check with Number.isInteger (#405)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: use Number.isInteger. This works correctly for large numbers.

Related to #147
  • Loading branch information
mAAdhaTTah authored and jquense committed Mar 15, 2019
1 parent 9944729 commit 1c18442
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import isAbsent from './util/isAbsent';

let isNaN = value => value != +value;

let isInteger = val => isAbsent(val) || val === (val | 0);

export default function NumberSchema() {
if (!(this instanceof NumberSchema)) return new NumberSchema();

MixedSchema.call(this, { type: 'number' });

this.withMutation(() => {
this.transform(function(value) {
let parsed = value
let parsed = value;

if (typeof parsed === 'string') {
parsed = parsed.replace(/\s/g, '');
if (parsed === '') return NaN;
Expand Down Expand Up @@ -94,7 +92,11 @@ inherits(NumberSchema, MixedSchema, {
},

integer(message = locale.integer) {
return this.test({ name: 'integer', message, test: isInteger });
return this.test({
name: 'integer',
message,
test: val => isAbsent(val) || Number.isInteger(val),
});
},

truncate() {
Expand Down
4 changes: 2 additions & 2 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ describe('Number types', function() {
var schema = number().integer();

TestHelpers.validateAll(schema, {
valid: [4, -5222],
invalid: [10.53, 0.1 * 0.2, -34512535.626, 3.12312e51, new Date()],
valid: [4, -5222, 3.12312e51],
invalid: [10.53, 0.1 * 0.2, -34512535.626, new Date()],
});

it('should return default message', () => {
Expand Down

0 comments on commit 1c18442

Please sign in to comment.