javascript递归验证

时间:2014-11-11 13:35:51

标签: javascript json recursion tree

我想用条件(AND,NOT,OR)和id键验证json树。这样的事情:

{

    "OR": [
        {
            "AND": [
                {
                    "NOT": [
                        {
                            "id": 2
                        }
                    ]
                },
                {
                    "id": 2
                }
            ]
        },
        {
            "AND": [
                {
                    "id": 3
                },
                {
                    "id": 3
                }
            ]
        }
    ]

}

这是有效的树。但是如果json包含任何其他操作或者为空([],{}),它将无效。例如,

{
    "X": [ //INVALID TAG
        {
            "AND": [
                {
                    "NOT": [
                        {
                            //EMPTY
                        }
                    ]
                },
                {
                    "id": 2
                }
            ]
        },
        {
            "AND": [
                {
                    "id": 3
                },
                {
                    "id": 3
                }
            ]
        }
    ]

}

我的代码:

var validateRule = function (js) {
console.log('current validation ' + JSON.stringify(js));

if (js.hasOwnProperty('OR')) {
    return js.hasOwnProperty('length') ? js.length > 0 && validateRule(js.OR) : validateRule(js.OR);
}

if (js.hasOwnProperty('AND')) {
    return js.hasOwnProperty('length') ? js.length > 0 && validateRule(js.AND) : validateRule(js.AND);
}

if (js.hasOwnProperty('NOT')) {
    return js.hasOwnProperty('length') ? js.length > 0 && validateRule(js.NOT) : validateRule(js.NOT);
}

if (js.hasOwnProperty('length')) { //JSON Array
    if (js.length == 0) {
        return false;
    } else if (js.length == 1) {
        return js[0].hasOwnProperty('id');
    } else {
        for (var key in js) {
            if (js.hasOwnProperty(key)) {
                return validateRule(js[key]);
            }
        }
    }
} else {
    return js.hasOwnProperty('id');
}
};

但是因为在循环中我有'返回'代码不能正常工作。请给我建议。

1 个答案:

答案 0 :(得分:1)

异常可能是在嵌套验证中传递故障的最简洁方法。例如,

function classOf(p) {
    return {}.toString.call(p).slice(8, -1);
}

function check(expr) {

    if(classOf(expr) != "Object")
        throw SyntaxError("Object expected");

    if(!Object.keys(expr).length)
        throw SyntaxError("Empty object");

    return Object.keys(expr).forEach(function(key) {
        var val = expr[key];
        switch(key) {
            case "AND":
            case "OR":
            case "NOT":
                if(classOf(val) != "Array")
                    throw SyntaxError("Array expected");
                if(key == "NOT" && val.length !== 1)
                    throw SyntaxError("Incorrect number of arguments");
                if(key != "NOT" && val.length < 2)
                    throw SyntaxError("Incorrect number of arguments");
                val.forEach(check);
                break;
            case "id":
                if(classOf(val) != "Number")
                    throw SyntaxError("Number expected");
                break;
            default:
                throw SyntaxError("Invalid operator " + key);
        }
    });
}

顶级代码有点难看,但由于JS不支持类型catch块,这是不可避免的:

try {
    check(expr);
    // expression is valid, move on
} catch(e) {
    if(e instanceof SyntaxError)
       // expression is invalid, handle that
    else
       // something else went wrong
       throw e;
}