Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

json - Referring child nodes in allOf if then condition in parent node

    {
   "$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"
                  ]
               }
            }
         }
      }
   }
}



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



    
    

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

  1. if LandMark1 = USA and FlatNo = Excalibur then Sector is required

I am not able to figure out where exactly should i place this condition and its syntax

i tried using $ref in if then conditions in allOf in Details as its parent for both Address and LandMarks but even that didn't work as it was not able to figure out reference

Any help is highly apprecitiable

question from:https://stackoverflow.com/questions/65930876/referring-child-nodes-in-allof-if-then-condition-in-parent-node

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll need to use the if/then keywords to describe this. The hard part is the if schema.

You need to define an if schema that will validate to true when given an instance where your conditions are met. It can help to develop this schema separately and then add it to your full schema.

{
  "type": "object",
  "properties": {
    "FlatNo": { "const": "Excalibur" },
    "LandMarks": {
      "type": "object",
      "properties": {
        "LandMark1": { "const": "USA" }
      },
      "required": ["LandMark1"]
    }
  },
  "required": ["LandMarks", "FlatNo"]
}

The following instance would pass validation against that schema. It would fail if FlatNo != Excalibur or LandMark1 != USA.

{
  "FlatNo":"Excalibur",
  "Sector":"07",
  "LandMarks":{
    "LandMark1":"USA",
    "LandMark2":"UK"
  }
}

Now it should be trivial to apply if/then to your address schema.

"if": { ... schema from above ... },
"then": { "required": ["Sector"] }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...