mirror of
https://github.com/grafana/grafana.git
synced 2024-11-22 08:56:43 -06:00
Alerting: validate recording rule name (#39136)
This commit is contained in:
parent
aba8af1d59
commit
e251863085
@ -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',
|
||||
},
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user