如何在父级要求中引用子级属性 [英] How to refer child attribute in required of parent level

查看:96
本文介绍了如何在父级要求中引用子级属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {"$ schema":"https://json-schema.org/draft/2019-09/schema","$ id":"PersonalDetails.json","type":"object",属性":{标题":{"type":"object",属性":{"HeaderName":{"type":"string"},"HeaderValue":{"type":"string"}}},详细信息":{"type":"array",项目":{"type":"object",属性":{"FName":{"type":"string"},"LName":{"type":"string"},地址":{"type":"object",属性":{"FlatNo":{"type":"string"},部门":{"type":"string"},地标":{"type":"object",属性":{"LandMark1":{"type":"string"},"LandMark2":{"type":"string"}}}},必填":[部门"]}}}}}} 

示例数据:

 {标题":{" HeaderName":"DummyName","HeaderValue":"DummyName"},详细信息":[{"FName":"Chicago","LName":老挝",地址":{"FlatNo":"Excalibur",扇区":"07",地标":{"LandMark1":美国","LandMark2":"UK"}}}]} 

这是我尝试过的方法

 "allOf":[{如果":属性":{"FlatNo":{"const":"07"表示},地标":{属性":{"LandMark1":{"const":"USA";}},"then":"LandMarks":{必填":["LandMark2"]}}] 

所以要求是在JSON Schema中插入具有以下条件的IF然后条件

如果LandMark1 =美国,FlatNo = Excalibur,则需要LandMark2我无法弄清楚该条件及其语法的确切位置

我在父级尝试了Landmarks的if-then条件,但无论如何还是不起作用.

任何帮助都是非常重要的

解决方案

我不能完全确定更新问题所在的架构片段在哪里,但这是基于该片段的完全固定的架构...

您必须将 properties 从最远的树嵌套在JSON实例中,您希望该条件以及所得到的应用子模式具有上下文.

这是一个实时演示: https://jsonschema.dev/s/6VwCt

  {属性":{详细信息":{"items":{" allOf" ;: [{如果":{属性":{地址":{属性":{"FlatNo":{"const":"07"},地标":{属性":{"LandMark1":{"const":美国"}}}}}}},然后":{属性":{地址":{属性":{地标":{必填":["LandMark2"]}}}}}}]}}}} 

在您的示例数据中,我假设您将 FlatNo Sector 混合在一起,所以我在实时演示实例中对此进行了纠正.

这是在 Address 级别上的模式片段的外观...

  {属性":{地址":{" allOf" ;: [{如果":{属性":{"FlatNo":{"const":"07"},地标":{属性":{"LandMark1":{"const":美国"}}}}},然后":{属性":{地标":{必填":["LandMark2"]}}}}]}}} 

{
   "$schema":"https://json-schema.org/draft/2019-09/schema",
   "$id":"PersonalDetails.json",
   "type":"object",
   "properties":{
      "Header":{
         "type":"object",
         "properties":{
            "HeaderName":{
               "type":"string"
            },
            "HeaderValue":{
               "type":"string"
            }
         }
      },
      "Details":{
         "type":"array",
         "items":{
            "type":"object",
            "properties":{
               "FName":{
                  "type":"string"
               },
               "LName":{
                  "type":"string"
               },
               "Address":{
                  "type":"object",
                  "properties":{
                     "FlatNo":{
                        "type":"string"
                     },
                     "Sector":{
                        "type":"string"
                     },
                     "LandMarks":{
                        "type":"object",
                        "properties":{
                           "LandMark1":{
                              "type":"string"
                           },
                           "LandMark2":{
                              "type":"string"
                           }
                        }
                     }
                  },
                  "required":[
                     "Sector"
                  ]
               }
            }
         }
      }
   }
}

Example Data:


    {
       "Header":{
          "HeaderName":"DummyName",
          "HeaderValue":"DummyName"
       },
       "Details":[
          {
             "FName":"Chicago",
             "LName":"Laos",
             "Address":{
                "FlatNo":"Excalibur",
                "Sector":"07",
                "LandMarks":{
                   "LandMark1":"USA",
                   "LandMark2":"UK"
                }
             }
          }
       ]
    }

This is the approach I tried

"allOf":[
   {
      "if":"properties":{
         "FlatNo":{
            "const":"07"
         },
         "LandMarks":{
            "properties":{
               "LandMark1":{
                  "const":"USA"
               }
            },
            "then":"LandMarks":{
               "required":[
                  "LandMark2"
               ]
            }
         }
      ]

So the requirement is to insert IF then conditions in JSON Schema with following conditions

if LandMark1 = USA and FlatNo = Excalibur then LandMark2 is required I am not able to figure out where exactly should I place this condition and its syntax

I tried if-then condition for LandMarks on parent level but somehow even that was not working.

Any help is highly appreciable

解决方案

I'm not totally sure where you had the schema fragment you updated your question with, but here's a fully fixed schema based on that fragment...

You have to nest properties from the furthest up the tree in the JSON instance that you want the condition and resulting applied subschema to have context with.

Here's a live demo: https://jsonschema.dev/s/6VwCt

{
  "properties": {
    "Details": {
      "items": {
        "allOf": [
          {
            "if": {
              "properties": {
                "Address": {
                  "properties": {
                    "FlatNo": {
                      "const": "07"
                    },
                    "LandMarks": {
                      "properties": {
                        "LandMark1": {
                          "const": "USA"
                        }
                      }
                    }
                  }
                }
              }
            },
            "then": {
              "properties": {
                "Address": {
                  "properties": {
                    "LandMarks": {
                      "required": [
                        "LandMark2"
                      ]
                    }
                  }
                }
              }
            }
          }
        ]
      }
    }
  }
}

I've assumed in your example data you got FlatNo and Sector mixed up, so I've corrected that in the live demo instance.

Here is what the schema fragment would look like at the Address level...

{
  "properties": {
    "Address": {
      "allOf": [
        {
          "if": {
            "properties": {
              "FlatNo": {
                "const": "07"
              },
              "LandMarks": {
                "properties": {
                  "LandMark1": {
                    "const": "USA"
                  }
                }
              }
            }
          },
          "then": {
            "properties": {
              "LandMarks": {
                "required": [
                  "LandMark2"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

这篇关于如何在父级要求中引用子级属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆