diff --git a/x-pack/plugins/alerting/server/config.test.ts b/x-pack/plugins/alerting/server/config.test.ts index f3298fe16a694..08ef06921a143 100644 --- a/x-pack/plugins/alerting/server/config.test.ts +++ b/x-pack/plugins/alerting/server/config.test.ts @@ -35,4 +35,75 @@ describe('config validation', () => { } `); }); + + describe('rules.minimumScheduleInterval.value', () => { + test('allows 1d as a value', () => { + const config: Record = { + rules: { + minimumScheduleInterval: { + value: '1d', + }, + }, + }; + const validatedConfig = configSchema.validate(config); + expect(validatedConfig.rules.minimumScheduleInterval.value).toMatchInlineSnapshot(`"1d"`); + }); + + test(`doesn't allow 2d as a value`, () => { + const config: Record = { + rules: { + minimumScheduleInterval: { + value: '2d', + }, + }, + }; + expect(() => configSchema.validate(config)).toThrowErrorMatchingInlineSnapshot( + `"[rules.minimumScheduleInterval.value]: duration cannot exceed one day"` + ); + }); + + test(`doesn't allow 25h as a value`, () => { + const config: Record = { + rules: { + minimumScheduleInterval: { + value: '25h', + }, + }, + }; + expect(() => configSchema.validate(config)).toThrowErrorMatchingInlineSnapshot( + `"[rules.minimumScheduleInterval.value]: duration cannot exceed one day"` + ); + }); + }); + + describe('rules.run.actions.max', () => { + test('allows 100000 as a value', () => { + const config: Record = { + rules: { + run: { + actions: { + max: 100000, + }, + }, + }, + }; + const validatedConfig = configSchema.validate(config); + expect(validatedConfig.rules.run.actions.max).toMatchInlineSnapshot(`100000`); + }); + + test(`doesn't allow 100001 as a value`, () => { + const config: Record = { + rules: { + run: { + actions: { + max: 100001, + }, + }, + }, + }; + expect(() => configSchema.validate(config)).toThrowErrorMatchingInlineSnapshot( + `"[rules.run.actions.max]: Value must be equal to or lower than [100000]."` + ); + }); + }); }); diff --git a/x-pack/plugins/alerting/server/config.ts b/x-pack/plugins/alerting/server/config.ts index 8819a6c1e6211..64c09a9b4bb09 100644 --- a/x-pack/plugins/alerting/server/config.ts +++ b/x-pack/plugins/alerting/server/config.ts @@ -6,8 +6,9 @@ */ import { schema, TypeOf } from '@kbn/config-schema'; -import { validateDurationSchema } from './lib'; +import { validateDurationSchema, parseDuration } from './lib'; +const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000; const ruleTypeSchema = schema.object({ id: schema.string(), timeout: schema.maybe(schema.string({ validate: validateDurationSchema })), @@ -16,7 +17,17 @@ const ruleTypeSchema = schema.object({ const rulesSchema = schema.object({ minimumScheduleInterval: schema.object({ value: schema.string({ - validate: validateDurationSchema, + validate: (duration: string) => { + const validationResult = validateDurationSchema(duration); + if (validationResult) { + return validationResult; + } + + const parsedDurationMs = parseDuration(duration); + if (parsedDurationMs > ONE_DAY_IN_MS) { + return 'duration cannot exceed one day'; + } + }, defaultValue: '1m', }), enforce: schema.boolean({ defaultValue: false }), // if enforce is false, only warnings will be shown @@ -24,7 +35,7 @@ const rulesSchema = schema.object({ run: schema.object({ timeout: schema.maybe(schema.string({ validate: validateDurationSchema })), actions: schema.object({ - max: schema.number({ defaultValue: 100000 }), + max: schema.number({ defaultValue: 100000, max: 100000 }), }), ruleTypeOverrides: schema.maybe(schema.arrayOf(ruleTypeSchema)), }),