Home > Back-end >  Json Schema: Require a property only when a specific property is present
Json Schema: Require a property only when a specific property is present

Time:08-24

good day everyone, I am new here,

I have a json response looking like this

{
  "Number": "20.09.00001",
  "Supplier": {
    "Name": "John Doe",
    "Title": "Mr.",
    "FirstName": "John",
    "LastName": "Doe",
    "Phone": "0212341234",
    "Email": "[email protected]",
    "Code": "Foo123",
    "Gender": "Male"
  }
}

I want to make inside the supplier properties required so I make a JSON schema validation looking like this

{
    "type": "object",
    "required": [
        "Supplier"
    ],
    "properties": {
        "Supplier": {
            "type": "object",
            "required": [
                "Name",
                "Title",
                "FirstName",
                "LastName",
                "Phone",
                "Email",
                "Code",
                "Gender"
            ],
            "properties": {
                "Name": {
                    "type": "string"
                },
                "Title": {
                    "type": "string"
                },
                "FirstName": {
                    "type": "string"
                },
                "LastName": {
                    "type": "string"
                },
                "Phone": {
                    "type": "string"
                },
                "Email": {
                    "type": "string"
                },
                "Code": {
                    "type": "string"
                },
                "Gender": {
                    "type": "string"
                }
            }
        }
    }
}

but the problem is, the inside of supplier properties are not always present, when it's not supplied, it will only return empty objects like this

{
  "Number": "20.09.00001",
  "Supplier": {}
}

how can I validate only IF the inside supplier returns full property object, and ignore if the supplier returns an empty object?

I have tried using if else and anyOff but resulting in no luck.

my code with if else that did not work:

"Supplier": {
    "type": ["object"],
    "if": {
        "properties": {
            "Name": {
                "type": ["string", "null"]
            },
            "Title": {
                "type": ["string", "null"]
            },
            "FirstName": {
                "type": ["string", "null"]
            },
            "LastName": {
                "type": ["string", "null"]
            },
            "Phone": {
                "type": ["string", "null"]
            },
            "Email": {
                "type": ["string", "null"]
            },
            "Code": {
                "type": ["string", "null"]
            },
            "Gender": {
                "type": ["string", "null"]
            }
        }
    },
    "then": {
        "required": [
            "Name",
            "Title",
            "FirstName",
            "LastName",
            "Phone",
            "Email",
            "Code",
            "Gender"
        ]
    },
    "else": {
        "required": []
    }
}

CodePudding user response:

I think anyOf is the best approach in this case. The object is either empty or all the properties are required. There are a couple of way to assert that an object is empty. You could use "maxProperties": 0 or "const": {}.

{
  "type": "object",
  "properties": {
    ... all your properties ...
  },

  "anyOf": [
    { "const": {} },
    { "required": [ ... all required properties ... ] }
  ]
}
  • Related