{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.baijimu.com/contracts/timer-definition/2.0.0/schema.json",
  "title": "Baijimu Bundle Timer Definition",
  "description": "New Timer authoring uses ServiceReference expressions whose property sources belong to the same Bundle. Scope is checked by the authoring owner.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "name",
    "target",
    "schedule",
    "enabled"
  ],
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200,
      "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
    },
    "description": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 2000,
      "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
    },
    "target": {
      "$ref": "#/$defs/target"
    },
    "schedule": {
      "oneOf": [
        {
          "$ref": "#/$defs/cronSchedule"
        },
        {
          "$ref": "#/$defs/everySchedule"
        },
        {
          "$ref": "#/$defs/atSchedule"
        }
      ]
    },
    "enabled": {
      "type": "boolean"
    },
    "configurationPolicy": {
      "type": [
        "object",
        "null"
      ],
      "additionalProperties": false,
      "properties": {
        "enabled": {
          "type": "boolean"
        },
        "scheduleTypes": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": [
              "CRON",
              "EVERY",
              "AT"
            ]
          }
        }
      }
    }
  },
  "$defs": {
    "nonBlankText": {
      "type": "string",
      "minLength": 1,
      "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "resourceLocator",
        "method"
      ],
      "properties": {
        "method": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "pattern": "^\\S(?:[\\s\\S]*\\S)?$"
        },
        "params": true,
        "resourceLocator": {
          "$ref": "#/$defs/serviceReferenceExpression"
        },
        "executionBindings": {
          "type": "array",
          "maxItems": 16,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "source",
              "parameterPath"
            ],
            "properties": {
              "source": {
                "const": "TRIGGER_ID"
              },
              "parameterPath": {
                "type": "array",
                "minItems": 1,
                "maxItems": 16,
                "items": {
                  "type": "string",
                  "minLength": 1
                }
              }
            }
          }
        }
      }
    },
    "cronSchedule": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "expression",
        "timezone"
      ],
      "properties": {
        "type": {
          "const": "CRON"
        },
        "expression": {
          "$ref": "#/$defs/nonBlankText"
        },
        "timezone": {
          "$ref": "#/$defs/nonBlankText"
        }
      }
    },
    "everySchedule": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "everyMs"
      ],
      "properties": {
        "type": {
          "const": "EVERY"
        },
        "everyMs": {
          "type": "integer",
          "minimum": 300000
        }
      }
    },
    "atSchedule": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "at"
      ],
      "properties": {
        "type": {
          "const": "AT"
        },
        "at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "canonicalText": {
      "type": "string",
      "minLength": 1,
      "pattern": "^\\S(?:[^\\x00-\\x1f\\x7f]*\\S)?$"
    },
    "serviceReferenceExpression": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "contractVersion",
        "expression",
        "references"
      ],
      "properties": {
        "type": {
          "const": "service_reference_expression"
        },
        "contractVersion": {
          "const": "1.0.0"
        },
        "expression": {
          "$ref": "#/$defs/canonicalText"
        },
        "references": {
          "type": "object",
          "minProperties": 1,
          "propertyNames": {
            "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
          },
          "additionalProperties": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "resourceLocator",
              "propertyName"
            ],
            "properties": {
              "resourceLocator": {
                "type": "string",
                "pattern": "^[^/\\s]+/[^/\\s]+/MODULE/[^/\\s]+$"
              },
              "propertyName": {
                "$ref": "#/$defs/canonicalText"
              }
            }
          }
        }
      }
    }
  }
}
