3
votes

I need to create schema for simple grammar expressions, where:

{Expression}:
     {Function}
     OR {Variable}
{Function}:   
     FunctionName [array of {Expression}]
{Variable}:
     VariableName

So I can write JSON tree expressions like this:

{
    "Expression": {
        "function": "MyFunc",
        "args": [ { "varName": "X1" }, { "varName": "X2" }, { "function": "MyFunc2", "args": [ { "varName": "123" } ] } ]
    }
}

I need to have ability of additionalProperties both for functions and variables. Here is the schema that I created:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "var": {
            "type": "object",
            "properties": {
                "varName": { "type": "string" }
            },
            "required": [ "varName" ]
        },
        "func": {
            "type": "object",
            "properties": {
                "function": { "type": "string" },
                "args": { "type": "array", "items": { "$ref": "#/definitions/expr" } }
            },
            "required": [ "function" ]
        },
        "expr": {
            "oneOf": [
                { "$ref": "#/definitions/var" },
                { "$ref": "#/definitions/func" }
            ]
        }
    },
    "type": "object",
    "properties": {
        "Expression": { "$ref": "#/definitions/expr" }
    }
}

For some reason it does not pass validation, for expression above in Visual Studio 2013 Update 5, showing the following errors:

1) The required property "varName" was not present

2) The value of property "Expression" must conform to exactly one of the following schemas: { "type": "object", "properties": { "varName": { "type": "string" } }, "required": [ "varName" ] }, { "type": "object", "properties": { "function": { "type": "string" }, "args": { "type": "array", "items": { "$ref": "#/definitions/expr" } } }, "required": [ "function" ] }

Any ideas are appreciated

1

1 Answers

1
votes

Your schema is well written and without errors. The problem must be in the validator you are using. The schema validates successfully at both http://jsonschemalint.com/draft4/# and http://json-schema-validator.herokuapp.com/. I also reviewed it in detail and found no errors.

One thing you can try is to use anyOf instead of oneOf. It will produce the same result as oneOf because the two schemas are mutually exclusive. If you are lucky, the bug is affecting oneOf but not anyOf.