0

I tried searching, but I'm not quite sure how to put this in words! The point of confusion is how "required" works in JSON schema v4.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "prop1": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "A": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "name": {
                  enum:["abc","bcd"]
                }
              },
              "required": [
                "name"
              ]
            }
          }
        },
        "required": [
          "A"
        ]
      }
    },
    "prop2": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "field": {
            "type": "string"
          }
        },
        "required": [
          "field"
        ]
      }
    }
  },
  "required": [
    "prop1"
  ]
}

Here I want to set a rule that if prop1.name=="abc" then only prop2 is required otherwise prop2 is optional, how to do that ?

2
  • prop1 is an array, so I am not sure how you can do that. If prop1 was an object with a name property, you could use anyOf to make prop2 required if prop1.name is "abc".
    – Scottux
    Oct 17, 2017 at 13:43
  • Yes Scottux, that is the case. I need validation on array. Oct 30, 2017 at 14:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.