What is the best way to test the integrity of a complex object in JavaScript?
My object has a bunch of different variables, some optional, some required. The correct structure is vital to the code’s functionality, but if I make a mistake during the definition, finding the exact value that caused the problem could get very tedious. Especially with error messages that tell me no more than “Somwehere in the code you’re using the wrong variable type!”.
My object could look something like this, for example:
{
name:"Test",
categories:{
A:{
depth:1,
groups:{
main:[
{myFunction:funcA, arg:[1,2]},
{myFunction:funcB}
]
}
},
B:{
groups{
main:[
{myFunction:funcC}
],
secondary:[
{myFunction:funcD}
]
}
}
}
}
Thanks!
Okay, this is how I’ve solved it: I create an object that defines what my complex objects should look like. A blueprint, in a way.
Then I run a function that compares the blueprint (“struct”) with the object to be tested (“def”):
The comparison function is still work in progress, but that’s the concept I’m going with for now.