I am using JSON for defining some configuration files and I want to validate them with json scheme. My problem is that I want to ensure that keys of some object should be subset of items of an array defined in same JSON:
ex:
Valid:
{
"files": ["file1", "file2"],
"filelocations": {
"file1": "/etc/globalconfigs/file1.conf",
"file2": "/usr/bin/file2.sh"
}
}
Invalid (otherkey is not in files):
{
"files": ["file1", "file2"],
"filelocations": {
"file1": "/etc/globalconfigs/file1.conf",
"otherkey": "/usr/bin/file2.sh"
}
}
etc. What I want is to ensure that keys of filelocations are found in files array.
Although in this example, we can change the structure of JSON by combining keys-values so that there is no need to have this kind of constraint, in my case I can’t change JSON like this, so it is nice to have a validation mechanism for this.
How can I achieve this?
You cannot achieve this with JSON Schema, there are no combination of keywords which can guarantee this.
If you are adventurous (and I can even code that for you), you can use my JSON Schema API and code a custom keyword to fit your needs, however. It is doable.