clove682
clove682

Reputation: 94

JSON Schema: How to Check array A contains at least all value of array B

I expect array A to contain at least all the elements of attribute array B.
Here are some examples:

// valid
{
"A": [1,2,3],
"B": [1,2]
}

// valid
{
"A": [1,2],
"B": [1,2]
}

// invalid
{
"A": [1,2],
"B": [1,2,3]
}

There are no specific requirements regarding the draft of JSON Schema

I found this answer, and it was similar to my need: Is it possible in json schema to define a constraint between two properties

I tried using:

{
...
"propertires": {
  "A": {
    "minContains": {"$data": "1/B"}
  },
  "B": {
    ...
  }
}
}

result: invalid test cases can also pass

Upvotes: 1

Views: 49

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

It should be noted that $data is not a JSON Schema keyword. Some implementations have chosen to support some kind of functionality for it, but that functionality is not specified officially and will vary across implementations, if it's supported at all.

The primary reason it has not been adopted is that it forms invalid schemas. In your example, "minContains": {"$data": "1/B"}, minContains requires a number, but you've given it an object.

I have created an alternative data keyword in a custom vocab that you're welcome to look at, but so far it only has support in .Net.

In the end, JSON Schema doesn't have a way to express the kinds of references you want.

Upvotes: 0

Related Questions