I have to validation my input of json data. So the input object will be like
components: {["A","B","C","D","E"]};
so i have to validate some cases here. well i have passed those cases i need to know is there any better way out here.
so the logic is :
- first element can only include A,B,C
- second can only include D, E
- third can only include F,G,H
- fourth can only include I,J
- fifth can only include K,L
function validationInput(obj){
// logic
// it must have 5 inputs and include one of each
// A or B or C
// D or E
// F or G or H
// I or J
// K or L
// if A exist then check if B and C doesnt
// then check D or E exist then check it must doesnt exist other
// then check or parts toooo......
const arr = obj.components.sort();
console.log(arr);
if(arr[0] === "A" || arr[0] ==="B" || arr[0] ==="C"){
console.log("pass1");
if(arr[1] === "D" || arr[1] ==="E"){
console.log("pass 2");
if(arr[2] === "F" || arr[2] === "G" || arr[2] === "H"){
console.log("pass 3");
if(arr[3] === "I" || arr[3] === "J"){
console.log("pass 4");
if(arr[4] === "K" || arr[4] === "L"){
console.log("pass 5");
return true;
} else return false;
} else return false;
} else return false;
} else return false;
} else return false;
};
After Suggestion
function validationInput(obj){
const arr = obj.components.sort();
if(arr[0] !== "A" && arr[0] !=="B" && arr[0] !=="C") return false;
if(arr[1] !== "D" && arr[1] !=="E") return false;
if(arr[2] !== "F" && arr[2] !== "G" && arr[2] !== "H") return false;
if(arr[3] !== "I" && arr[3] !== "J") return false;
if(arr[4] !== "K" && arr[4] !== "L") return false;
return true;
};