I receive a JSON object through an HTTP request.I just want to check if that object has only the fields that I want, don't need to check their value. I did this:
function checkObject(o1, o2) {
for (const p1 in o1) {
for (const p2 in o2) {
if (o1.hasOwnProperty(p2) === false) {
return false;
}
if (o2.hasOwnProperty(p1) === false) {
return false;
}
}
}
return true;
}
This is the valid object. I want to accept only requests with this body, don't care about the values.
const validObject = {
country: '',
name: '',
zip: '',
//etc
};
after this i do an if statement like:
if(checkObject(validObject, receivedObject)){
//do stuff here
}else{
//reject the request
}
Ok this works, but it is too ugly. It does not pass the lint test and there are too many for loops. What do you think? is there an alternative to do the same thing? Thank you!!