{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://product-information-graph.org/schema/2026-05-08/jsonld/aPackage.json",
  "title": "CASCaRA aPackage Schema for JSON-LD",
  "description": "Validates a complete JSON-LD document (CASCaRA aPackage) with @context (namespace and vocabulary definitions) as well as @graph (array of PIG items). The graph contains a mix of item classes (Property, Link, Entity, Relationship) and instances (anEntity, aRelationship). Package-level metadata includes optional dcterms:modified, dcterms:creator, dcterms:title, and dcterms:description. Validates each item against its corresponding schema based on cas:itemType.",
  "type": "object",
  "properties": {
    "@context": {
      "description": "JSON-LD context defining namespaces and vocabulary mappings",
      "oneOf": [
        {
          "type": "object",
          "description": "Context as object with namespace mappings"
        },
        {
          "type": "array",
          "description": "Context as array of contexts",
          "items": {
            "oneOf": [
              { "type": "object" },
              { "type": "string" }
            ]
          }
        },
        {
          "type": "string",
          "description": "Context as URL reference"
        }
      ]
    },
    "@id": {
      "type": "string",
      "pattern": "^(?:[A-Za-z0-9_\\-]+:[^:\\s]+|https?:\\/\\/[^\\s]+)$",
      "description": "Optional package identifier"
    },
    "@type": {
      "$ref": "#/$defs/idString",
      "description": "Reference to the Package class this instance belongs to"
    },
    "cas:itemType": {
      "type": "object",
      "required": [ "@id" ],
      "properties": {
        "@id": {
          "type": "string",
          "enum": [ "cas:aPackage" ],
          "description": "The CASCaRA metamodel item type for cas:aPackage"
        }
      },
      "additionalProperties": false
    },
    "cas:revision": {
      "type": "string",
      "description": "Revision identifier for version control"
    },
    "cas:priorRevision": {
      "type": "array",
      "description": "Array of prior revision identifiers",
      "maxItems": 2,
      "items": {
        "type": "string"
      }
    },
    "dcterms:modified": {
      "type": "string",
      "format": "date-time",
      "description": "Optional package modification timestamp"
    },
    "dcterms:creator": {
      "type": "string",
      "description": "Optional package creator"
    },
    "dcterms:title": {
      "type": "array",
      "description": "Optional package title",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/languageValue"
      }
    },
    "dcterms:description": {
      "type": "array",
      "description": "Optional package description",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/languageValue"
      }
    },
    "@graph": {
      "type": "array",
      "minItems": 0,
      "description": "Array of PIG items (classes and instances)",
      "items": {
        "type": "object",
        "required": [ "cas:itemType" ],
        "properties": {
          "cas:itemType": {
            "type": "object",
            "required": [ "@id" ],
            "properties": {
              "@id": {
                "type": "string",
                "enum": [
                  "cas:Property",
                  "cas:Link",
                  "cas:Entity",
                  "cas:Relationship",
                  "cas:Enumeration",
                  "cas:anEntity",
                  "cas:aRelationship"
                ]
              }
            }
          }
        },
        "allOf": [
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:anEntity" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/anEntity.json"
            }
          },
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:aRelationship" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/aRelationship.json"
            }
          },
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:Property" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/Property.json"
            }
          },
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:Link" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/Link.json"
            }
          },
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:Entity" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/Entity.json"
            }
          },
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:Relationship" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/Relationship.json"
            }
          },
          {
            "if": {
              "type": "object",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": { "const": "cas:Enumeration" }
                  }
                }
              }
            },
            "then": {
              "$ref": "http://product-information-graph.org/schema/2026-05-08/jsonld/Enumeration.json"
            }
          }
        ]
      }
    }
  },
  "patternProperties": {
    "^(?!cas:itemType|cas:revision|cas:priorRevision|@id|@type|dcterms:modified|dcterms:creator|dcterms:title|dcterms:description)([A-Za-z_][A-Za-z0-9_-]*:[^:\\s]+|https?://[^\\s]+)$": {
        "type": "array",
        "description": "Configurable properties and links with namespaced keys",
        "items": {
          "type": "object",
          "properties": {
            "cas:itemType": {
              "type": "object",
              "required": [ "@id" ],
              "properties": {
                "@id": {
                  "type": "string",
                  "enum": [ "cas:aProperty", "cas:aTargetLink" ],
                  "description": "Type of configurable item: property value or target link"
                }
              },
              "additionalProperties": false
            },
            "@value": {
              "type": "string",
              "description": "Direct property value (for aProperty)"
            },
            "@id": {
              "$ref": "#/$defs/idString",
              "description": "Reference to enumeration value or linked entity/relationship"
            }
          },
          "required": [ "cas:itemType" ],
          "oneOf": [
            {
              "type": "object",
              "description": "aProperty with direct value",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": {
                      "const": "cas:aProperty"
                    }
                  }
                }
              },
              "required": [ "@value" ],
              "not": {
                "required": [ "@id" ]
              }
            },
            {
              "type": "object",
              "description": "aTargetLink to an enumeration, entity or relationship",
              "properties": {
                "cas:itemType": {
                  "type": "object",
                  "properties": {
                    "@id": {
                      "const": "cas:aTargetLink"
                    }
                  }
                }
              },
              "required": [ "@id" ],
              "not": {
                "required": [ "@value" ]
              }
            }
          ],
          "additionalProperties": false
        }
      }
    },
    "required": [
      "@context",
      "@id",
      "@type",
      "cas:itemType",
      "@graph",
      "dcterms:modified"
    ],
    "additionalProperties": true,
    "$defs": {
      "idString": {
        "type": "string",
        "pattern": "^(?:[A-Za-z_][A-Za-z0-9_-]*:[^:\\s]+|https?://[^\\s]+)$",
        "description": "Valid identifier format: namespace:name or full URI (e.g., 'cas:Property', 'http://example.org/id')"
      },
      "languageValue": {
        "type": "object",
        "required": [ "@value" ],
        "properties": {
          "@value": {
            "type": "string",
            "minLength": 1,
            "description": "The actual text content"
          },
          "@language": {
            "type": "string",
            "pattern": "^[a-z]{2}(-[A-Z]{2})?$",
            "description": "IETF language tag (e.g., 'en', 'de', 'en-US')"
          }
        },
        "additionalProperties": false,
        "description": "Multi-language text value with optional language tag"
      }
    }
  }
