I would use a schema-like object that contains all the keys that need to exist in the object and recursively check if an object contains those keys or not using a function like so:
const verifyObject = (object, schema) =>
Object.entries(schema).every(([key, value]) =>
object && object[key] && (typeof value != "object" || verifyObject(object[key], value))
);
The object[key] test will not only verify that the key exists but it will also verify that the value for that key is truthy which may fail the whole test if a value contains a falsy value. If you only want it to check if the key exists, then replace it with key in object.
The schema-like object for you JanrainAppSettings object is simply an object containing all the keys that needs to exists in a valid JanrainAppSettings, the values could be just 0s or empty strings as they are not needed:
const janrainAppSettingsSchema = { JanrainAppSettings: { settings: { capture: { clientId: "", appId: "", appDomain: "", captureServer: "" }, tokenUrl: "", httpLoadUrl: "", httpsLoadUrl: "" } } };
So to verify an object, just call verifyObject and pass the object and the schema-like object to verify against like so:
if(verifyObject(options, janrainAppSettingsSchema)) {
// everything is ok
}
Demo:
const verifyObject = (object, schema) =>
Object.entries(schema).every(([key, value]) =>
object && object[key] && (typeof value != "object" || verifyObject(object[key], value))
);
let thisWillPass = {
JanrainAppSettings: {
settings: {
capture: {
clientId: "Id",
appId: "Id",
appDomain: "Domain",
captureServer: "Server"
},
tokenUrl: "Url",
httpLoadUrl: "Url",
httpsLoadUrl: "Url"
}
}
};
let thisWontPass = {
JanrainAppSettings: {
settings: {
capture: {
clientId: "Id",
appId: "Id",
// appDomain: "Domain",
captureServer: "Server"
},
tokenUrl: "Url",
httpLoadUrl: "Url",
httpsLoadUrl: "Url"
}
}
};
const janrainAppSettingsSchema = { JanrainAppSettings: { settings: { capture: { clientId: "", appId: "", appDomain: "", captureServer: "" }, tokenUrl: "", httpLoadUrl: "", httpsLoadUrl: "" } } };
console.log("Will pass:", verifyObject(thisWillPass, janrainAppSettingsSchema));
console.log("Won't pass:", verifyObject(thisWontPass, janrainAppSettingsSchema));