0

I have a long JSON document that I need to validate against the schema. I have certain sub sections of the JSON that only need to validated if there is data present. Here is a sub-section of my JSON that I am trying to validate only if there is required data present in that sub-section:

{
      "Entity":{
        "Base":{
          "Key":"",
          "SubmittedDate":"1/1/1900",
          "Address":[
              {
                "Key":"",
                "Type": ["Physical", "Mailing", "Primary", "Secondary"],
                "LineOne":"123 here st",
                "LineTwo": "",
                "CountyOrigin":"",
                "CityTown": "Anytown",
                "StateProvidence": "CA",
                "Country": "US",
                "PostalCode": "33333"
              }
            ]
        }
      }
    }

The "Address" node is conditional. If there an address in the array, I need to validate the required fields are present in the address. They address may not be present since it isn't require for the "Base" node. Here is the JSON Schema that I got started:

  {
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "required": [
        "Entity"
    ],
    "properties": {
        "Entity": {
            "$id": "#/properties/Entity",
            "type": "object",
            "title": "The Entity schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
            "required": [
                "Base"
            ],
            "properties": {
                "Base": {
                    "$id": "#/properties/Entity/properties/Base",
                    "type": "object",
                    "title": "The Base schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": {},
                    "required": [
                        "Key",
                        "SubmittedDate"
                    ],
                    "properties": {
                        "Key": {
                            "$id": "#/properties/Entity/properties/Base/properties/Key",
                            "type": "string",
                            "title": "The Key schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": ""
                        },
                        "SubmittedDate": {
                            "$id": "#/properties/Entity/properties/Base/properties/SubmittedDate",
                            "type": "string",
                            "title": "The SubmittedDate schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": ""
                        },
                        "Address": {
                            "$id": "#/properties/Entity/properties/Base/properties/Address",
                            "type": "array",
                            "title": "The Address schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": [],
                            "additionalItems": true,
                            "items": {
                                "$id": "#/properties/Entity/properties/Base/properties/Address/items",
                                "anyOf": [
                                    {
                                        "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0",
                                        "type": "object",
                                        "title": "The first anyOf schema",
                                        "description": "An explanation about the purpose of this instance.",
                                        "default": {},
                                        "required": [
                                            "Type",
                                            "LineOne",
                                            "CountyOrigin",
                                            "CityTown",
                                            "StateProvidence",
                                            "Country",
                                            "PostalCode"
                                        ],
                                        "properties": {
                                            "Key": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Key",
                                                "type": "string",
                                                "title": "The Key schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "Type": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Type",
                                                "type": "array",
                                                "title": "The Type schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": [],
                                                "additionalItems": true,
                                                "items": {
                                                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Type/items",
                                                    "anyOf": [
                                                        {
                                                            "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Type/items/anyOf/0",
                                                            "type": "string",
                                                            "title": "The first anyOf schema",
                                                            "description": "An explanation about the purpose of this instance.",
                                                            "default": ""
                                                        }
                                                    ]
                                                }
                                            },
                                            "LineOne": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/LineOne",
                                                "type": "string",
                                                "title": "The LineOne schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "LineTwo": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/LineTwo",
                                                "type": "string",
                                                "title": "The LineTwo schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "CountyOrigin": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/CountyOrigin",
                                                "type": "string",
                                                "title": "The CountyOrigin schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "CityTown": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/CityTown",
                                                "type": "string",
                                                "title": "The CityTown schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "StateProvidence": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/StateProvidence",
                                                "type": "string",
                                                "title": "The StateProvidence schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "Country": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Country",
                                                "type": "string",
                                                "title": "The Country schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            },
                                            "PostalCode": {
                                                "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/PostalCode",
                                                "type": "string",
                                                "title": "The PostalCode schema",
                                                "description": "An explanation about the purpose of this instance.",
                                                "default": ""
                                            }
                                        },
                                        "additionalProperties": true
                                    }
                                ]
                            }
                        }
                    },
                    "additionalProperties": true
                }
            },
            "additionalProperties": true
        }
    },
    "additionalProperties": true

}

I only want to validate the Address section if there is an address present in the array. I modified the require fields in the schema to not require address and making sure that in the address schema that there were required fields. If I don't add and address it will validate, but if I put in an address to the array and leave out a required field for the address, it still validates. How do I validate the address if present?

john
  • 1,273
  • 3
  • 16
  • 41
  • Does this answer your question? [jsonSchema attribute conditionally required](https://stackoverflow.com/questions/38717933/jsonschema-attribute-conditionally-required) – Relequestual Sep 23 '20 at 08:18

1 Answers1

0

I am not certain I understand the purpose of the anyOf in the specification of Address items. If you simplify it like this:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "http://example.com/example.json",
  "type": "object",
  "title": "The root schema",
  "description": "The root schema comprises the entire JSON document.",
  "default": {
    
  },
  "required": [
    "Entity"
  ],
  "properties": {
    "Entity": {
      "$id": "#/properties/Entity",
      "type": "object",
      "title": "The Entity schema",
      "description": "An explanation about the purpose of this instance.",
      "default": {
        
      },
      "required": [
        "Base"
      ],
      "properties": {
        "Base": {
          "$id": "#/properties/Entity/properties/Base",
          "type": "object",
          "title": "The Base schema",
          "description": "An explanation about the purpose of this instance.",
          "default": {
            
          },
          "required": [
            "Key",
            "SubmittedDate"
          ],
          "properties": {
            "Key": {
              "$id": "#/properties/Entity/properties/Base/properties/Key",
              "type": "string",
              "title": "The Key schema",
              "description": "An explanation about the purpose of this instance.",
              "default": ""
            },
            "SubmittedDate": {
              "$id": "#/properties/Entity/properties/Base/properties/SubmittedDate",
              "type": "string",
              "title": "The SubmittedDate schema",
              "description": "An explanation about the purpose of this instance.",
              "default": ""
            },
            "Address": {
              "$id": "#/properties/Entity/properties/Base/properties/Address",
              "type": "array",
              "title": "The Address schema",
              "description": "An explanation about the purpose of this instance.",
              "default": [
                
              ],
              "additionalItems": true,
              "items": {
                "$id": "#/properties/Entity/properties/Base/properties/Address/items",
                "type": "object",
                "title": "Address item schema",
                "description": "An explanation about the purpose of this instance.",
                "default": {
                  
                },
                "required": [
                  "Type",
                  "LineOne",
                  "CountyOrigin",
                  "CityTown",
                  "StateProvidence",
                  "Country",
                  "PostalCode"
                ],
                "properties": {
                  "Key": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Key",
                    "type": "string",
                    "title": "The Key schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "Type": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Type",
                    "type": "array",
                    "title": "The Type schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": [
                      
                    ],
                    "additionalItems": true,
                    "items": {
                      "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Type/items",
                      "anyOf": [
                        {
                          "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Type/items/anyOf/0",
                          "type": "string",
                          "title": "The first anyOf schema",
                          "description": "An explanation about the purpose of this instance.",
                          "default": ""
                        }
                      ]
                    }
                  },
                  "LineOne": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/LineOne",
                    "type": "string",
                    "title": "The LineOne schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "LineTwo": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/LineTwo",
                    "type": "string",
                    "title": "The LineTwo schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "CountyOrigin": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/CountyOrigin",
                    "type": "string",
                    "title": "The CountyOrigin schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "CityTown": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/CityTown",
                    "type": "string",
                    "title": "The CityTown schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "StateProvidence": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/StateProvidence",
                    "type": "string",
                    "title": "The StateProvidence schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "Country": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/Country",
                    "type": "string",
                    "title": "The Country schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  },
                  "PostalCode": {
                    "$id": "#/properties/Entity/properties/Base/properties/Address/items/anyOf/0/properties/PostalCode",
                    "type": "string",
                    "title": "The PostalCode schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                  }
                },
                "additionalProperties": true
              }
            }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}

the Address data is still optional, but if provided, it will be validated.

user2650994
  • 136
  • 5