I am trying to model a json-schema that supports arbitrary boolean expressions like:
(prop1 > 'val1' || prop2 = 'val2') && !(prop3 in [1, 2, 3])
{
and: [
{
or: [
{ property: 'prop1', operator: 'gt', value: 'val1' },
{ property: 'prop2', operator: 'eq', value: 'val2' }
]
},
{
not: { property: 'prop3', operator: 'in', value: [1, 2, 3] }
}
]
}
This is the schema so far:
{
"$ref": "#/definitions/BooleanExpression",
"definitions": {
"BooleanExpression": {
"oneOf": [
{ "$ref": "#/definitions/BooleanCondition" },
{ "$ref": "#/definitions/BooleanAnd" },
{ "$ref": "#/definitions/BooleanOr" },
{ "$ref": "#/definitions/BooleanNot" }
]
},
"BooleanOperator": {
"type": "string",
"enum": [
"eq",
"gt",
"gte",
"lt",
"lte",
"in"
]
},
"BooleanCondition": {
"type": "object",
"required": [
"property",
"operator",
"value"
],
"properties": {
"property": {
"type": "string",
"minLength": 1
},
"operator": {
"$ref": "#/definitions/BooleanOperator"
},
"value": {
"type": [
"string",
"number",
"boolean",
"null",
"array"
]
}
}
},
"BooleanAnd": {
"type": "object",
"required": [ "and" ],
"additionalProperties": false,
"properties": {
"and": {
"type": "array",
"minItems": 2,
"items": {
"$ref": "#/definitions/BooleanExpression"
}
}
}
},
"BooleanOr": {
"type": "object",
"required": [ "or" ],
"additionalProperties": false,
"properties": {
"or": {
"type": "array",
"minItems": 2,
"items": {
"$ref": "#/definitions/BooleanExpression"
}
}
}
},
"BooleanNot": {
"type": "object",
"required": [ "not" ],
"additionalProperties": false,
"properties": {
"not": {
"$ref": "#/definitions/BooleanExpression"
}
}
}
}
}
I would like to express constraints reguarding allowed types for the $.value
field in BooleanCondition
based on the value of $.operator
field. For example, if $.operator = in
, $.value
should be only an array of integers or strings:
{
property: 'prop1',
operator: 'in',
value: 'should not compile'
}
{
property: 'prop1',
operator: 'in',
value: ['this', 'is', 'ok']
}
Or if $.operator = eq
, then $.value
can be of type string, number, boolean or null (but not array).
{
property: 'prop1',
operator: 'eq',
value: ['should', 'not', 'compile']
}
{
property: 'prop1',
operator: 'eq',
value: 'this is ok'
}
Is it possible to express these types of conditional constraints in the schema?