Files
supabase/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.schema.ts
T
Jordi Enric 6f36fe879b fix(studio): require an available project region FE-3884 (#51582)
## Problem

Project creation can send a request without a region, which the API
rejects. Empty region values pass frontend validation, and a failed
available-regions request fills in a static region name that cannot
resolve to a region selection.

## Fix

Keep the region selector controlled from mount and require a nonempty
value. Disable creation when available regions fail to load, guard
submission against unresolved region selections, and preserve the static
fallback for providers that use it.

Fixes
[FE-3884](https://linear.app/supabase/issue/FE-3884/project-creation-wizard-can-submit-with-an-empty-region).

## How to test

- Open the new-project form and verify the recommended region submits
without manually selecting a region.
- Submit with an empty region. Expected result: an inline validation
error appears and no project creation request is sent.
- Fail the available-regions endpoint, including after changing compute
size with a region already selected. Expected result: the region-loading
error appears, creation is disabled, and direct form submission sends no
project creation request.
- Run `pnpm --filter studio exec vitest --run
'tests/pages/new/[slug].test.tsx'
tests/pages/vercel-deploy-button-new-project.test.tsx
components/interfaces/ProjectCreation/ProjectCreation.schema.test.ts`.

Local validation: all 54 focused tests and formatting pass. Focused
ESLint has no errors. Local typecheck reports the existing image type
mismatch in InstructionBlocks.tsx; CI typecheck passed for the previous
commit.
2026-10-13 12:20:43 +02:00

115 lines
3.7 KiB
TypeScript

import { z } from 'zod'
import { DEFAULT_MINIMUM_PASSWORD_STRENGTH } from '@/lib/constants'
export const FormSchema = z
.object({
organization: z.string({
required_error: 'Please select an organization',
}),
projectName: z
.string()
.trim()
.min(1, 'Please enter a project name.') // Required field check
.min(3, 'Project name must be at least 3 characters long.') // Minimum length check
.max(64, 'Project name must be no longer than 64 characters.'), // Maximum length check
highAvailability: z.boolean(),
postgresVersion: z.string({
required_error: 'Please enter a Postgres version.',
}),
instanceType: z.string().optional(),
kubernetesClusterId: z
.string()
.trim()
.transform((value) => (value === '' ? undefined : value))
.optional(),
kubernetesClusterForce: z.boolean().optional(),
dbRegion: z
.string({ required_error: 'Please select a region.' })
.trim()
.min(1, 'Please select a region.'),
cloudProvider: z.string({
required_error: 'Please select a cloud provider.',
}),
dbPass: z
.string({ required_error: 'Please enter a database password.' })
.min(1, 'Password is required.'),
dbPassStrength: z
.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)])
.default(0),
dbPassStrengthMessage: z.string().default(''),
dbPassStrengthWarning: z.string().default(''),
instanceSize: z.string().optional(),
githubRepositoryId: z.string().optional().default(''),
githubInstallationId: z.number().optional(),
githubRepositoryName: z.string().optional().default(''),
dataApi: z.boolean(),
dataApiDefaultPrivileges: z.boolean(),
enableRlsEventTrigger: z.boolean(),
postgresVersionSelection: z.string(),
useOrioleDb: z.boolean(),
shouldRunMigrations: z.boolean(),
})
.superRefine(
(
{
dbPassStrength,
dbPassStrengthWarning,
highAvailability,
cloudProvider,
useOrioleDb,
kubernetesClusterId,
kubernetesClusterForce,
},
ctx
) => {
if (dbPassStrength < DEFAULT_MINIMUM_PASSWORD_STRENGTH) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['dbPass'],
message: dbPassStrengthWarning || 'Password not secure enough',
})
}
if (highAvailability && cloudProvider !== 'AWS_K8S') {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['cloudProvider'],
message: 'High availability is only supported on AWS (Revamped)',
})
}
if (highAvailability && useOrioleDb) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['highAvailability'],
message: 'High availability is not supported with OrioleDB images',
})
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['useOrioleDb'],
message: 'High availability is not supported with OrioleDB images',
})
}
if (kubernetesClusterId && cloudProvider !== 'AWS_K8S' && cloudProvider !== 'AWS_NIMBUS') {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['kubernetesClusterId'],
message:
'Kubernetes cluster ID is only supported for Kubernetes-architecture cloud providers',
})
}
if (kubernetesClusterForce && !kubernetesClusterId) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['kubernetesClusterForce'],
message: 'Force-deploy has no effect without a Kubernetes cluster ID',
})
}
}
)
export type CreateProjectForm = z.infer<typeof FormSchema>