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.

Jacob
  • 426
  • 3
  • 19
  • JSON schemas cannot be used to express constraints between fields. – Michael Aug 23 '19 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. – Relequestual Aug 23 '19 at 13:37
  • Possible duplicate of [JSONSchema - Required property dependent on parent property](https://stackoverflow.com/questions/57141416/jsonschema-required-property-dependent-on-parent-property) – Relequestual Aug 23 '19 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. – Relequestual Aug 23 '19 at 13:39
  • Please don't hesitate to ask if you have follow up questions on this! – Relequestual Aug 23 '19 at 13:40

0 Answers0