jsonsystems / json-schema

JSONSchema.Net Public Repository
Apache License 2.0
663 stars 64 forks source link

Invalid parsing of an array of objects #59

Closed akopchinskiy closed 4 years ago

akopchinskiy commented 5 years ago

Example. This:

{
  "objects": [
    {
      "type": "type",
      "node": {
        "_id": 1,
        "class": "company"
      }
    },
    {
      "type": "type",
      "node": {
        "_id": 2,
        "class": "company",
        "name": "Name"
      }
    }
  ]
}

Results in this:

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "The Root Schema",
  "required": [
    "objects"
  ],
  "properties": {
    "objects": {
      "$id": "#/properties/objects",
      "type": "array",
      "title": "The Objects Schema",
      "items": {
        "$id": "#/properties/objects/items",
        "type": "object",
        "title": "The Items Schema",
        "required": [
          "type",
          "node"
        ],
        "properties": {
          "type": {
            "$id": "#/properties/objects/items/properties/type",
            "type": "string",
            "title": "The Type Schema",
            "default": "",
            "examples": [
              "type"
            ],
            "pattern": "^(.*)$"
          },
          "node": {
            "$id": "#/properties/objects/items/properties/node",
            "type": "object",
            "title": "The Node Schema",
            "required": [
              "_id",
              "class"
            ],
            "properties": {
              "_id": {
                "$id": "#/properties/objects/items/properties/node/properties/_id",
                "type": "integer",
                "title": "The _id Schema",
                "default": 0,
                "examples": [
                  1
                ]
              },
              "class": {
                "$id": "#/properties/objects/items/properties/node/properties/class",
                "type": "string",
                "title": "The Class Schema",
                "default": "",
                "examples": [
                  "thread"
                ],
                "pattern": "^(.*)$"
              }
            }
          }
        }
      }
    }
  }
}

Which is invalid. node.name property does not exist in the generated schema. It should exists but be optional.

jackwootton commented 5 years ago

This depends on the type of validation chosen for the array, specifically how the items keyword is used. See https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4.1

The value of "items" MUST be either a valid JSON Schema or an array of valid JSON Schemas.

This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.

If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema.

If "items" is an array of schemas, validation succeeds if each element of the instance validates against the schema at the same position, if any.

Use the hamburger menu in the top-left corner to change array validation to "tuple" and items will become an array that includes all objects:

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "The Root Schema",
  "required": [
    "objects"
  ],
  "properties": {
    "objects": {
      "$id": "#/properties/objects",
      "type": "array",
      "title": "The Objects Schema",
      "items": [
        {
          "$id": "#/properties/objects/items/0",
          "type": "object",
          "title": "The 0 Schema",
          "required": [
            "type",
            "node"
          ],
          "properties": {
            "type": {
              "$id": "#/properties/objects/items/0/properties/type",
              "type": "string",
              "title": "The Type Schema",
              "default": "",
              "examples": [
                "type"
              ],
              "pattern": "^(.*)$"
            },
            "node": {
              "$id": "#/properties/objects/items/0/properties/node",
              "type": "object",
              "title": "The Node Schema",
              "required": [
                "_id",
                "class"
              ],
              "properties": {
                "_id": {
                  "$id": "#/properties/objects/items/0/properties/node/properties/_id",
                  "type": "integer",
                  "title": "The _id Schema",
                  "default": 0,
                  "examples": [
                    1
                  ]
                },
                "class": {
                  "$id": "#/properties/objects/items/0/properties/node/properties/class",
                  "type": "string",
                  "title": "The Class Schema",
                  "default": "",
                  "examples": [
                    "company"
                  ],
                  "pattern": "^(.*)$"
                }
              }
            }
          }
        },
        {
          "$id": "#/properties/objects/items/1",
          "type": "object",
          "title": "The 1 Schema",
          "required": [
            "type",
            "node"
          ],
          "properties": {
            "type": {
              "$id": "#/properties/objects/items/1/properties/type",
              "type": "string",
              "title": "The Type Schema",
              "default": "",
              "examples": [
                "type"
              ],
              "pattern": "^(.*)$"
            },
            "node": {
              "$id": "#/properties/objects/items/1/properties/node",
              "type": "object",
              "title": "The Node Schema",
              "required": [
                "_id",
                "class",
                "name"
              ],
              "properties": {
                "_id": {
                  "$id": "#/properties/objects/items/1/properties/node/properties/_id",
                  "type": "integer",
                  "title": "The _id Schema",
                  "default": 0,
                  "examples": [
                    2
                  ]
                },
                "class": {
                  "$id": "#/properties/objects/items/1/properties/node/properties/class",
                  "type": "string",
                  "title": "The Class Schema",
                  "default": "",
                  "examples": [
                    "company"
                  ],
                  "pattern": "^(.*)$"
                },
                "name": {
                  "$id": "#/properties/objects/items/1/properties/node/properties/name",
                  "type": "string",
                  "title": "The Name Schema",
                  "default": "",
                  "examples": [
                    "Name"
                  ],
                  "pattern": "^(.*)$"
                }
              }
            }
          }
        }
      ]
    }
  }
}
jackwootton commented 4 years ago

Closing as this is fixed on www.jsonschema.net