mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 19:35:06 +03:00
- Move versioning fields (retention days, max noncurrent versions) from local useState into the parent form via react-hook-form + zod, fixing the bug where clearing an input forced it back to "0" and blocked typing a fresh single digit - Show a validation error on submit if versioning is enabled but a field is left empty - Gate object versioning by org billing plan: disable the toggle and show an upgrade prompt on the Free plan (which also doesn't support lifecycle policy management); enforce plan-specific min/max ranges for retention days and max versions on Pro/Team/Enterprise - Add getVersioningPlanLimits() + validateVersioningFields() helpers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TiUEvmC84bRsteqsWXHY2p
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import { type VersioningPlanLimits } from './StorageProtection.constants'
|
|
|
|
/**
|
|
* Parses a raw (possibly empty/partial) numeric input string into a number.
|
|
* Returns `undefined` for empty strings or values that aren't a valid integer,
|
|
* so the caller can distinguish "not provided" from "provided but invalid".
|
|
*/
|
|
export const parseVersioningNumberField = (raw: string | undefined): number | undefined => {
|
|
const trimmed = raw?.trim() ?? ''
|
|
if (trimmed.length === 0) return undefined
|
|
|
|
const parsed = Number(trimmed)
|
|
return Number.isInteger(parsed) ? parsed : undefined
|
|
}
|
|
|
|
export interface VersioningFieldErrors {
|
|
version_expiry_days?: string
|
|
max_noncurrent_versions?: string
|
|
}
|
|
|
|
/**
|
|
* Validates the versioning retention/max-versions fields against the current
|
|
* org plan's bounds. Returns an empty object when there's nothing to report.
|
|
*/
|
|
export const validateVersioningFields = ({
|
|
isEnabled,
|
|
expiryDaysRaw,
|
|
maxVersionsRaw,
|
|
limits,
|
|
}: {
|
|
isEnabled: boolean
|
|
expiryDaysRaw: string | undefined
|
|
maxVersionsRaw: string | undefined
|
|
limits: VersioningPlanLimits | null
|
|
}): VersioningFieldErrors => {
|
|
if (!isEnabled || !limits) return {}
|
|
|
|
const errors: VersioningFieldErrors = {}
|
|
|
|
const expiryDays = parseVersioningNumberField(expiryDaysRaw)
|
|
if (expiryDays === undefined) {
|
|
errors.version_expiry_days = 'Please provide a retention period'
|
|
} else if (expiryDays < limits.minRetentionDays || expiryDays > limits.maxRetentionDays) {
|
|
errors.version_expiry_days = `Must be between ${limits.minRetentionDays} and ${limits.maxRetentionDays} days on your plan`
|
|
}
|
|
|
|
const maxVersions = parseVersioningNumberField(maxVersionsRaw)
|
|
if (maxVersions === undefined) {
|
|
errors.max_noncurrent_versions = 'Please provide a value'
|
|
} else if (maxVersions < limits.minVersions || maxVersions > limits.maxVersions) {
|
|
errors.max_noncurrent_versions = `Must be between ${limits.minVersions} and ${limits.maxVersions} versions on your plan`
|
|
}
|
|
|
|
return errors
|
|
}
|