好的,这就是我解决它的方法:我创建了一个对象,它定义了我的复杂对象应该是什么样子。某种意义上的蓝图。
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.
}
比较功能仍在进行中,但这是我现在要使用的概念。