{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.baijimu.com/contracts/bundle-definition/23.0.0/schema.json",
  "title": "Baijimu BundleDefinition",
  "description": "Database-owned editable Bundle definition and immutable published content use the same structure.",
  "type": "object",
  "required": [
    "schemaVersion",
    "resources"
  ],
  "properties": {
    "schemaVersion": {
      "const": "23.0.0"
    },
    "resources": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/resourceSelection"
      },
      "default": []
    },
    "propertyDefinition": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/propertyDefinition"
      },
      "default": []
    },
    "configuration": {
      "$ref": "#/$defs/configuration"
    }
  },
  "$defs": {
    "exactSemanticVersion": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(?:\\.(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$"
    },
    "resourceSelection": {
      "type": "object",
      "required": [
        "resourceType",
        "resourceKey",
        "semanticVersion"
      ],
      "properties": {
        "resourceType": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_]{0,31}$",
          "not": {
            "enum": [
              "ISSUE_DEFINITION",
              "ROLE_TEMPLATE_SET",
              "PERMISSION_DEFINITION_SET"
            ]
          }
        },
        "resourceKey": {
          "type": "string",
          "minLength": 1,
          "pattern": "^[^/\\s]+$"
        },
        "semanticVersion": {
          "$ref": "#/$defs/exactSemanticVersion"
        }
      },
      "not": {
        "anyOf": [
          {
            "required": [
              "resourceLocator"
            ],
            "properties": {
              "resourceLocator": {}
            }
          },
          {
            "required": [
              "environmentKey"
            ],
            "properties": {
              "environmentKey": {}
            }
          },
          {
            "required": [
              "bundleId"
            ],
            "properties": {
              "bundleId": {}
            }
          },
          {
            "required": [
              "templateKey"
            ],
            "properties": {
              "templateKey": {}
            }
          }
        ]
      }
    },
    "propertyDefinition": {
      "type": "object",
      "required": [
        "name",
        "type"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "$ref": "#/$defs/propertyType"
        },
        "description": {
          "type": [
            "string",
            "null"
          ]
        },
        "required": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "isSensitive": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "autoBinding": {
          "type": "boolean",
          "default": false
        },
        "isExternalManaged": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "readOnly": {
          "type": [
            "boolean",
            "null"
          ]
        },
        "defaultValue": {}
      },
      "allOf": [
        {
          "if": {
            "required": [
              "autoBinding"
            ],
            "properties": {
              "autoBinding": {
                "const": true
              }
            },
            "type": "object"
          },
          "then": {
            "properties": {
              "type": {
                "properties": {
                  "@type": {
                    "enum": [
                      "Interface",
                      "RefType",
                      "InterfaceMap"
                    ]
                  }
                },
                "type": "object"
              },
              "isExternalManaged": {
                "not": {
                  "const": true
                }
              }
            },
            "type": "object"
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "properties": {
                  "@type": {
                    "not": {
                      "const": "JsonSchema"
                    }
                  }
                },
                "type": "object"
              }
            },
            "type": "object"
          },
          "then": {
            "not": {
              "required": [
                "defaultValue"
              ],
              "type": "object",
              "properties": {
                "defaultValue": {}
              }
            }
          }
        }
      ]
    },
    "propertyType": {
      "type": "object",
      "required": [
        "@type"
      ],
      "properties": {
        "@type": {
          "enum": [
            "JsonSchema",
            "Interface",
            "RefType",
            "InterfaceMap",
            "InterfaceList",
            "MethodDefinition"
          ]
        }
      }
    },
    "configuration": {
      "type": "object",
      "properties": {
        "resourceBindings": {
          "type": "array",
          "default": [],
          "items": {
            "type": "object",
            "required": [
              "resourceType",
              "resourceKey",
              "fields"
            ],
            "properties": {
              "resourceType": {
                "type": "string",
                "minLength": 1
              },
              "resourceKey": {
                "type": "string",
                "minLength": 1
              },
              "fields": {
                "type": "object",
                "additionalProperties": {
                  "$ref": "#/$defs/configurationValueSource"
                }
              }
            }
          }
        }
      }
    },
    "configurationValueSource": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "@type",
            "value"
          ],
          "properties": {
            "@type": {
              "const": "Data"
            },
            "value": {}
          }
        },
        {
          "type": "object",
          "required": [
            "@type",
            "expression"
          ],
          "properties": {
            "@type": {
              "const": "Expression"
            },
            "expression": {
              "type": "string",
              "minLength": 1
            }
          }
        },
        {
          "type": "object",
          "required": [
            "@type",
            "property"
          ],
          "properties": {
            "@type": {
              "const": "Property"
            },
            "property": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false
}
