Variable properties in JSON schema
I have the following JSON example for which I want to write a JSON schema. The limitation is that property2 contains a list of strings that are dynamic, depending on the dataset. And in the property3 object, some properties are called afters by these strings.
{
"property1": "value",
"property2": ["value1","value2","value2"],
"property3": {
"title": "test",
"value1": "hello",
"value2": "world"
}
}
JSON-Schema might look like this, but I don't know how to describe dynamic properties. Is it possible?
{
"title": "Test Object",
"type": "object",
"properties": {
"property1": {
"type": "string"
}
"property1": {
"type": "array"
}
"property3": {
"type": "object",
"properties": {
"title": {
"type": "string"
}
[ Something is missing here ]
}
}
}
}
+3
source to share
1 answer
You cannot bind property values to property keys using Json-Schema (as from v4 project).
You can:
- Define property2 as a schema and reference it (#ref) from property3 (dependencies, allOf, anyOf, additional properties ...). If these property names are very dynamic in your case, you can build this schema on the fly.
- Use patternProperties to find valid property names in property3 for some fixed regex.
+1
source to share