JSON Schema to validate interdependent array structure

tscherg

I am trying to write a schema validating arrays with the following structural constraints:

  • it can only ever contain the values 1,2,3,4,5
  • if the array contains a 1, that must be the only entry
  • the array can only contain either 2, 3 or 4 at the same time, so e.g. [2,3] is not allowed
  • 5 can be present together with 2,3,4

so the valid arrays are

[1],
[2],
[3],
[4],
[5],
[2,5],
[3,5],
[4,5]

I started writing a schema as follows:

{
    "type": "array",
    "oneOf": [
        { "items": { "enum": [1] } },
        {
            "anyOf": [
                ???
            ]
        }
    ]
}

I can't get the ??? part to work. Is it possible at all? NOTE: I would like to avoid hardcoding all possible arrays, as I have to validate more complex structures - this is only an example. Also, the optimum is a solution using only anyOf, allOf, oneOf, not, avoiding keywords like minItems

Jason Desrosiers

This passes all of your constraints.

{
  "type": "array",
  "anyOf": [
    { "enum": [[1]] },
    {
      "items": { "enum": [2, 3, 4, 5] },
      "oneOf": [
        { "$ref": "#/definitions/doesnt-contain-2-3-or-4" },
        { "$ref": "#/definitions/contains-2" },
        { "$ref": "#/definitions/contains-3" },
        { "$ref": "#/definitions/contains-4" }
      ]
    }
  ],
  "definitions": {
    "doesnt-contain-2-3-or-4": {
      "items": { "not": { "enum": [2, 3, 4] } }
    },
    "contains-2": {
      "not": {
        "items": { "not": { "enum": [2] } }
      }
    },
    "contains-3": {
      "not": {
        "items": { "not": { "enum": [3] } }
      }
    },
    "contains-4": {
      "not": {
        "items": { "not": { "enum": [4] } }
      }
    }
  }
}

If you have the option of using the new draft-06 keywords contains and const, it's actually a pretty clean solution. There is a little duplication, but I don't think that can be helped.

{
  "type": "array",
  "anyOf": [
    { "const": [1] },
    {
      "items": { "enum": [2, 3, 4, 5] },
      "oneOf": [
        { "not": { "contains": { "enum": [2 ,3, 4] } } },
        { "contains": { "const": 2 } },
        { "contains": { "const": 3 } },
        { "contains": { "const": 4 } }
      ]
    }
  ]
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSON Schema validation: validate array of objects

From Dev

JSON Schema validation: validate array of objects

From Dev

Json schema validate property name in array

From Dev

Json schema: is it possible to validate a string is a JSON array or object?

From Dev

json schema to validate array of objects with anyOf and oneOf requirements

From Dev

json schema to validate array of objects with anyOf and oneOf requirements

From Dev

JavaScript: JSON schema to validate another JSON schema

From Dev

JavaScript: JSON schema to validate another JSON schema

From Dev

validate json against schema in javascript

From Dev

How to validate JSON schema in java?

From Dev

Validate JSON against JSON Schema C#

From Dev

How to validate string and number using json schema

From Dev

How to always validate an angular json schema form?

From Dev

How to validate JSON Schema in Objective-C?

From Dev

JSON Schema: validate a number-or-null value

From Dev

JSON Schema: Validate that exactly one property is present

From Dev

Validate JSON Schema for list of known values

From Dev

Validate JSON against XML Schema (XSD)

From Dev

Validate one attribute of an object using json schema

From Dev

Validate Json Schema Draft V4

From Dev

JSON Schema: Validate that exactly one property is present

From Dev

Validate property against another with JSON Schema

From Dev

Why doesn't this JSON validate against this schema?

From Dev

Validate JSON Schema for list of known values

From Dev

JSON Schema for Array of tuples

From Dev

Validate JSON schema compliance with Jackson against an external schema file

From Dev

Parse JSON Array where each member has different schema but same general structure

From Dev

How to validate a specific json structure in Jackson?

From Dev

Validate if JSON has a determined structure on PHP

Related Related

  1. 1

    JSON Schema validation: validate array of objects

  2. 2

    JSON Schema validation: validate array of objects

  3. 3

    Json schema validate property name in array

  4. 4

    Json schema: is it possible to validate a string is a JSON array or object?

  5. 5

    json schema to validate array of objects with anyOf and oneOf requirements

  6. 6

    json schema to validate array of objects with anyOf and oneOf requirements

  7. 7

    JavaScript: JSON schema to validate another JSON schema

  8. 8

    JavaScript: JSON schema to validate another JSON schema

  9. 9

    validate json against schema in javascript

  10. 10

    How to validate JSON schema in java?

  11. 11

    Validate JSON against JSON Schema C#

  12. 12

    How to validate string and number using json schema

  13. 13

    How to always validate an angular json schema form?

  14. 14

    How to validate JSON Schema in Objective-C?

  15. 15

    JSON Schema: validate a number-or-null value

  16. 16

    JSON Schema: Validate that exactly one property is present

  17. 17

    Validate JSON Schema for list of known values

  18. 18

    Validate JSON against XML Schema (XSD)

  19. 19

    Validate one attribute of an object using json schema

  20. 20

    Validate Json Schema Draft V4

  21. 21

    JSON Schema: Validate that exactly one property is present

  22. 22

    Validate property against another with JSON Schema

  23. 23

    Why doesn't this JSON validate against this schema?

  24. 24

    Validate JSON Schema for list of known values

  25. 25

    JSON Schema for Array of tuples

  26. 26

    Validate JSON schema compliance with Jackson against an external schema file

  27. 27

    Parse JSON Array where each member has different schema but same general structure

  28. 28

    How to validate a specific json structure in Jackson?

  29. 29

    Validate if JSON has a determined structure on PHP

HotTag

Archive