0

I would like to validate collection+json objects with schema that have different formats under the same array. For example:

{
    "href": "https://example.com/whatnot",
    "data": [
        {
            "name": "foo",
            "value": "xyz:123:456"
        },
        {
            "name": "bar",
            "value": "8K"
        },
        {
            "name": "baz",
            "value": false
        }
    ]
}

Here, the value is one of exactly pattern (\w+:\d+:\d+), one of exactly ([\w\d]+), and one of exactly boolean. There are no other variations.

Is there any way in json schema to have this list checked against these requirements?

Squrppi
  • 95
  • 1
  • 7
  • In your `data` array, it seems like you actually have different types of data. Do you control the schema? I ask because this could be accomplished pretty quickly if you are able to include a "type" property on the `data' objects. – AJ X. Aug 23 '18 at 13:04
  • I cannot change the way data is presented. – Squrppi Aug 23 '18 at 13:09

1 Answers1

0

I slept overnight and figured how to make the oneOf schema. I tried to use it inside "properties", but it turned out that cannot be done. For the perfect solution, I guess I'd need "explicitOf" kind of method. But, this is good enough for now.

{
    "type": "object",
    "required": [
        "name",
        "value"
    ],
    "oneOf": [
        {
            "properties":
            {
                "name":
                {
                    "type": "string",
                    "pattern": "foo"
                },
                "value":
                {
                    "type": "string",
                    "pattern": "^(\\w+:\\d+:\\d+)$"
                }
            }
        },
        {
            "properties":
            {
                "name":
                {
                    "type": "string",
                    "pattern": "bar"
                },
                "value":
                {
                    "type": "string",
                    "pattern": "^([\\w\\d]+)$"
                }
            }
        },
        {
            "properties":
            {
                "name":
                {
                    "type": "string",
                    "pattern": "baz"
                },
                "value":
                {
                    "type": "boolean"
                }
            }
        }
    ]
}
Squrppi
  • 95
  • 1
  • 7