{"record":{"id":"9d5173b9c5cd0f60","repo":"googleapis/mcp-toolbox","slug":"invalid-region-q-must-match-s","errorCode":null,"errorMessage":"invalid region %q: must match %s","messagePattern":"invalid region %q: must match (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/util/cloudsqlconnect/inputvalidate.go","lineNumber":51,"sourceCode":"\tgcpRegionRe        = regexp.MustCompile(`^[a-z]+-[a-z0-9-]+$`)\n\tcloudSQLInstanceRe = regexp.MustCompile(`^[a-z][a-z0-9-]{0,97}[a-z0-9]$`)\n\tgceResourceRe      = regexp.MustCompile(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`)\n\tdatabaseNameRe     = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_-]{0,62}$`)\n)\n\n// ValidateInstanceConnectionName splits and validates project, region, and\n// instance ID per the GCP naming rules. Use this in place of plain\n// ParseConnectionName when the parts will flow into generated code or shell.\nfunc ValidateInstanceConnectionName(connName string) (project, region, instance string, err error) {\n\tproject, region, instance, err = ParseConnectionName(connName)\n\tif err != nil {\n\t\treturn \"\", \"\", \"\", err\n\t}\n\tif !projectIDRe.MatchString(project) {\n\t\treturn \"\", \"\", \"\", fmt.Errorf(\"invalid project ID %q: must match %s\", project, projectIDRe)\n\t}\n\tif !gcpRegionRe.MatchString(region) {\n\t\treturn \"\", \"\", \"\", fmt.Errorf(\"invalid region %q: must match %s\", region, gcpRegionRe)\n\t}\n\tif !cloudSQLInstanceRe.MatchString(instance) {\n\t\treturn \"\", \"\", \"\", fmt.Errorf(\"invalid Cloud SQL instance ID %q: must match %s\", instance, cloudSQLInstanceRe)\n\t}\n\treturn project, region, instance, nil\n}\n\n// ValidateGCEResourceName checks a VM name or zone name against the standard\n// GCE resource-name rule (lowercase, digits, hyphen; must start with a letter\n// and not end with a hyphen, max 63 chars).\nfunc ValidateGCEResourceName(name, kind string) error {\n\tif !gceResourceRe.MatchString(name) {\n\t\treturn fmt.Errorf(\"invalid %s %q: must match %s\", kind, name, gceResourceRe)\n\t}\n\treturn nil\n}\n\n// ValidateDatabaseName accepts the conservative subset of database identifier","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/util/cloudsqlconnect/inputvalidate.go#L33-L69","documentation":"ValidateInstanceConnectionName checks the region segment of a connection name against ^[a-z]+-[a-z0-9-]+$ (lowercase letters, digits, hyphens). The region must look like a valid GCP region string (e.g. us-central1). This guards against malformed input being interpolated into generated connection code and shell commands.","triggerScenarios":"Passing a connection name whose middle segment contains uppercase letters, underscores, spaces, or is empty — e.g. 'proj:US-CENTRAL1:inst' or 'proj:my_region:inst'.","commonSituations":"Region copied from an Azure/AWS name (e.g. 'eastus', 'us-east-1' with digits in AWS style is fine, but 'East US' is not); using a zone instead of a region ('us-central1-a' actually matches the regex, but 'usCentral1' does not); env var with wrong value.","solutions":["Use the exact GCP region shown by `gcloud sql instances describe <instance> --format='value(region)'`","Normalize the region to lowercase with hyphens (e.g. 'us-central1', 'europe-west1')","Verify the connection-name separator ':' wasn't misplaced, shifting a non-region value into the region slot"],"exampleFix":"// before\nValidateInstanceConnectionName(\"my-project:US-Central1:my-db\")\n// after\nValidateInstanceConnectionName(\"my-project:us-central1:my-db\")","handlingStrategy":"validation","validationCode":"var gcpRegionRe = regexp.MustCompile(`^[a-z]+-[a-z0-9-]+$`)\nregion := parts[1]\nif !gcpRegionRe.MatchString(region) {\n    return fmt.Errorf(\"region %q must be a lowercase GCP region like us-central1\", region)\n}","typeGuard":"func isValidGCPRegion(r string) bool {\n    return regexp.MustCompile(`^[a-z]+-[a-z0-9-]+$`).MatchString(r)\n}","tryCatchPattern":"project, region, instance, err := cloudsqlconnect.ValidateInstanceConnectionName(connName)\nif err != nil {\n    return fmt.Errorf(\"invalid connection name %q: %w; fetch region via 'gcloud sql instances describe'\", connName, err)\n}","preventionTips":["Copy region strings directly from `gcloud sql instances describe` output","Normalize user input with strings.ToLower and space->hyphen before validating","Keep a whitelist of the regions your org actually uses"],"tags":["gcp","validation","cloudsql"],"backgroundTag":"input-validation-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}