Check that host_filter is regexp

This commit is contained in:
Jake McDermott 2020-08-27 12:13:40 -04:00 committed by Ryan Petrello
parent f04aff81c4
commit dcf5917a4e
No known key found for this signature in database
GPG Key ID: F2AA5F2122351777
3 changed files with 23 additions and 1 deletions

View File

@ -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)}
/>
);
});

View File

@ -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;
};
}

View File

@ -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();
});
});