Reputation: 10144
I am constructing a schema using JSON-schema, part of which describes mathematical operations (such as add, multiply, subtract, divide, etc).
These operations can take operands that are variables, but they can also take instances of themselves. That is "add"
and "multiply"
types may be represented as follows:
"add": {
"description": "The mathematical operation of adding two or more operands together",
"type": "array",
"minItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/variable" },
{ "$ref": "#/definitions/subformula" },
{ "$ref": "#/definitions/add" },
{ "$ref": "#/definitions/multiply" },
{ "$ref": "#/definitions/subtract" },
{ "$ref": "#/definitions/divide" },
{ "$ref": "#/definitions/sum" },
{ "$ref": "#/definitions/maximum" },
{ "$ref": "#/definitions/average" }
]
}
},
"multiply": {
"description": "The mathematical operation of multiplying two or more operands together",
"type": "array",
"minItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/variable" },
{ "$ref": "#/definitions/subformula" },
{ "$ref": "#/definitions/add" },
{ "$ref": "#/definitions/multiply" },
{ "$ref": "#/definitions/subtract" },
{ "$ref": "#/definitions/divide" },
{ "$ref": "#/definitions/sum" },
{ "$ref": "#/definitions/maximum" },
{ "$ref": "#/definitions/average" }
]
}
}
For JSON such as:
"add": [ 5, 6, "multiply": [ 2, 3 ] ]
This list of type references is similar for every operation and sub-formula. This is rather cumbersome in the case that more operations might be added they need to be added to this list at every place that the list occurs...
Is it possible to somehow markup the operations such that I could refer to them as a group, something like the following:
"add": {
"description": "The mathematical operation of adding two or more operands together",
"type": "array",
"minItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/variable" },
{ "$ref": "#/definitions/subformula" },
{ "$ref": "#/definitions/operations/" } //note the trailing '/'
]
}
},
"multiply": {
"description": "The mathematical operation of multiplying two or more operands together",
"type": "array",
"minItems": 2,
"items": {
"anyOf": [
{ "$ref": "#/definitions/variable" },
{ "$ref": "#/definitions/subformula" },
{ "$ref": "#/definitions/operations/" } //note the trailing '/'
]
}
}
Upvotes: 1
Views: 214
Reputation: 12315
You are already using definitions
. The value of a key in the definitions
key word must be a JSON Schema. As such, you can combine anyOf
and $ref
similar to how you have done already.
The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. The keyword does not directly affect the validation result.
This keyword's value MUST be an object. Each member value of this object MUST be a valid JSON Schema.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-9
"items": {
"$ref": "#/definitions/myOtherGroup",
}
...
"definitions": {
"operations": {
"anyOf": [
{ "$ref": "#/definitions/add" },
{ "$ref": "#/definitions/multiply" },
{ "$ref": "#/definitions/subtract" },
{ "$ref": "#/definitions/divide" },
{ "$ref": "#/definitions/sum" },
{ "$ref": "#/definitions/maximum" },
{ "$ref": "#/definitions/average" }
]
}
"myOtherGroup": {
"anyOf": [
{ "$ref": "#/definitions/variable" },
{ "$ref": "#/definitions/subformula" },
{ "$ref": "#/definitions/operations" },
]
}
}
You would just need to change the name of "myOtherGroup" to whatever you want to call it. I'm assuming your other definitions are already there but you just didn't include them in your question (which is fine for this purpose).
Upvotes: 1