I have a JSON schema (v7.0), in which I need to ensure that exactly the properties listed are present in the final formData.
For most cases, this can be solved using "additionalProperties": false
and "minProperties": X
, where X represents the #properties in an object (courtersy of this solution)
However, in some cases the number of properties is variable.
This is the case in below example, where a selection of "Manual update" (enum 2) in "sync" can result in the field "manual_date" being added. When "sync" is set to "Manual update", the "manual_date" property needs to be required - but otherwise it should not be required.
I tried implementing this via the "required" statement under dependencies, but I believe this is wrong as I get some very extensive error messages when testing it via a validator.
So in short: I'm looking for the correct way to make enum-dependent fields required in my JSON schema.
My schema:
{
"type": "object",
"properties": {
"rtc": {
"title": "REAL-TIME-CLOCK (RTC)",
"type": "object",
"properties": {
"sync": {
"title": "Time synchronization method",
"type": "integer",
"default": 1,
"anyOf": [
{
"type": "integer",
"title": "Retain current time",
"enum": [
1
]
},
{
"type": "integer",
"title": "Manual update",
"enum": [
2
]
},
{
"type": "integer",
"title": "Automatic update (requires WiFi)",
"enum": [
3
]
}
]
},
"timezone": {
"title": "Time zone (UTC−12 to UTC+14)",
"type": "number",
"default": 0,
"minimum": -12,
"maximum": 14
},
"adjustment": {
"title": "Adjustment (-129600 to 129600 seconds)",
"type": "number",
"default": 0,
"minimum": -129600,
"maximum": 129600
}
},
"dependencies": {
"sync": {
"oneOf": [
{
"properties": {
"sync": {
"enum": [
1
]
}
}
},
{
"properties": {
"sync": {
"enum": [
2
]
},
"manual_date": {
"title": "Time (UTC) ",
"type": "string",
"format": "date-time"
}
},
"required": [
"manual_date"
]
},
{
"properties": {
"sync": {
"enum": [
3
]
}
}
}
]
}
},
"additionalProperties": false,
"patternProperties": {
"manual_date": {}
},
"minProperties": 3
}
}
}