mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
## What kind of change does this PR introduce? Refactor. Extracts zod number input preprocessing into a shared Studio helper and migrates existing call sites. ## What is the current behavior? Number input clearing logic for controlled `type="number"` fields is duplicated across Studio forms. The pipeline destination form had local helpers, while Auth and settings forms inline the same `z.preprocess` pattern. ## What is the new behavior? Adds `apps/studio/lib/forms/zod-number-input.ts` with: - `requiredNumberInputSchema` for required `z.number()` fields (`''` → `NaN`) - `optionalNumberInputSchema` for optional number fields (`''`/`null` → `undefined`) - `preprocessEmptyNumberInput` for `z.coerce.number()` schemas Migrates: - Pipeline destination form - Connection pooling settings - Auth provider validation (email and SMS OTP fields) - SMTP settings - MFA settings - Protection settings ## To test 1. Open **Authentication > Providers > Email**, clear **Email OTP expiration** or **Minimum password length**, and confirm the field stays empty and shows validation on save. 2. Open **Authentication > Providers > Phone**, select a provider, clear **SMS OTP Expiry**, and confirm validation still works. 3. Open **Authentication > Emails > SMTP settings**, clear **Port** or **Rate limit**, and confirm validation on save. 4. Open **Authentication > Multi-factor authentication**, clear **Max enrolled factors** or **Phone OTP length**, and confirm validation on save. 5. Open **Database > Replication**, start a new pipeline, select **BigQuery**, expand **Advanced settings**, clear a required numeric field, and confirm it stays empty until filled. 6. (Optional, hosted non-HA projects only) Open **Database > Settings**, scroll to **Connection pooling**, clear **Connection pool size**, and confirm the value does not snap back before save. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of empty numeric fields across authentication, SMTP, database replication, and connection pooling settings. * Optional numeric settings can now be cleared without triggering unnecessary validation errors. * Required numeric fields continue to display appropriate validation errors when left incomplete. * Standardized validation behavior for OTP settings, password requirements, SMTP limits, replication configuration, and pool sizing. * Refined protection settings to focus on CAPTCHA and leaked-password options. * **Tests** * Added coverage for empty and optional numeric input behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
25 lines
1.0 KiB
TypeScript
25 lines
1.0 KiB
TypeScript
import * as z from 'zod'
|
|
|
|
export const isEmptyNumberInput = (value: unknown) => value === '' || value == null
|
|
|
|
/**
|
|
* Wraps a zod number schema so an empty controlled input (`''`) fails validation with the
|
|
* schema's `invalid_type_error` instead of snapping back to the previous value.
|
|
*/
|
|
export const requiredNumberInputSchema = (schema: z.ZodNumber) =>
|
|
z.preprocess((value) => (value === '' ? Number.NaN : value), schema)
|
|
|
|
/**
|
|
* Wraps a zod number schema so an empty controlled input (`''` or `null`) normalizes to
|
|
* `undefined` before validation.
|
|
*/
|
|
export const optionalNumberInputSchema = (schema: z.ZodNumber) =>
|
|
z.preprocess((value) => (isEmptyNumberInput(value) ? undefined : value), schema.optional())
|
|
|
|
/**
|
|
* Normalizes empty controlled number inputs (`''` or `null`) to `undefined` before running
|
|
* the inner schema. Useful with `z.coerce.number()` in auth and settings forms.
|
|
*/
|
|
export const preprocessEmptyNumberInput = <T extends z.ZodTypeAny>(schema: T) =>
|
|
z.preprocess((value) => (isEmptyNumberInput(value) ? undefined : value), schema)
|