jsonSchema attribute conditionally required depends on parent object

大城市里の小女人 提交于 2019-12-01 09:01:49

问题


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?


回答1:


Yes, the same approach applies. You just need to nest schemas a little deeper.

{
  "type": "object",
  "properties": {
    "os": { "enum": ["macOs", "windows"] },
    "specs": {
      "macModel": { "enum": ["macbook air", "macbook pro", "macbook"] },
      "memory": { "type": "number" }
    }
  },
  "allOf": [{ "$ref": "#/definitions/os-macOs-requires-macModel" }],
  "definitions": {
    "os-macOs-requires-macModel": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/os-macOs" } },
        { "$ref": "#/definitions/requires-macModel" }
      ]
    },
    "os-macOs": {
      "properties": {
        "os": { "const": "macOs" }
      },
      "required": ["os"]
    },
    "requires-macModel": {
      "properties": {
        "specs": {
          "required": ["macModel"]
        }
      }
    }
  }
}

Notice that in the /definitions/requires-macModel schema it has to dig into the "specs" property and place the required 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.



来源:https://stackoverflow.com/questions/51203606/jsonschema-attribute-conditionally-required-depends-on-parent-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!