I have input like
{'OR': [false, true, {'AND': [true, true]}]}
and so on. It evaluates to
(false || true || (true && true))
which is true
I need to check if input has any key / value which is null / undefined / numeric key / not in above format
keys can be 'OR' 'AND', Array values only need to be boolean
var isValid = true;
var checkArr = function(obj){
if(obj === null){
isValid = false;
return;
}
if(Array.isArray(obj)){
obj.forEach(function(k){
if(!(typeof(k) === 'boolean' || typeof(k) === 'object' && (k !== null && k !== undefined))){
isValid = false;
return;
}
});
}
else if(typeof(obj) === 'object'){
var key = Object.keys(obj)[0];
if(['OR', 'AND'].indexOf(key) === -1){
isValid = false;
return;
}
}
Object.keys(obj).forEach(function(key){
if(typeof(obj[key]) === 'object'){
checkArr(obj[key]);
}
});
};
Is there a more elegant way to do this?
You could check the types and use an iterative and recursive approach