Home > front end >  validate one of the object in json schema conditionally
validate one of the object in json schema conditionally

Time:12-08

In below json is valid only if type is mobile and endDate is empty/null, else validation need to fail. Here type can have any values but my validation only on mobile type.

Valid json:

{
  "contacts": [
   {
    "type": "mobile",
    "endDate": "",
    "number": "1122334455"
   },
   {
    "type": "home",
    "endDate": "",
    "number": "1111122222"
   },
   {
    "type": "mobile",
    "endDate": "12-Jan-2017",
    "number": "1234567890"
   },
  ]
}

Invalid json: (since contacts don't have a valid mobile number)

{
  "contacts": [
   {
    "type": "mobile",
    "endDate": "12-Jan-2021",
    "number": "1122334455"
   },
   {
    "type": "home",
    "endDate": "",
    "number": "1111122222"
   },
   {
    "type": "mobile",
    "endDate": "12-Jan-2017",
    "number": "1234567890"
   },
  ]
}

Schema which i tried

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "properties": {
    "contacts": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string"
          },
          "endDate": {
            "type": [
              "string",
              "null"
            ]
          },
          "number": {
            "type": "string"
          }
        },
        "anyOf": [
          {
            "if": {
              "properties": {
                "type": {
                  "const": "mobile"
                }
              }
            },
            "then": {
              "properties": {
                "endDate": {
                  "maxLength": 0
                }
              }
            }
          }
        ]
      }
    }
  }
}

could anyone provide me a right schema for the above json, attaching the example code here, this is valid json but getting error. Invalid json example is here why because type mobile don't have empty endDate. Thanks in advance.

CodePudding user response:

I found the answer some how, we need not to use anyOf or oneOf. The right one is contains. place of the contains also a mater. working example is here

Here is correct schema

{
  "type": "object",
  "properties": {
    "contacts": {
      "type": "array",
      "minItems": 1,
      "contains": {
        "type": "object",
        "properties": {
          "type": {
            "const": "mobile"
          },
          "endDate": {
            "type" : ["string", "null"],
            "maxLength": 0
          }
        },
        "required": ["type"]
      },
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string"
          },
          "endDate": {
            "type": [
              "string",
              "null"
            ]
          },
          "number": {
            "type": "string"
          }
        }
      }
    }
  }
}

CodePudding user response:

Your schema defines properties > data but your instance data uses contacts rather than data. Changing either fixes your problem. You have otherwise done this correctly.

(If you only have one type to check, you do not need to wrap your if/then schema in an anyOf.)

  • Related