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',