Alerting: validate recording rule name (#39136)

This commit is contained in:
Domas 2021-09-15 18:24:26 +03:00 committed by GitHub
parent aba8af1d59
commit e251863085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -281,6 +281,19 @@ describe('RuleEditor', () => {
await userEvent.type(ui.inputs.labelKey(1).get(), 'team');
await userEvent.type(ui.inputs.labelValue(1).get(), 'the a-team');
// try to save, find out that recording rule name is invalid
userEvent.click(ui.buttons.save.get());
expect(
await byText(
'Recording rule name must be valid metric name. It may only contain letters, numbers, and colons. It may not contain whitespace.'
).find()
).toBeInTheDocument();
expect(mocks.api.setRulerRuleGroup).not.toBeCalled();
// fix name and re-submit
await userEvent.type(await ui.inputs.name.find(), '{selectall}{del}my:great:new:recording:rule');
userEvent.click(ui.buttons.save.get());
// save and check what was sent to backend
userEvent.click(ui.buttons.save.get());
await waitFor(() => expect(mocks.api.setRulerRuleGroup).toHaveBeenCalled());
@ -288,7 +301,7 @@ describe('RuleEditor', () => {
name: 'group2',
rules: [
{
record: 'my great new recording rule',
record: 'my:great:new:recording:rule',
labels: { team: 'the a-team' },
expr: 'up == 1',
},

View File

@ -14,6 +14,12 @@ interface Props {
editingExistingRule: boolean;
}
const recordingRuleNameValidationPattern = {
message:
'Recording rule name must be valid metric name. It may only contain letters, numbers, and colons. It may not contain whitespace.',
value: /^[a-zA-Z_:][a-zA-Z0-9_:]*$/,
};
export const AlertTypeStep: FC<Props> = ({ editingExistingRule }) => {
const styles = useStyles2(getStyles);
@ -63,7 +69,10 @@ export const AlertTypeStep: FC<Props> = ({ editingExistingRule }) => {
>
<Input
id="name"
{...register('name', { required: { value: true, message: 'Must enter an alert name' } })}
{...register('name', {
required: { value: true, message: 'Must enter an alert name' },
pattern: ruleFormType === RuleFormType.cloudRecording ? recordingRuleNameValidationPattern : undefined,
})}
autoFocus={true}
/>
</Field>