Mikhail P. Mikhail P. - 13 days ago 5
JSON Question

Json schema to validate object's values against content of another object

I'm trying to create json schema for a document where field values in some object should validate against a enum defined in another object in the same document.

More specifically, in the example below, I'd like to be able to define "properties" with

id
and
values
(I should be able to define different properties in different json files).
Then "objects" should be able to refer to these properties, so that
object.properties[i].id
must match with id of one of the properties and
object.properties[i].value
must match with one of the enum values defined for that property.

{
"properties": [
{
"id": "SIZE",
"values": ["small", "medium", "big"]
},
{
"id": "MATERIAL",
"values": ["wood", "glass", "steel", "plastic"]
},
{
"id": "COLOR",
"values": ["red", "green", "blue"]
}
],

"objects": [
{
"name": "chair",
"properties": [
{
"id": "SIZE",
"value": "small"
},
{
"id": "COLOR",
"value": "red"
}
],
},
{
"name": "table",
"properties": [
{
"id": "MATERIAL",
"value": "wood"
}
]
}
]
}


I tried to create json schema to validate such structure, but got stuck with describing reference to inner fields of "property" object. I also looked into the standard and did not find a way to achieve the goal.

Is it possible to create a json schema which would validate my json files?

esp esp
Answer

There is a proposal for $data reference that allows to do it. It's is supported in Ajv (I am the author)

Comments