-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23fd776
commit 8ea0bfe
Showing
13 changed files
with
389 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
frontend/packages/manager/src/components/ValidateInventoryAndGroup.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React from 'react'; | ||
|
||
const validateInventoryAndGroup = (validationObj) => { | ||
|
||
let errors = { | ||
groupName: [], | ||
address: [], | ||
port: [], | ||
community: [], | ||
secret: [], | ||
securityEngine: [], | ||
walkInterval: [], | ||
profiles: [] | ||
}; | ||
let isValid = true; | ||
console.log(validationObj); | ||
|
||
// Validate group name | ||
if (validationObj.hasOwnProperty("groupName")){ | ||
if (validationObj.groupName.length === 0){ | ||
errors.groupName.push("Group name is required"); | ||
isValid = false; | ||
}else if (!validationObj.groupName.match(/^[a-zA-Z0-9_-]+$/)){ | ||
isValid = false; | ||
errors.groupName.push("Group name can consist only of upper and lower english letters, " + | ||
"numbers and two special characters: '-' and '_'. No spaces are allowed."); | ||
} | ||
} | ||
|
||
// Validate address | ||
if (validationObj.hasOwnProperty("address")){ | ||
if (validationObj.address.length === 0){ | ||
let err = ((validationObj.hasOwnProperty("onlyAdress")) ? "Address is required" : "Address or Group is required") | ||
errors.address.push(err); | ||
isValid = false; | ||
}else if (Number.isInteger(Number(validationObj.address.charAt(0))) || validationObj.hasOwnProperty("onlyAdress")){ | ||
let doesMatch = validationObj.address.match(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/); | ||
let octetsValid = true; | ||
if (doesMatch){ | ||
let octets = validationObj.address.split("."); | ||
for (const octet of octets){ | ||
if (Number(octet) < 0 || Number(octet) > 255) { | ||
octetsValid = false; | ||
break; | ||
} | ||
} | ||
} | ||
if(!doesMatch || !octetsValid){ | ||
isValid = false; | ||
errors.address.push("Provided address isn't a valid IPv4 address") | ||
} | ||
}else{ | ||
if (!validationObj.address.match(/^[a-zA-Z0-9_-]+$/)){ | ||
isValid = false; | ||
errors.address.push("Group name can consist only of upper and lower english letters, " + | ||
"numbers and two special characters: '-' and '_'. No spaces are allowed."); | ||
} | ||
} | ||
} | ||
|
||
// Validate port | ||
if (validationObj.hasOwnProperty("port")){ | ||
if (!(Number.isInteger(validationObj.port) && validationObj.port > 0 && validationObj.port < 65535)){ | ||
isValid = false; | ||
errors.port.push("Port number must be an integer in range 1-65535"); | ||
} | ||
} | ||
|
||
// Validate community | ||
if (validationObj.hasOwnProperty("community")){ | ||
if (validationObj.community.length > 0 && !validationObj.community.match(/^[.a-zA-Z0-9_-]+$/)){ | ||
isValid = false; | ||
errors.community.push("Community can consist only of upper and lower english letters, " + | ||
"numbers and three special characters: '.' '-' and '_'. No spaces are allowed."); | ||
} | ||
} | ||
|
||
// Validate secret | ||
if (validationObj.hasOwnProperty("version") && validationObj.hasOwnProperty("secret")){ | ||
if (validationObj.version === "3" && validationObj.secret.length === 0){ | ||
isValid = false; | ||
errors.secret.push("When using SNMP version 3, secret must be specified"); | ||
} | ||
if (validationObj.secret.length > 0 && !validationObj.secret.match(/^[.a-zA-Z0-9_-]+$/)){ | ||
isValid = false; | ||
errors.secret.push("Secret can consist only of upper and lower english letters, " + | ||
"numbers and three special characters: '.' '-' and '_'. No spaces are allowed."); | ||
} | ||
} | ||
|
||
// Validate securityEngine | ||
if (validationObj.hasOwnProperty("securityEngine")){ | ||
if (validationObj.securityEngine.length > 0 && !validationObj.securityEngine.match(/^([A-F0-9]{10,64}|[a-f0-9]{10,64})$/)){ | ||
isValid = false; | ||
errors.securityEngine.push("If provided, Security Engine can consists only of 10-64 characters in " + | ||
"hexadecimal notation. All letter must be either upper or lowe case."); | ||
} | ||
} | ||
|
||
// Validate Walk Interval | ||
if (validationObj.hasOwnProperty("walkInterval")){ | ||
if (!(Number.isInteger(validationObj.walkInterval) && validationObj.walkInterval >= 1800)){ | ||
isValid = false; | ||
errors.walkInterval.push("Walk Interval number must be an integer greater than or equal 1800"); | ||
} | ||
} | ||
|
||
// Validate profiles | ||
if (validationObj.hasOwnProperty("profiles") && validationObj.hasOwnProperty("initProfiles")){ | ||
console.log(validationObj.initProfiles); | ||
for (const prof of validationObj.profiles){ | ||
if (!validationObj.initProfiles.includes(prof)){ | ||
isValid = false; | ||
errors.profiles.push(`${prof} is an invalid profile`); | ||
} | ||
} | ||
} | ||
|
||
return [isValid, errors]; | ||
}; | ||
|
||
export default validateInventoryAndGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.