Per this question jsonschema attribute conditionally required, I can apply conditional required properties. However, it can only depend on the properties on the same level of object. In certain case, I want to one property required depend on its parent object property, is it possible? For the below example:
{
type: 'object',
properties: {
{
os: { type: 'string', enum: ['macOs', 'windows'] },
specs: {
macModel: {
type: 'string',
enum: ['macbook air', 'macbook pro', 'macbook']
},
memory: { type: 'number' }
}
}
}
}
Is it possible to meet this requirement: /spec/macModel is required only when /os equals to macOs?
Yes, the same approach applies. You just need to nest schemas a little deeper.
Notice that in the
/definitions/requires-macModel
schema it has to dig into the "specs" property and place therequired
there instead of at the top level as it is in the flat case.I've used the implication pattern for this example, but the same approach can be taken with
if
-then
if you prefer that approach and have access to a draft-07 validator.