{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://redbook.fun/specification/1.0/schemas/component-envelope.schema.json",
  "title": "Portable RedBook component envelope",
  "type": "object",
  "properties": {
    "formatVersion": {
      "const": "1.0"
    },
    "kind": {
      "enum": [
        "blueprint",
        "theme",
        "plugin"
      ]
    },
    "id": {
      "type": "string",
      "minLength": 1
    },
    "version": {
      "type": "string",
      "minLength": 1
    },
    "title": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "resources": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "path": {
            "type": "string",
            "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\.(?:/|$))[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*(?![\\s\\S])"
          },
          "mediaType": {
            "type": "string",
            "minLength": 1
          },
          "bytes": {
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          },
          "sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}(?![\\s\\S])"
          }
        },
        "required": [
          "path",
          "mediaType",
          "bytes",
          "sha256"
        ],
        "additionalProperties": false
      }
    },
    "dependencies": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "version": {
            "type": "string",
            "minLength": 1
          }
        },
        "required": [
          "id",
          "version"
        ],
        "additionalProperties": false
      },
      "uniqueItems": true
    },
    "requires": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "graphPart": {
      "type": "string",
      "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\.(?:/|$))[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*(?![\\s\\S])"
    },
    "target": {
      "enum": [
        "reader",
        "redbook",
        "viewbox"
      ]
    },
    "entry": {
      "type": "string",
      "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\.(?:/|$))[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*(?![\\s\\S])"
    },
    "export": {
      "const": "setup"
    },
    "configuration": {
      "$ref": "https://redbook.fun/specification/1.0/schemas/value.schema.json#/$defs/map"
    },
    "configurationSchemaPart": {
      "type": "string",
      "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\.(?:/|$))[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*(?![\\s\\S])"
    },
    "profile": {
      "enum": [
        "css",
        "template"
      ]
    },
    "stylesheet": {
      "type": "string",
      "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\.(?:/|$))[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*(?![\\s\\S])"
    },
    "authoringPart": {
      "type": "string",
      "pattern": "^(?!.*(?:^|/)\\.{1,2}(?:/|$))(?!.*\\.(?:/|$))[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*(?![\\s\\S])"
    }
  },
  "required": [
    "formatVersion",
    "kind",
    "id",
    "version",
    "title",
    "resources",
    "dependencies",
    "requires"
  ],
  "additionalProperties": false,
  "oneOf": [
    {
      "properties": {
        "kind": {
          "const": "blueprint"
        }
      },
      "required": [
        "graphPart"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "target"
            ]
          },
          {
            "required": [
              "entry"
            ]
          },
          {
            "required": [
              "export"
            ]
          },
          {
            "required": [
              "configuration"
            ]
          },
          {
            "required": [
              "configurationSchemaPart"
            ]
          },
          {
            "required": [
              "profile"
            ]
          },
          {
            "required": [
              "stylesheet"
            ]
          }
        ]
      }
    },
    {
      "properties": {
        "kind": {
          "const": "theme"
        }
      },
      "required": [
        "target",
        "entry",
        "configuration"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "graphPart"
            ]
          },
          {
            "required": [
              "export"
            ]
          }
        ]
      }
    },
    {
      "properties": {
        "kind": {
          "const": "plugin"
        }
      },
      "required": [
        "target",
        "entry",
        "export",
        "configuration"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "graphPart"
            ]
          },
          {
            "required": [
              "profile"
            ]
          },
          {
            "required": [
              "stylesheet"
            ]
          }
        ]
      }
    }
  ],
  "allOf": [
    {
      "if": {
        "required": [
          "stylesheet"
        ]
      },
      "then": {
        "required": [
          "profile"
        ],
        "properties": {
          "profile": {
            "const": "template"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "plugin"
          }
        }
      },
      "then": {
        "properties": {
          "requires": {
            "contains": {
              "const": "rbx.ecmascript.1"
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "theme"
          }
        }
      },
      "then": {
        "properties": {
          "requires": {
            "contains": {
              "const": "rbx.dom.1"
            }
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "profile"
        ],
        "properties": {
          "profile": {
            "const": "template"
          }
        }
      },
      "then": {
        "properties": {
          "requires": {
            "contains": {
              "const": "rbx.templates.1"
            }
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "authoringPart"
        ]
      },
      "then": {
        "properties": {
          "kind": {
            "const": "plugin"
          },
          "target": {
            "enum": [
              "redbook",
              "viewbox"
            ]
          },
          "requires": {
            "contains": {
              "const": "rbx.participant-authoring.1"
            }
          }
        }
      }
    }
  ]
}
