skip to Main Content

Im using json schema draft-4 and I am trying to conditionally add a requirement for a required label. For any value in env labels 1/2/3 are required but if the value is sit or uat then another label, date must also be required.

    "date": {
      "type": [
        "string"
      ]
    }
  },
  "anyOf": [
    {
      "not": {
        "properties": { "env": { "enum": ["sit", "uat"] } },
        "required": ["label1","label2","label3]
        }
    },
    { "required": ["date"] }
  ],

Ive based what I have on Implications documented here: https://json-schema.org/understanding-json-schema/reference/conditionals

2

Answers


  1. This will satisfy your requirements.

    if date is only allowed with sit and uat then you can modify this slightly to constrain date from the required array in the second oneOf schema.

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "required": [
            "env",
            "label1",
            "label2",
            "label3"
        ],
        "type": "object",
        "properties": {
            "env": {
                "type": "string"
            },
            "label1": {
                "type": "string"
            },
            "label2": {
                "type": "string"
            },
            "label3": {
                "type": "string"
            }
        },
        "oneOf": [
            {
                "required": [
                    "date",
                    "env"
                ],
                "type": "object",
                "properties": {
                    "env": {
                        "type": "string",
                        "enum": [
                            "sit",
                            "uat"
                        ]
                    },
                    "date": {
                        "type": "string"
                    }
                }
            },
            {
                "not": {  <-- optional depending on your `date` requirement
                    "required": [
                        "date"
                    ]
                },
                "type": "object",
                "properties": {
                    "env": {
                        "type": "string",
                        "enum": [
                            "dev",
                            "prod"
                        ]
                    }
                }
            }
        ]
    }
    
    Login or Signup to reply.
  2. An alternative way to do this would be to redefine the entire schema in a oneOf

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "oneOf": [
            {
                "required": [
                    "date",
                    "env",
                    "label1",
                    "label2",
                    "label3"
                ],
                "properties": {
                    "date": {
                        "type": "string"
                    },
                    "env": {
                        "enum": [
                            "sit",
                            "uat"
                        ]
                    },
                    "label1": {
                        "type": "string"
                    },
                    "label2": {
                        "type": "string"
                    },
                    "label3": {
                        "type": "string"
                    }
                }
            },
            {
                "required": [
                    "env",
                    "label1",
                    "label2",
                    "label3"
                ],
                "properties": {
                    "env": {
                        "enum": [
                            "prod",
                            "dev"
                        ]
                    },
                    "label1": {
                        "type": "string"
                    },
                    "label2": {
                        "type": "string"
                    },
                    "label3": {
                        "type": "string"
                    }
                }
            }
        ]
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search