diff --git a/awx/ui_next/src/screens/Inventory/shared/InventorySourceSubForms/SharedFields.jsx b/awx/ui_next/src/screens/Inventory/shared/InventorySourceSubForms/SharedFields.jsx index 8150a9a45f..35e2c3fc30 100644 --- a/awx/ui_next/src/screens/Inventory/shared/InventorySourceSubForms/SharedFields.jsx +++ b/awx/ui_next/src/screens/Inventory/shared/InventorySourceSubForms/SharedFields.jsx @@ -3,7 +3,7 @@ import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { useField } from 'formik'; import { FormGroup } from '@patternfly/react-core'; -import { minMaxValue } from '../../../../util/validators'; +import { minMaxValue, regExp } from '../../../../util/validators'; import AnsibleSelect from '../../../../components/AnsibleSelect'; import { VariablesField } from '../../../../components/CodeMirrorInput'; import FormField, { @@ -192,6 +192,7 @@ export const HostFilterField = withI18n()(({ i18n }) => { )} name="host_filter" type="text" + validate={regExp(i18n)} /> ); }); diff --git a/awx/ui_next/src/util/validators.jsx b/awx/ui_next/src/util/validators.jsx index ee97cd4702..035b4dfb56 100644 --- a/awx/ui_next/src/util/validators.jsx +++ b/awx/ui_next/src/util/validators.jsx @@ -88,3 +88,14 @@ export function combine(validators) { return undefined; }; } + +export function regExp(i18n) { + return value => { + try { + RegExp(value); + } catch { + return i18n._(t`This field must be a regular expression`); + } + return undefined; + }; +} diff --git a/awx/ui_next/src/util/validators.test.js b/awx/ui_next/src/util/validators.test.js index f3e37c6cde..11b1a3bfd9 100644 --- a/awx/ui_next/src/util/validators.test.js +++ b/awx/ui_next/src/util/validators.test.js @@ -5,6 +5,7 @@ import { noWhiteSpace, integer, combine, + regExp, } from './validators'; const i18n = { _: val => val }; @@ -128,4 +129,13 @@ describe('validators', () => { }); expect(combine(validators)('ok')).toBeUndefined(); }); + + test('regExp rejects invalid regular expression', () => { + expect(regExp(i18n)('[')).toEqual({ + id: 'This field must be a regular expression', + }); + expect(regExp(i18n)('')).toBeUndefined(); + expect(regExp(i18n)('ok')).toBeUndefined(); + expect(regExp(i18n)('[^a-zA-Z]')).toBeUndefined(); + }); });