diff --git a/frontend/src/views/setting/alert/dash/task/index.vue b/frontend/src/views/setting/alert/dash/task/index.vue index 7ad5193e4..e77c02636 100644 --- a/frontend/src/views/setting/alert/dash/task/index.vue +++ b/frontend/src/views/setting/alert/dash/task/index.vue @@ -454,79 +454,61 @@ const allTaskOptions = [ { value: 'panelUpdate', label: 'xpack.alert.panelUpdate', show: isMaster.value }, ]; -function checkCycle(rule: any, value: any, callback: any) { - if (value === '') { - callback(); +function checkRange(value: any, min: number, max: number, callback: any) { + const num = Number(value); + if (isNaN(num)) { + return callback(new Error(i18n.global.t('commons.rule.number'))); } - if (dialogData.value.rowData.type === 'ssl') { - const regex = /^(?:[1-9]|[1-5][0-9]|60)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 60]))); - } - } else if (ipTypes.includes(dialogData.value.rowData.type)) { - const regex = /^(?:[1-9]|[1-5][0-9]|200)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 200]))); - } - } else { - const regex = /^(?:[1-9]|[12][0-9]|30)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 30]))); - } + if (num < min || num > max) { + return callback(new Error(i18n.global.t('commons.rule.numberRange', [min, max]))); } callback(); } +function checkCycle(rule: any, value: any, callback: any) { + if (value === '') return callback(); + + const type = dialogData.value.rowData.type; + + if (type === 'ssl') { + return checkRange(value, 1, 60, callback); + } else if (ipTypes.includes(type)) { + return checkRange(value, 1, 200, callback); + } else { + return checkRange(value, 1, 30, callback); + } +} + function checkCount(rule: any, value: any, callback: any) { - if (value === '') { - callback(); + if (value === '') return callback(); + + const type = dialogData.value.rowData.type; + const cycle = dialogData.value.rowData.cycle; + + if (avgTypes.includes(type) || ipTypes.includes(type)) { + return checkRange(value, 1, 100, callback); } - if (avgTypes.includes(dialogData.value.rowData.type) || ipTypes.includes(dialogData.value.rowData.type)) { - const regex = /^(?:[1-9]|[1-9][0-9]|100)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 100]))); - } - } - if (dialogData.value.rowData.type === 'disk' && dialogData.value.rowData.cycle === 2) { - const regex = /^(?:[1-9]|[1-9][0-9]|100)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 100]))); - } + if (type === 'disk' && cycle === 2) { + return checkRange(value, 1, 100, callback); } callback(); } function checkSendCount(rule: any, value: any, callback: any) { - if (value === '') { - callback(); - } - if ( - dialogData.value.rowData.type === 'disk' || - avgTypes.includes(dialogData.value.rowData.type) || - ipTypes.includes(dialogData.value.rowData.type) - ) { - const regex = /^(?:[1-9]|[1-4][0-9]|50)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 50]))); - } - } else if (noParamTypes.includes(dialogData.value.rowData.type)) { - const regex = /^(?:[1-9]|[12][0-9]|30)$/; - if (!regex.test(value)) { - return callback(new Error(i18n.global.t('commons.rule.numberRange', [1, 30]))); - } - } else { - if (dialogData.value.rowData.cycle > 0) { - const minValue = 1; - const maxValue = dialogData.value.rowData.cycle; - const regex = new RegExp(`^(${minValue}|[2-9]|[1-9][0-9]*|${maxValue})$`); - if (!regex.test(value) || value > maxValue) { - return callback( - new Error(i18n.global.t('commons.rule.numberRange', [1, dialogData.value.rowData.cycle])), - ); - } - } - } + if (value === '') return callback(); + const type = dialogData.value.rowData.type; + const cycle = dialogData.value.rowData.cycle; + + if (type === 'disk' || avgTypes.includes(type) || ipTypes.includes(type)) { + return checkRange(value, 1, 50, callback); + } else if (noParamTypes.includes(type)) { + return checkRange(value, 1, 30, callback); + } else { + if (cycle > 0) { + return checkRange(value, 1, cycle, callback); + } + } callback(); }