Files
Danny White 8047d93e3c feat(studio): validate pipeline wizard connections (#51818)
## Problem

The pipeline wizard from #51776 allows users to continue without valid
destination credentials. This is the next slice of #49243.

## Solution

- Validate connection fields for all four destinations using the
existing schemas and helpers.
- Validate the destination on **Continue** and advance to Data on
success. Show field errors and connection failures inline; pause for
warnings before continuing.
- Require a new check after connection settings change and block
browser-history shortcuts past an unverified connection.

| Errored Test | Success Test |
| --- | --- |
| <img width="1532" height="2062" alt="CleanShot 2026-10-16 at 12 19
51@2x"
src="https://github.com/user-attachments/assets/a40a6c01-ee89-4601-b5d7-6ffc85071dc7"
/> | <img width="1500" height="1950" alt="CleanShot 2026-10-16 at 12 18
33@2x"
src="https://github.com/user-attachments/assets/c50c1ad4-256b-44b5-bd06-ec0685aae759"
/> |
 
The screenshots show earlier test states; successful checks now advance
directly to Data.

Data validation and pipeline creation remain for later slices. **Start
pipeline** stays disabled.

## Review instructions

1. Open the [Pipelines
preview](https://studio-staging-git-dnywh-featpipeline-connectio-eb9566-supabase.vercel.app/dashboard/project/_/database/pipelines),
select a test project and enable **Pipeline creation wizard**. Open the
[wizard
preview](https://studio-staging-git-dnywh-featpipeline-connectio-eb9566-supabase.vercel.app/dashboard/project/_/database/pipelines/new)
for that project.
2. Choose a destination and click **Continue** with empty or invalid
fields. Check the errors, then enter valid credentials and retry. A
failed check keeps your values. A failed result stays visible while
editing, until retry or destination change. Changed settings still
require a fresh check. A successful check opens Data automatically;
warnings remain for review before continuing.
3. Continue to Data, return to Connection and change a credential or
advanced connection setting. Check that browser **Forward** cannot skip
the new check, then click **Continue** to validate again.
2026-10-19 10:15:08 +11:00

94 lines
3.2 KiB
TypeScript

import { useLayoutEffect, useRef, useState } from 'react'
import { useWatch, type UseFormReturn } from 'react-hook-form'
import type { DestinationPanelSchemaType } from '../DestinationPanel/DestinationForm/DestinationForm.schema'
import { useDestinationValidation } from '../DestinationPanel/DestinationForm/useDestinationValidation'
import {
getConnectionFieldNames,
getConnectionValidationIssues,
} from './PipelineConnectionValidation.utils'
import type { ValidationFailure } from '@/data/replication/validate-destination-mutation'
export const usePipelineConnectionValidation = ({
projectRef,
type,
form,
}: {
projectRef?: string
type: Parameters<typeof getConnectionFieldNames>[0]
form: UseFormReturn<DestinationPanelSchemaType>
}) => {
const names = getConnectionFieldNames(type)
const values = useWatch({ control: form.control, name: names })
const snapshot = JSON.stringify([type, ...values])
const currentSnapshotRef = useRef(snapshot)
const [result, setResult] = useState<{
snapshot: string
type: typeof type
failures: ValidationFailure[]
verified: boolean
}>()
const [isValidating, setIsValidating] = useState(false)
const isValidatingRef = useRef(false)
const { validateDestination } = useDestinationValidation({ projectRef, selectedType: type })
const isConnectionVerified = result?.snapshot === snapshot && result.verified
const failures =
result?.type === type && (!result.verified || isConnectionVerified) ? result.failures : []
const validateConnection = async () => {
if (isValidatingRef.current) return false
form.clearErrors(names)
const data = form.getValues()
const issues = getConnectionValidationIssues(type, data)
if (issues.length > 0) {
issues.forEach(({ path, message }) => form.setError(path, { type: 'validate', message }))
form.setFocus(issues[0].path)
setResult(undefined)
return false
}
if (isConnectionVerified) return true
isValidatingRef.current = true
setIsValidating(true)
setResult(undefined)
try {
const { validation_failures: failures } = await validateDestination({ data })
if (currentSnapshotRef.current !== snapshot) return false
const verified = !failures.some((failure) => failure.failure_type === 'critical')
setResult({ snapshot, type, failures, verified })
// Warnings stay on this step for review before continuing.
return verified && failures.length === 0
} catch (error) {
if (currentSnapshotRef.current === snapshot) {
setResult({
snapshot,
type,
verified: false,
failures: [
{
name: 'Failed to validate destination',
failure_type: 'critical',
reason: error instanceof Error ? error.message : 'Please try again.',
},
],
})
}
return false
} finally {
isValidatingRef.current = false
setIsValidating(false)
}
}
useLayoutEffect(() => {
currentSnapshotRef.current = snapshot
}, [snapshot])
return {
validateConnection,
resetValidation: () => setResult(undefined),
isValidating,
isConnectionVerified,
failures,
}
}