How to validate that an array contains only keywords from existing properties? #358
-
For my work, I have to write JSON files that basically define the UI layout for a test (language, trainings, etc.) on our platform. My team is not technical in any sense, so this task is rather challenging and error prone. To make things easier, I've put together a JSON Schema. It's really handy, the IDE now highlights when an error is made and provides suggestions for valid properties. However, in our JSON files we have array properties that must contain the keywords of existing properties (which are variable in number and name). It would be really helpful if that can be validated. Is this possible? In case, I haven't explained very well, here is a small code snippet of one of our JSON files and what we'd like to validate: {
"name": "Sample File",
"elements": {
"init": {
"editorType": {
"metaDataReferences": [
"Field1",
"Field2" // Field2 hasn't been defined in the "elements" block
]
}
},
"Field1": { // Field1 is a valid keyword since "elements" can contain properties that match the Regex "^[a-zA-Z0-9]*$"
"stringType": {
"argumentReferences": [
"Field3",
"Field4" // Field4 also hasn't been defined in the "elements" block
]
}
},
"Field3": {
"booleanType": {}
}
}
} Is it possible to validate that the keywords in both arrays are present in the "elements" block? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
There is no standardized way to do this, that is because JSON Schema is mostly limited to "structural" validation, rather than "data consistency" validation that depends on remembering values that have been defined elsewhere in the document. Some more information is available here: https://github.com/json-schema-org/json-schema-spec/wiki/Scope-of-JSON-Schema-Validation However this may still be possible, as JSON Schema is somewhat extensible. "$data" is one such solution. It will depend on the particular validator you're using. |
Beta Was this translation helpful? Give feedback.
There is no standardized way to do this, that is because JSON Schema is mostly limited to "structural" validation, rather than "data consistency" validation that depends on remembering values that have been defined elsewhere in the document.
Some more information is available here: https://github.com/json-schema-org/json-schema-spec/wiki/Scope-of-JSON-Schema-Validation
However this may still be possible, as JSON Schema is somewhat extensible. "$data" is one such solution. It will depend on the particular validator you're using.