Skip to content

Problem with the validation in example JSON schemas #3

Description

@jboehm1911

Current Situation

The current example JSON Schema for Author is:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "21.T11969/Author Attribute Schema",
  "description": "Validates the 21.T11969/Author attribute in an FDO record.",
  "type": "array",
  "items": {
    "type": "object",
    "title": "FDO Record Item",
    "properties": {
      "key": { "type": "string" },
      "value": {}
    },
    "required": ["key", "value"]
  },
  "oneOf": [
    {
      "title": "21.T11969_Author - Option 1: Repeated individual objects (1 to 20)",
      "contains": {
        "properties": {
          "key": { "const": "21.T11969/Author" },
          "value": { "$ref": "#/$defs/21.T11969_Author_Syntax" }
        },
        "required": ["key", "value"]
      },
      "minContains": 1,
      "maxContains": 20
    },
    {
      "title": "21.T11969_Author - Option 2: Single list containing 1 to 20 objects",
      "contains": {
        "properties": {
          "key": { "const": "21.T11969/Author" },
          "value": {
            "type": "array",
            "items": { "$ref": "#/$defs/21.T11969_Author_Syntax" },
            "minItems": 1,
            "maxItems": 20
          }
        },
        "required": ["key", "value"]
      },
      "minContains": 1,
      "maxContains": 1
    }
  ],
  "$defs": {
    "21.T11969_Author_Syntax": {
      "type": "object",
      "properties": {
        "21.example/first_name": { "type": "string" },
        "21.example/last_name": { "type": "string" }
      },
      "required": ["21.example/last_name"],
      "additionalProperties": false
    }
  }
}

Problem

There is a problem for validation when repeated individual objects are used in the JSON example. Currently, the schema would validate the following example JSON:

[
  {
    "key": "21.T11969/Author",
    "value": {
      "21.example/first_name": "Jane",
      "21.example/last_name": "Doe"
    }
  },
  {
    "key": "21.T11969/Author",
    "value": {
      "21.example/first_name": "Jon",
      "21.example/last_name": 123
    }
  }
]

But it should not validate this example, because the second item should have a string for the value of 21.example/last_name.
The example wrongly validates because the contains condition is is already fulfilled by the first item. Then, the second (invalid) item would be ignored, leading to successful validation.

Suggested Fix

Combine the contains with an if-else block. The contains checks that the cardinality is correct. The if-else block checks that if the key is 21.T11969/Author, then the value must fulfill the attribute definition of author. A suggested fixed schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "21.T11969/Author Attribute Schema",
  "description": "Validates the 21.T11969/Author attribute in an FDO record.",
  "type": "array",
  "items": {
    "type": "object",
    "title": "FDO Record Item",
    "properties": {
      "key": { "type": "string" },
      "value": {}
    },
    "required": ["key", "value"]
  },
  "oneOf": [
    {
      "title": "21.T11969_Author - Option 1: Repeated individual objects (1 to 20)",
      "contains": {
        "properties": {
          "key": {
            "const": "21.T11969/Author"
          }
        }
      },
      "minContains": 1,
      "maxContains": 20,
      "items": {
        "if": {
          "properties": {
            "key": {
              "const": "21.T11969/Author"
            }
          }
        },
        "then": {
          "properties": {
            "value": {
              "$ref": "#/$defs/21.T11969~1Author"
            }
          }
        }
      }
    },
    {
      "title": "21.T11969_Author - Option 2: Single list containing 1 to 20 objects",
      "contains": {
        "properties": {
          "key": {
            "const": "21.T11969/Author"
          }
        }
      },
      "minContains": 1,
      "maxContains": 1,
      "items": {
        "if": {
          "properties": {
            "key": {
              "const": "21.T11969/Author"
            }
          }
        },
        "then": {
          "properties": {
            "value": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/21.T11969~1Author"
              },
              "minItems": 1,
              "maxItems": 20
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "21.T11969/Author": {
      "type": "object",
      "properties": {
        "21.example/first_name": { "type": "string" },
        "21.example/last_name": { "type": "string" }
      },
      "required": ["21.example/last_name"],
      "additionalProperties": false
    }
  }
}

Note: this bug applies to all example schemas in the folder -> they all may need an update.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions