fix: validate advanced snmp oid json rows
This commit is contained in:
@@ -355,12 +355,21 @@ const hasSnmpOidTableUnsupportedFields = (raw: string) => {
|
|||||||
const rows = parseSnmpOidJsonArray(raw)
|
const rows = parseSnmpOidJsonArray(raw)
|
||||||
return rows.some((item) => {
|
return rows.some((item) => {
|
||||||
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
||||||
return Boolean(item)
|
return true
|
||||||
}
|
}
|
||||||
return Object.keys(item).some((key) => !snmpOidTableFields.includes(key))
|
return Object.keys(item).some((key) => !snmpOidTableFields.includes(key))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const findInvalidSnmpOidJsonRowIndex = (rows: unknown[]) =>
|
||||||
|
rows.findIndex((item) => {
|
||||||
|
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
const row = item as Record<string, unknown>
|
||||||
|
return !String(row.oid ?? '').trim() || !String(row.metric_name ?? '').trim()
|
||||||
|
})
|
||||||
|
|
||||||
const stringifySnmpOidRows = (rows: SnmpOidRow[]) => {
|
const stringifySnmpOidRows = (rows: SnmpOidRow[]) => {
|
||||||
const normalizedRows = normalizeSnmpOidRows(rows)
|
const normalizedRows = normalizeSnmpOidRows(rows)
|
||||||
return normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : ''
|
return normalizedRows.length ? JSON.stringify(normalizedRows, null, 2) : ''
|
||||||
@@ -552,12 +561,12 @@ const validateSnmpOidConfigBeforeSubmit = () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const parsedRows = parseSnmpOidJsonArray(formData.snmp_oids)
|
const parsedRows = parseSnmpOidJsonArray(formData.snmp_oids)
|
||||||
const rows = parseSnmpOidRows(formData.snmp_oids)
|
const invalidJsonIndex = findInvalidSnmpOidJsonRowIndex(parsedRows)
|
||||||
const invalidIndex = findIncompleteSnmpOidRowIndex(rows)
|
if (invalidJsonIndex >= 0) {
|
||||||
if (invalidIndex >= 0) {
|
Message.warning(`SNMP OID 第 ${invalidJsonIndex + 1} 行请填写 OID 和指标名称`)
|
||||||
Message.warning(`SNMP OID 第 ${invalidIndex + 1} 行请填写 OID 和指标名称`)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
const rows = parseSnmpOidRows(formData.snmp_oids)
|
||||||
const normalizedRows = normalizeSnmpOidRows(rows)
|
const normalizedRows = normalizeSnmpOidRows(rows)
|
||||||
snmpOidRows.value = normalizedRows
|
snmpOidRows.value = normalizedRows
|
||||||
formData.snmp_oids = JSON.stringify(parsedRows, null, 2)
|
formData.snmp_oids = JSON.stringify(parsedRows, null, 2)
|
||||||
|
|||||||
Reference in New Issue
Block a user