Any way to ignore a property being required if it does not exist in the properties
definition?
For example:
{
"type": "object",
"properties": {
"billingAddress": {
"type": "string"
},
"isSameAsBillingAddress": {
"type": "boolean"
}
// Say I deleted the `mailingAddress` property
},
"required": [
"billingAddress",
"isSameAsBillingAddress",
"mailingAddress" // ignore
]
}
From my understanding, this doesn't seem possible. I know this is kind of a silly question as someone may say to just remove the property name from the required
array, but it becomes a bit more complicated when I am generating this JSON schema and also want to conditionally require a field if it exists.
For example:
{
"type": "object",
"properties": {
"billingAddress": {
"type": "string"
},
"isSameAsBillingAddress": {
"type": "boolean"
}
// Delete `mailingAddress` property
},
"required": [
"billingAddress",
"isSameAsBillingAddress"
],
"oneOf": [
{
"properties": {
"isSameAsBillingAddress": {
"enum": [true]
}
}
},
{
"properties": {
"isSameAsBillingAddress": {
"enum": [false]
}
},
// Would like to ignore...
"required": ["mailingAddress"]
},
]
}
Yes, I could write some code to go back and clean up this schema if the mailingAddress
property gets deleted, but I was wonder if there was some JSON schema way of intelligently ignoring undefined properties
?
Also from a JSON schema generator perspective, there just seems to be too many places a property name could be lurking, so any advice on how to structure this where code could easily clean up and generate a coherent JSON schema would be appreciated too.
This seems like something better suited to a schema linter -- to warn you that a property definition is missing and perhaps accidentally deleted.
You can find a list of linters here -- https://json-schema.org/implementations.html#schema-linter -- if this check isn't implemented yet, it is likely fairly easy to do so.