Хорошо, вот как я это решил: я создаю объект, который определяет, как должны выглядеть мои сложные объекты. Проект, в некотором смысле.
var structure = {
property1: {id:"name", type:"string", required:false},
property2: {
id:"categories", type:"object", required:true,
content:{
property1:{
type:"object", min:1,
content:{
property1:{id:"depth", type:"positiveinteger", required:false},
property2:{
id:"groups", type:"object", required:true,
content:{
property1:{
type:"array", min:1,
content:{
type:"object", min:1,
content:{
property1:{id:"myFunction", type:"function", required:true},
property2:{id:"args", type:"array", required:false},
}
}
}
}
}
}
}
}
}
}
Затем я запускаю функцию, которая сравнивает проект ("struct") с объектом, который будет проверен ("def"):
function compareStructure(struct, def){
if(isArray(def)){
if(isDefined(struct.min) && def.length < struct.min){ alert("Error in structur check: " + " min is " + struct.min + "."); return false}
if(isDefined(struct.max) && def.length > struct.max){ alert("Error in structur check: " + " max is " + struct.max + "."); return false}
for(var i = 0; i < def.length; i++){
compareStructure(struct.content, def[i]);
}
} else {
for(var k in struct){
if(struct[k].id){
propFound = false;
for(var m in def) if(m == struct[k].id) propFound = m;
if(!propFound && struct[k].required){ alert("Error in structure check: " + struct[k].id + " not defined."); return false}
if(propFound && !verifyThis(struct[k], def[propFound], propFound)) return false;
if(propFound && struct[k].content) compareStructure(struct[k].content, def[propFound]);
} else {
for(var m in def){
if(!verifyThis(struct[k], def[m], m)) return false;
if(struct[k].content) compareStructure(struct[k].content, def[m]);
}
}
}
}
}
function verifyThis(struct, def, prop){
// This is where the checks for types and values are made.
}
Функция сравнения все еще находится в стадии разработки, но сейчас я использую эту концепцию.