{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.ishare.eu/v3/datarights/schema.json",
  "title": "Verifiable Credential: iSHARE Data Rights",
  "description": "Verifiable Credential envelope for an iSHARE Data Rights credential. VC 2.0 fields at the top level; domain-specific claims under credentialSubject. This schema describes the unsigned VC envelope (without proof).",
  "type": "object",
  "properties": {
    "@context": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": [
        { "const": "https://www.w3.org/ns/credentials/v2" },
        { "const": "https://schemas.ishare.eu/v3/datarights/context.jsonld" }
      ]
    },
    "type": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": [
        { "const": "VerifiableCredential" },
        { "const": "DataRightsCredential" }
      ]
    },
    "id": { "type": "string"},
    "issuer": {
      "oneOf": [
        { "type": "string"},
        {
          "type": "object",
          "properties": { "id": { "type": "string"} },
          "required": ["id"],
          "additionalProperties": true
        }
      ]
    },
    "validFrom": { "type": "string", "format": "date-time" },
    "validUntil": { "type": "string", "format": "date-time" },
    "credentialStatus": {
      "$ref": "https://schemas.ishare.eu/v3/common/credential-status.json#/$defs/CredentialStatus",
      "description": "Optional credential status for checking revocation, suspension, or refresh status using bitstring status lists (BitstringStatusListEntry) as defined in W3C Bitstring Status List v1.0"
    },
    "credentialSubject": { "$ref": "#/$defs/DataRightsSubject" }
  },
  "required": ["@context", "type", "issuer", "credentialSubject"],
  "additionalProperties": false,
  "$defs": {
    "DataRightsSubject": {
      "type": "object",
      "properties": {
        "datarightsEvidence": {
          "type": "object",
          "properties": {
            "policySets": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "maxDelegationDepth": { "type": "integer" },
                  "target": {
                    "type": "object",
                    "properties": {
                      "environment": {
                        "type": "object",
                        "properties": {
                          "licenses": { "$ref": "#/$defs/LicenseRuleList" }
                        },
                        "required": ["licenses"]
                      }
                    },
                    "required": ["environment"]
                  },
                  "policies": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "target": {
                          "type": "object",
                          "properties": {
                            "resource": {
                              "type": "object",
                              "properties": {
                                "type": { "type": "string" },
                                "identifiers": { "type": "array", "items": { "type": "string" } },
                                "attributes": { "type": "array", "items": { "type": "string" } }
                              },
                              "required": ["type"]
                            },
                            "actions": { "type": "array", "items": { "type": "string" } }
                          },
                          "required": ["resource", "actions"]
                        },
                        "rules": {
                          "type": "array",
                          "items": {
                            "type": "object",
                            "properties": {
                              "effect": { "type": "string", "enum": ["Permit", "Deny"] },
                              "conditions": {
                                "type": "object",
                                "properties": {
                                  "allOf": {
                                    "type": "array",
                                    "items": {
                                      "type": "object",
                                      "properties": {
                                        "leftOperand": { "type": "string" },
                                        "operator": { "type": "string" },
                                        "rightOperand": { "type": "string" }
                                      },
                                      "required": ["leftOperand", "operator", "rightOperand"]
                                    }
                                  }
                                },
                                "required": ["allOf"]
                              }
                            },
                            "required": ["effect"]
                          }
                        }
                      },
                      "required": ["target", "rules"]
                    }
                  }
                },
                "required": ["target", "policies"]
              }
            }
          },
          "required": ["policySets"]
        }
      },
      "required": ["datarightsEvidence"],
      "additionalProperties": true
    },
    "LicenseRuleList": {
      "type": "array",
      "description": "Root list of license expressions",
      "items": { "$ref": "#/$defs/LicenseRule" }
    },
    "LicenseRule": {
      "description": "A license identifier or a logical group (allOf / anyOf)",
      "oneOf": [
        { "$ref": "#/$defs/LicenseIdentifier" },
        { "$ref": "#/$defs/LicenseAllOf" },
        { "$ref": "#/$defs/LicenseAnyOf" }
      ]
    },
    "LicenseIdentifier": {
      "type": "string",
      "description": "License identifier",
      "example": "https://licenses.ishare.eu/example"
    },
    "LicenseAllOf": {
      "type": "object",
      "required": ["allOf"],
      "properties": {
        "allOf": {
          "type": "array",
          "items": { "$ref": "#/$defs/LicenseRule" },
          "minItems": 1
        }
      },
      "additionalProperties": false
    },
    "LicenseAnyOf": {
      "type": "object",
      "required": ["anyOf"],
      "properties": {
        "anyOf": {
          "type": "array",
          "items": { "$ref": "#/$defs/LicenseRule" },
          "minItems": 1
        }
      },
      "additionalProperties": false
    }
  }
}
  