1

Below is a sample JSON I need to validate -

   {
    "request": {
    "hdr": {
        "version": "1.0"
    },
    "bd": {

        "id": 1,
        "name": "test",
        "address": "test",
        "items": {
            "code": "PP"
        }

    }
}
    }

with dependency conditions as below -

1) request.bd.name field should be present only when request.hdr.version is 1.4. For any other version this is an invalid field and should not be present in JSON.

2) request.bd.address field max length should be 50 in case request.bd.items.code is PP but should be 60 in case of any other value.

I referred to jsonSchema attribute conditionally required but that seems to be deal with a simple case where all elements are in the same level.

I am not sure how to implement this in my scenario where fields in one subschema are dependent on fields in another parallel subschema.

Please help advise.

I am planning to use https://github.com/everit-org/json-schema for validation but I think the JSON schema would be more or less the same irrespective of the JSON validation implementation selected.

5
  • JSON schemas cannot be used to express constraints between fields.
    – Michael
    Commented Aug 23, 2019 at 13:32
  • That's not what the OP asked. JSON Schema cannot be used to express constraints USING values from specific fields to form a condition. Commented Aug 23, 2019 at 13:37
  • Possible duplicate of JSONSchema - Required property dependent on parent property Commented Aug 23, 2019 at 13:38
  • 1
    I think that question (and answer) will like answer your question. Essentialy, you can only ever "see down" the tree, never up, so if your conditional requirements span multiple tree levels and paths, you need to define it at thr higest common level depth. Commented Aug 23, 2019 at 13:39
  • Please don't hesitate to ask if you have follow up questions on this! Commented Aug 23, 2019 at 13:40

0