{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.shimpz.com/specs/driver/v1/credential-form.schema.json",
  "title": "Shimpz Driver Credential Form v1",
  "description": "Closed declarative form contract for Capsule-scoped Driver credential sets.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema_version", "owner_scope", "cardinality", "profiles"],
  "properties": {
    "schema_version": {
      "const": 1
    },
    "owner_scope": {
      "const": "capsule"
    },
    "cardinality": {
      "enum": ["one", "many"]
    },
    "profiles": {
      "type": "array",
      "minItems": 1,
      "maxItems": 16,
      "uniqueItems": true,
      "items": {
        "oneOf": [
          {
            "$ref": "#/$defs/secretFieldsProfile"
          },
          {
            "$ref": "#/$defs/oauthAuthorizationCodeProfile"
          }
        ]
      }
    }
  },
  "$defs": {
    "profileId": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$",
      "maxLength": 80
    },
    "fieldId": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$",
      "maxLength": 80
    },
    "title": {
      "type": "string",
      "pattern": "^\\S(?:[^\\r\\n]*\\S)?$",
      "maxLength": 80
    },
    "summary": {
      "type": "string",
      "pattern": "^\\S(?:[^\\r\\n]*\\S)?$",
      "maxLength": 240
    },
    "textFormat": {
      "enum": [
        "plain-text",
        "account-id",
        "bucket-name",
        "hostname",
        "region",
        "tenant-id",
        "username"
      ]
    },
    "secretFormat": {
      "enum": [
        "access-key-id",
        "api-key",
        "password",
        "private-key",
        "secret-access-key",
        "secret-token"
      ]
    },
    "textField": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "label", "required", "format", "min_length", "max_length"],
      "properties": {
        "id": {
          "$ref": "#/$defs/fieldId"
        },
        "type": {
          "const": "text"
        },
        "label": {
          "$ref": "#/$defs/title"
        },
        "help": {
          "$ref": "#/$defs/summary"
        },
        "required": {
          "type": "boolean"
        },
        "format": {
          "$ref": "#/$defs/textFormat"
        },
        "min_length": {
          "type": "integer",
          "minimum": 0,
          "maximum": 1024
        },
        "max_length": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1024
        }
      }
    },
    "secretField": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type",
        "label",
        "required",
        "format",
        "min_length",
        "max_length",
        "write_only"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/fieldId"
        },
        "type": {
          "const": "secret"
        },
        "label": {
          "$ref": "#/$defs/title"
        },
        "help": {
          "$ref": "#/$defs/summary"
        },
        "required": {
          "type": "boolean"
        },
        "format": {
          "$ref": "#/$defs/secretFormat"
        },
        "min_length": {
          "type": "integer",
          "minimum": 1,
          "maximum": 16384
        },
        "max_length": {
          "type": "integer",
          "minimum": 1,
          "maximum": 16384
        },
        "write_only": {
          "const": true
        }
      }
    },
    "selectOption": {
      "type": "object",
      "additionalProperties": false,
      "required": ["value", "label"],
      "properties": {
        "value": {
          "type": "string",
          "pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]{0,79}$"
        },
        "label": {
          "$ref": "#/$defs/title"
        }
      }
    },
    "selectField": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "label", "required", "format", "options"],
      "properties": {
        "id": {
          "$ref": "#/$defs/fieldId"
        },
        "type": {
          "const": "select"
        },
        "label": {
          "$ref": "#/$defs/title"
        },
        "help": {
          "$ref": "#/$defs/summary"
        },
        "required": {
          "type": "boolean"
        },
        "format": {
          "const": "option"
        },
        "options": {
          "type": "array",
          "minItems": 1,
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/selectOption"
          }
        }
      }
    },
    "secretFieldsProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "title", "fields"],
      "properties": {
        "id": {
          "$ref": "#/$defs/profileId"
        },
        "kind": {
          "const": "secret-fields"
        },
        "title": {
          "$ref": "#/$defs/title"
        },
        "summary": {
          "$ref": "#/$defs/summary"
        },
        "fields": {
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "uniqueItems": true,
          "items": {
            "oneOf": [
              {
                "$ref": "#/$defs/textField"
              },
              {
                "$ref": "#/$defs/secretField"
              },
              {
                "$ref": "#/$defs/selectField"
              }
            ]
          }
        }
      }
    },
    "oauthAuthorizationCodeProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "title", "adapter_id", "scopes", "pkce"],
      "properties": {
        "id": {
          "$ref": "#/$defs/profileId"
        },
        "kind": {
          "const": "oauth2-authorization-code"
        },
        "title": {
          "$ref": "#/$defs/title"
        },
        "summary": {
          "$ref": "#/$defs/summary"
        },
        "adapter_id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$",
          "maxLength": 80
        },
        "scopes": {
          "type": "array",
          "minItems": 1,
          "maxItems": 32,
          "uniqueItems": true,
          "items": {
            "type": "string",
            "pattern": "^[A-Za-z0-9][A-Za-z0-9:._/-]{0,255}$"
          }
        },
        "pkce": {
          "const": "S256"
        }
      }
    }
  }
}
