From acd8a8dd3c630c1caa2f61ab69b2fc62d9b81dfc Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 20 Oct 2020 10:29:23 -0700 Subject: [PATCH 1/4] fix integer/float errors in survey --- .../Template/Survey/SurveyQuestionForm.jsx | 12 +++++++++++- awx/ui_next/src/util/validators.jsx | 11 +++++++++++ awx/ui_next/src/util/validators.test.js | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx index ff57f3bf17..e06c865cdf 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx @@ -18,6 +18,8 @@ import { noWhiteSpace, combine, maxLength, + integer, + number as numberValidator, } from '../../../util/validators'; function AnswerTypeField({ i18n }) { @@ -177,7 +179,15 @@ function SurveyQuestionForm({ diff --git a/awx/ui_next/src/util/validators.jsx b/awx/ui_next/src/util/validators.jsx index 111736c3ed..c4475b24a6 100644 --- a/awx/ui_next/src/util/validators.jsx +++ b/awx/ui_next/src/util/validators.jsx @@ -68,6 +68,7 @@ export function noWhiteSpace(i18n) { export function integer(i18n) { return value => { + console.log(value); const str = String(value); if (/[^0-9]/.test(str)) { return i18n._(t`This field must be an integer`); @@ -76,6 +77,16 @@ export function integer(i18n) { }; } +export function number(i18n) { + return value => { + const str = String(value); + if (/^[0-9]*(\.[0-9]*)?$/.test(str)) { + return undefined; + } + return i18n._(t`This field must be a number`); + }; +} + export function url(i18n) { return value => { if (!value) { diff --git a/awx/ui_next/src/util/validators.test.js b/awx/ui_next/src/util/validators.test.js index fcde053dbb..6a0a221d57 100644 --- a/awx/ui_next/src/util/validators.test.js +++ b/awx/ui_next/src/util/validators.test.js @@ -4,6 +4,7 @@ import { maxLength, noWhiteSpace, integer, + number, url, combine, regExp, @@ -112,6 +113,24 @@ describe('validators', () => { }); }); + test('number should accept number (number)', () => { + expect(number(i18n)(13)).toBeUndefined(); + }); + + test('number should accept number (string)', () => { + expect(number(i18n)('13')).toBeUndefined(); + }); + + test('number should accept decimal/float', () => { + expect(number(i18n)(13.1)).toBeUndefined(); + }); + + test('number should reject string containing alphanum', () => { + expect(number(i18n)('15a')).toEqual({ + id: 'This field must be a number', + }); + }); + test('url should reject incomplete url', () => { expect(url(i18n)('abcd')).toEqual({ id: 'Please enter a valid URL', From a9c01e891fed78ea5ceaf4440a12478691b10bf6 Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 20 Oct 2020 11:18:40 -0700 Subject: [PATCH 2/4] remove console log Co-authored-by: Jake McDermott --- awx/ui_next/src/util/validators.jsx | 1 - 1 file changed, 1 deletion(-) diff --git a/awx/ui_next/src/util/validators.jsx b/awx/ui_next/src/util/validators.jsx index c4475b24a6..0dfbe77fcf 100644 --- a/awx/ui_next/src/util/validators.jsx +++ b/awx/ui_next/src/util/validators.jsx @@ -68,7 +68,6 @@ export function noWhiteSpace(i18n) { export function integer(i18n) { return value => { - console.log(value); const str = String(value); if (/[^0-9]/.test(str)) { return i18n._(t`This field must be an integer`); From 9b5e59f045e6c2eb20677c1f9bb8b27d93e5a47e Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 20 Oct 2020 15:44:17 -0700 Subject: [PATCH 3/4] 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', From a8bb3519c5fafa616e824579b193c248b9386c1f Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Fri, 23 Oct 2020 14:37:32 -0700 Subject: [PATCH 4/4] fix multiselect survey question on launch --- awx/ui_next/src/components/LaunchPrompt/steps/SurveyStep.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/SurveyStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/SurveyStep.jsx index d5f4a42df4..11ee7a74f7 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/SurveyStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/SurveyStep.jsx @@ -122,7 +122,7 @@ function MultiSelectField({ question, i18n }) { validate: question.isrequired ? required(null, i18n) : null, }); const id = `survey-question-${question.variable}`; - const hasActualValue = !question.required || meta.value.length > 0; + const hasActualValue = !question.required || meta.value?.length > 0; const isValid = !meta.touched || (!meta.error && hasActualValue); return (