From 1c1844293c69db5307b04764f20af3bbeaf8f265 Mon Sep 17 00:00:00 2001 From: James DiGioia Date: Fri, 15 Mar 2019 11:20:31 -0400 Subject: [PATCH] feat: replace integer check with Number.isInteger (#405) BREAKING CHANGE: use Number.isInteger. This works correctly for large numbers. Related to https://github.com/jquense/yup/pull/147 --- src/number.js | 12 +++++++----- test/number.js | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/number.js b/src/number.js index be65452b1..b179e6aa8 100644 --- a/src/number.js +++ b/src/number.js @@ -5,8 +5,6 @@ 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(); @@ -14,8 +12,8 @@ export default function NumberSchema() { 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; @@ -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() { diff --git a/test/number.js b/test/number.js index 4d9144b6e..651873dda 100644 --- a/test/number.js +++ b/test/number.js @@ -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', () => {