0

I want to validate json tree with conditions (AND, NOT, OR) and id keys. Something like that:

{

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

}

This is valid tree. But if json contains any other operations or has empty ([], {}) it will be invalid. For example,

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

}

My code:

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');
}
};

But since in loop i have 'return' code not work properly. Give me advice please.

3
  • did you understand the difference between json and array? Commented Nov 11, 2014 at 13:45
  • yes =) json it's format, array is datatype. Commented Nov 11, 2014 at 13:50
  • you return does not work because you return only the first value of the array Commented Nov 11, 2014 at 13:56

1 Answer 1

1

Exceptions is perhaps the cleanest way to pass failures around in the nested validation. For example,

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);
        }
    });
}

The top-level code will be a bit ugly, but since JS doesn't support typed catch blocks, this is unavoidable:

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. But in case "NOT": [{}] doesn't work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.