From 9b5e59f045e6c2eb20677c1f9bb8b27d93e5a47e Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 20 Oct 2020 15:44:17 -0700 Subject: [PATCH] add number validator support for negative numbers, large numbers --- awx/ui_next/src/util/validators.jsx | 6 +++++- awx/ui_next/src/util/validators.test.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/awx/ui_next/src/util/validators.jsx b/awx/ui_next/src/util/validators.jsx index 0dfbe77fcf..499957f6e5 100644 --- a/awx/ui_next/src/util/validators.jsx +++ b/awx/ui_next/src/util/validators.jsx @@ -79,7 +79,11 @@ export function integer(i18n) { export function number(i18n) { return value => { const str = String(value); - if (/^[0-9]*(\.[0-9]*)?$/.test(str)) { + if (/^-?[0-9]*(\.[0-9]*)?$/.test(str)) { + return undefined; + } + // large number scientific notation (e.g. '1e+21') + if (/^-?[0-9]*e[+-][0-9]*$/.test(str)) { return undefined; } return i18n._(t`This field must be a number`); diff --git a/awx/ui_next/src/util/validators.test.js b/awx/ui_next/src/util/validators.test.js index 6a0a221d57..a660c8ea5c 100644 --- a/awx/ui_next/src/util/validators.test.js +++ b/awx/ui_next/src/util/validators.test.js @@ -121,10 +121,19 @@ describe('validators', () => { expect(number(i18n)('13')).toBeUndefined(); }); + test('number should accept negative number', () => { + expect(number(i18n)(-14)).toBeUndefined(); + }); + test('number should accept decimal/float', () => { expect(number(i18n)(13.1)).toBeUndefined(); }); + test('number should accept large number', () => { + expect(number(i18n)(999999999999999999999.9)).toBeUndefined(); + expect(number(i18n)(-999999999999999999999.9)).toBeUndefined(); + }); + test('number should reject string containing alphanum', () => { expect(number(i18n)('15a')).toEqual({ id: 'This field must be a number',