使用jsonSchema在其他选择框的基础上加载选择框选项

时间:2016-01-29 07:09:18

标签: json jsonschema json-schema-validator

我使用jsonschema生成表单并验证这些表单。

以下是json示例:

{
  "title": "Microsoft Account Request",
  "readOnly": false,
  "$schema": "http://json-schema.org/draft-04/hyper-schema",
  "description": "Microsoft Azure Account Request Product Specification",
  "properties": {
    "product": {
      "title": "Product",
      "dataBinding": {"references": ["SPEC_ID#/properties/service"]},
      "properties": {
        "offers": {
          "title": "Product Offers",
          "propertyOrder": 1,
          "type": "array",
          "uniqueItems": true,
          "format": "tabs",
          "items": {
            "title":"Product Offer",
            "properties": {
              "category": {
                "title": "Category",
                "readOnly": false,
                "unique":true,
                "strictProperties": true,
                "enum": [
                  "Cloud Services",
                  "Virtual Machines",
                  "Azure App Service",
                  "Batch"


                ],
                "options": {
                    "dependencies": [
                      {"id":"subcategoryAdd", "value":true}
                    ]
                  },
                "description": "Select category",
                "propertyOrder": 1,
                "type": "string"
              },
              "subcategory": {
                  "id":"subcategoryAdd",
                "title": "Sub - Category",
                "readOnly": false,
                "strictProperties": true,
                "description": "Select Sub-Category",
                 "options": {
                    "hide_display": true
                  },
                "enum": [
                  "Build and Deployment",
                  "Application Insights"
                ],
                "propertyOrder": 2,
                "type": "string"
              }
            },
            "type": "object"
          }
        }

      },
      "type": "object"
    }
  },
  "type": "object"
}

示例输出:

enter image description here

在输出表单中,我突出显示了子类别选项,它的选择框应该根据所选类别加载。

例如,如果我选择batch,则子类别选项a,b,c应显示在子类别的选择框中,如果我选择Azure app service,则应显示子类别选项d,e,f在子类别的选择框中。

我用dependencies尝试但是徒劳无功。另外,我尝试使用watchenumSource提到here

任何帮助都是值得的。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用以下JSON模式验证您的类别/子类别关系。

{
  "type": "object",
  "anyOf": [
    {
      "properties": {
        "category": { "enum": ["foo"] },
        "subCategory": { "enum": ["asdf", "jkl;"] }
      }
    },
    {
      "properties": {
        "category": { "enum": ["bar"] },
        "subCategory": { "enum": ["asdf", "qwer", "uiop"] }
      }
    }
  ]
}

但是,这并不意味着您使用的表单生成器将能够基于此创建表单。如果可以,我会留下深刻的印象。