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