skip to Main Content

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:

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

result: invalid test cases can also pass

2

Answers


  1. Well the thing is that minContains is for counting occurrences. Try schema below.

    {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "type": "object",
          "required": ["A", "B"],
          "properties": {
            "A": {
              "type": "array",
              "items": {
                "type": "number"
              }
            },
            "B": {
              "type": "array",
              "items": {
                "type": "number"
              }
            }
          },
          "allOf": [
            {
              "$ref": "#/definitions/arrayContainment"
            }
          ],
          "definitions": {
            "arrayContainment": {
              "if": {
                "properties": {
                  "B": {
                    "minItems": 1
                  }
                }
              },
              "then": {
                "allOf": [
                  {
                    "$comment": "For each item in B, it must exist in A",
                    "allOf": [
                      {
                        "$data": "/B/*/",
                        "propertyNames": {
                          "enum": { "$data": "/A" }
                        }
                      }
                    ]
                  }
                ]
              }
            }
          }
        }
    

    TEST CASES

        // valid
    {
    "A": [1,2,3],
    "B": [1,2]
    }
    
    // valid
    {
    "A": [1,2],
    "B": [1,2]
    }
    
    // invalid
    {
    "A": [1,2],
    "B": [1,2,3]
    }
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search