Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore yup to zod (#44803)

Gildas Garciac6067a1a19a2
diff --git a/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx b/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx
index 55ce24c3314..b6de78934ae 100644
--- a/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx
+++ b/apps/studio/components/interfaces/Auth/AuditLogsForm.tsx
@@ -1,4 +1,4 @@
-import { yupResolver } from '@hookform/resolvers/yup'
+import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import { useEffect } from 'react'
@@ -23,7 +23,7 @@ import {
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
-import { boolean, object } from 'yup'
+import * as z from 'zod'
import { AlertError } from '@/components/ui/AlertError'
import { InlineLink } from '@/components/ui/InlineLink'
@@ -33,8 +33,8 @@ import { useTablesQuery } from '@/data/tables/tables-query'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
-const schema = object({
- AUDIT_LOG_DISABLE_POSTGRES: boolean().required(),
+const schema = z.object({
+ AUDIT_LOG_DISABLE_POSTGRES: z.boolean(),
})
const AUDIT_LOG_ENTRIES_TABLE = 'audit_log_entries'
@@ -73,9 +73,10 @@ export const AuditLogsForm = () => {
})
const form = useForm({
- resolver: yupResolver(schema),
+ resolver: zodResolver(schema),
defaultValues: { AUDIT_LOG_DISABLE_POSTGRES: false },
})
+ const { isDirty } = form.formState
const { AUDIT_LOG_DISABLE_POSTGRES: formValueDisablePostgres } = form.watch()
const currentlyDisabled = authConfig?.AUDIT_LOG_DISABLE_POSTGRES ?? false
const isDisabling = !currentlyDisabled && formValueDisablePostgres
@@ -192,7 +193,7 @@ export const AuditLogsForm = () => {
</CardContent>
<CardFooter className="justify-end space-x-2">
- {form.formState.isDirty && (
+ {isDirty && (
<Button type="default" onClick={() => form.reset()}>
Cancel
</Button>
@@ -200,7 +201,7 @@ export const AuditLogsForm = () => {
<Button
type="primary"
htmlType="submit"
- disabled={!canUpdateConfig || isUpdatingConfig || !form.formState.isDirty}
+ disabled={!canUpdateConfig || isUpdatingConfig || !isDirty}
loading={isUpdatingConfig}
>
Save changes
diff --git a/apps/studio/components/interfaces/Auth/AuthTemplatesValidation.tsx b/apps/studio/components/interfaces/Auth/AuthTemplatesValidation.tsx
index 7a6aabe88f7..915c1d06f2c 100644
--- a/apps/studio/components/interfaces/Auth/AuthTemplatesValidation.tsx
+++ b/apps/studio/components/interfaces/Auth/AuthTemplatesValidation.tsx
@@ -1,4 +1,4 @@
-import { object, string } from 'yup'
+import * as z from 'zod'
import type { FormSchema } from '@/types'
@@ -19,9 +19,9 @@ const CONFIRMATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .ConfirmationURL }}\` : URL to confirm the email address for the new account
-- \`{{ .Token }}\` : The 6-digit numeric email OTP
+- \`{{ .Token }}\` : The 6-digit numeric email OTP
- \`{{ .TokenHash }}\` : The hashed token used in the URL
- \`{{ .SiteURL }}\` : The URL of the site
- \`{{ .Email }}\` : The user's email address
@@ -30,8 +30,8 @@ const CONFIRMATION: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_CONFIRMATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_CONFIRMATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'authentication',
@@ -53,9 +53,9 @@ const INVITE: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .ConfirmationURL }}\` : URL to accept the invitation to create an account
-- \`{{ .Token }}\` : The 6-digit numeric email OTP
+- \`{{ .Token }}\` : The 6-digit numeric email OTP
- \`{{ .TokenHash }}\` : The hashed token used in the URL
- \`{{ .SiteURL }}\` : The URL of the site
- \`{{ .Email }}\` : The user's email address
@@ -64,8 +64,8 @@ const INVITE: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_INVITE: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_INVITE: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'authentication',
@@ -87,9 +87,9 @@ const MAGIC_LINK: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .ConfirmationURL }}\` : URL for a one-time login to the user's account
-- \`{{ .Token }}\` : The 6-digit numeric email OTP
+- \`{{ .Token }}\` : The 6-digit numeric email OTP
- \`{{ .TokenHash }}\` : The hashed token used in the URL
- \`{{ .SiteURL }}\` : The URL of the site
- \`{{ .Email }}\` : The user's email address
@@ -98,8 +98,8 @@ const MAGIC_LINK: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_MAGIC_LINK: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_MAGIC_LINK: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'authentication',
@@ -121,9 +121,9 @@ const EMAIL_CHANGE: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .ConfirmationURL }}\` : URL to confirm the email change
-- \`{{ .Token }}\` : The 6-digit numeric email OTP
+- \`{{ .Token }}\` : The 6-digit numeric email OTP
- \`{{ .TokenHash }}\` : The hashed token used in the URL
- \`{{ .SiteURL }}\` : The URL of the site
- \`{{ .Email }}\` : The original user's email address
@@ -133,8 +133,8 @@ const EMAIL_CHANGE: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_EMAIL_CHANGE: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_EMAIL_CHANGE: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'authentication',
@@ -156,9 +156,9 @@ const RECOVERY: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .ConfirmationURL }}\` : URL to confirm the password reset
-- \`{{ .Token }}\` : The 6-digit numeric email OTP
+- \`{{ .Token }}\` : The 6-digit numeric email OTP
- \`{{ .TokenHash }}\` : The hashed token used in the URL
- \`{{ .SiteURL }}\` : The URL of the site
- \`{{ .Email }}\` : The user's email address
@@ -167,8 +167,8 @@ const RECOVERY: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_RECOVERY: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_RECOVERY: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'authentication',
@@ -197,8 +197,8 @@ const REAUTHENTICATION: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_REAUTHENTICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_REAUTHENTICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'authentication',
@@ -221,14 +221,14 @@ const PASSWORD_CHANGED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's email address
- \`{{ .Data }}\` : The user's \`user_metadata\`
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_PASSWORD_CHANGED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_PASSWORD_CHANGED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
@@ -250,15 +250,15 @@ const EMAIL_CHANGED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's new email address
- \`{{ .OldEmail }}\` : The user's old email address
- \`{{ .Data }}\` : The user's \`user_metadata\`
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_EMAIL_CHANGED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_EMAIL_CHANGED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
@@ -280,7 +280,7 @@ const PHONE_CHANGED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's email address
- \`{{ .Phone }}\` : The user's new phone number
- \`{{ .OldPhone }}\` : The user's old phone number
@@ -288,8 +288,8 @@ const PHONE_CHANGED_NOTIFICATION: FormSchema = {
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_PHONE_CHANGED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_PHONE_CHANGED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
@@ -311,15 +311,15 @@ const IDENTITY_LINKED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's email address
- \`{{ .Provider }}\` : The provider of the newly linked identity
- \`{{ .Data }}\` : The user's \`user_metadata\`
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_IDENTITY_LINKED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_IDENTITY_LINKED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
@@ -341,15 +341,15 @@ const IDENTITY_UNLINKED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's email address
- \`{{ .Provider }}\` : The provider of the unlinked identity
- \`{{ .Data }}\` : The user's \`user_metadata\`
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_IDENTITY_UNLINKED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_IDENTITY_UNLINKED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
@@ -372,15 +372,15 @@ const MFA_FACTOR_ENROLLED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's email address
- \`{{ .FactorType }}\` : The type of the newly enrolled MFA factor
- \`{{ .Data }}\` : The user's \`user_metadata\`
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_MFA_FACTOR_ENROLLED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_MFA_FACTOR_ENROLLED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
@@ -403,15 +403,15 @@ const MFA_FACTOR_UNENROLLED_NOTIFICATION: FormSchema = {
title: 'Body',
descriptionOptional: 'HTML body of your email',
type: 'code',
- description: `
+ description: `
- \`{{ .Email }}\` : The user's email address
- \`{{ .FactorType }}\` : The type of the newly enrolled MFA factor
- \`{{ .Data }}\` : The user's \`user_metadata\`
`,
},
},
- validationSchema: object().shape({
- MAILER_SUBJECTS_MFA_FACTOR_UNENROLLED_NOTIFICATION: string().required('Subject is required.'),
+ validationSchema: z.object({
+ MAILER_SUBJECTS_MFA_FACTOR_UNENROLLED_NOTIFICATION: z.string().min(1, 'Subject is required.'),
}),
misc: {
emailTemplateType: 'security',
diff --git a/apps/studio/components/interfaces/Auth/BasicAuthSettingsForm.tsx b/apps/studio/components/interfaces/Auth/BasicAuthSettingsForm.tsx
index 829bb5c4fa9..cdadbcfbc75 100644
--- a/apps/studio/components/interfaces/Auth/BasicAuthSettingsForm.tsx
+++ b/apps/studio/components/interfaces/Auth/BasicAuthSettingsForm.tsx
@@ -1,4 +1,4 @@
-import { yupResolver } from '@hookform/resolvers/yup'
+import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import { ExternalLink } from 'lucide-react'
@@ -29,7 +29,7 @@ import {
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
-import { boolean, object, string } from 'yup'
+import * as z from 'zod'
import { NO_REQUIRED_CHARACTERS } from './Auth.constants'
import AlertError from '@/components/ui/AlertError'
@@ -41,12 +41,12 @@ import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
import { DOCS_URL } from '@/lib/constants'
-const schema = object({
- DISABLE_SIGNUP: boolean().required(),
- EXTERNAL_ANONYMOUS_USERS_ENABLED: boolean().required(),
- SECURITY_MANUAL_LINKING_ENABLED: boolean().required(),
- MAILER_AUTOCONFIRM: boolean().required(),
- SITE_URL: string().required('Must have a Site URL'),
+const schema = z.object({
+ DISABLE_SIGNUP: z.boolean(),
+ EXTERNAL_ANONYMOUS_USERS_ENABLED: z.boolean(),
+ SECURITY_MANUAL_LINKING_ENABLED: z.boolean(),
+ MAILER_AUTOCONFIRM: z.boolean(),
+ SITE_URL: z.string().min(1, 'Must have a Site URL'),
})
export const BasicAuthSettingsForm = () => {
@@ -72,7 +72,7 @@ export const BasicAuthSettingsForm = () => {
)
const form = useForm({
- resolver: yupResolver(schema),
+ resolver: zodResolver(schema),
defaultValues: {
DISABLE_SIGNUP: true,
EXTERNAL_ANONYMOUS_USERS_ENABLED: false,
@@ -81,6 +81,7 @@ export const BasicAuthSettingsForm = () => {
SITE_URL: '',
},
})
+ const { isDirty } = form.formState
useEffect(() => {
if (authConfig) {
@@ -325,7 +326,7 @@ export const BasicAuthSettingsForm = () => {
/>
</CardContent>
<CardFooter className="justify-end space-x-2">
- {form.formState.isDirty && (
+ {isDirty && (
<Button type="default" onClick={() => form.reset()}>
Cancel
</Button>
@@ -333,7 +334,7 @@ export const BasicAuthSettingsForm = () => {
<Button
type="primary"
htmlType="submit"
- disabled={!canUpdateConfig || isUpdatingConfig || !form.formState.isDirty}
+ disabled={!canUpdateConfig || isUpdatingConfig || !isDirty}
loading={isUpdatingConfig}
>
Save changes
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.tsx b/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.tsx
index b5efd7eb210..9ce0fa83604 100644
--- a/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.tsx
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.tsx
@@ -29,7 +29,7 @@ import {
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
-import { z } from 'zod'
+import * as z from 'zod'
import { TEMPLATES_SCHEMAS } from '../AuthTemplatesValidation'
import { slugifyTitle } from './EmailTemplates.utils'
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx b/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx
index 47a1888d027..650a2389f8d 100644
--- a/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx
@@ -1,3 +1,4 @@
+import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import type { editor } from 'monaco-editor'
@@ -80,7 +81,10 @@ export const TemplateEditor = ({ template }: TemplateEditorProps) => {
return result
}, [authConfig, properties])
- const form = useForm({ defaultValues: INITIAL_VALUES })
+ const form = useForm({
+ defaultValues: INITIAL_VALUES,
+ resolver: zodResolver(template.validationSchema),
+ })
const onSubmit = (values: any) => {
if (!projectRef) return console.error('Project ref is required')
diff --git a/apps/studio/components/interfaces/Auth/ProtectionAuthSettingsForm/ProtectionAuthSettingsForm.tsx b/apps/studio/components/interfaces/Auth/ProtectionAuthSettingsForm/ProtectionAuthSettingsForm.tsx
index 270aae48da2..603a1fbb730 100644
--- a/apps/studio/components/interfaces/Auth/ProtectionAuthSettingsForm/ProtectionAuthSettingsForm.tsx
+++ b/apps/studio/components/interfaces/Auth/ProtectionAuthSettingsForm/ProtectionAuthSettingsForm.tsx
@@ -1,9 +1,9 @@
-import { yupResolver } from '@hookform/resolvers/yup'
+import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import Link from 'next/link'
import { useEffect } from 'react'
-import { useForm } from 'react-hook-form'
+import { useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
import {
Badge,
@@ -31,7 +31,7 @@ import {
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
-import { boolean, number, object, string } from 'yup'
+import * as z from 'zod'
import { NO_REQUIRED_CHARACTERS } from '../Auth.constants'
import AlertError from '@/components/ui/AlertError'
@@ -47,31 +47,65 @@ const CAPTCHA_PROVIDERS = [
{ key: 'turnstile', label: 'Turnstile by Cloudflare' },
]
-const schema = object({
- DISABLE_SIGNUP: boolean().required(),
- EXTERNAL_ANONYMOUS_USERS_ENABLED: boolean().required(),
- SECURITY_MANUAL_LINKING_ENABLED: boolean().required(),
- SITE_URL: string().required('Must have a Site URL'),
- SECURITY_CAPTCHA_ENABLED: boolean().required(),
- SECURITY_CAPTCHA_SECRET: string().when('SECURITY_CAPTCHA_ENABLED', {
- is: true,
- then: (schema) => schema.required('Must have a Captcha secret'),
- }),
- SECURITY_CAPTCHA_PROVIDER: string().when('SECURITY_CAPTCHA_ENABLED', {
- is: true,
- then: (schema) =>
- schema
- .oneOf(['hcaptcha', 'turnstile'])
- .required('Captcha provider must be either hcaptcha or turnstile'),
- }),
- SESSIONS_TIMEBOX: number().min(0, 'Must be a positive number'),
- SESSIONS_INACTIVITY_TIMEOUT: number().min(0, 'Must be a positive number'),
- SESSIONS_SINGLE_PER_USER: boolean(),
- PASSWORD_MIN_LENGTH: number().min(6, 'Must be greater or equal to 6.'),
- PASSWORD_REQUIRED_CHARACTERS: string(),
- PASSWORD_HIBP_ENABLED: boolean(),
+type CaptchaProviders = 'hcaptcha' | 'turnstile'
+
+const baseSchema = z.object({
+ DISABLE_SIGNUP: z.boolean(),
+ EXTERNAL_ANONYMOUS_USERS_ENABLED: z.boolean(),
+ SECURITY_MANUAL_LINKING_ENABLED: z.boolean(),
+ SITE_URL: z.string().min(1, 'Must have a Site URL'),
+ SESSIONS_TIMEBOX: z
+ .preprocess(
+ (val) => (val === '' || val == null ? undefined : val),
+ z.coerce
+ .number({
+ required_error: 'Must have a sessions timebox',
+ invalid_type_error: 'Must have a sessions timebox',
+ })
+ .min(0, 'Must be greater than or equal to 0.')
+ )
+ .optional(),
+ SESSIONS_INACTIVITY_TIMEOUT: z.number().min(0, 'Must be greater than or equal to 0').optional(),
+ SESSIONS_SINGLE_PER_USER: z.boolean().optional(),
+ PASSWORD_MIN_LENGTH: z
+ .preprocess(
+ (val) => (val === '' || val == null ? undefined : val),
+ z.coerce
+ .number({
+ required_error: 'Must have a password min length',
+ invalid_type_error: 'Must have a password min length',
+ })
+ .min(6, 'Must be greater or equal to 6.')
+ )
+ .optional(),
+ PASSWORD_REQUIRED_CHARACTERS: z.string().optional(),
+ PASSWORD_HIBP_ENABLED: z.boolean().optional(),
})
+const captchaEnabledSchema = z
+ .object({
+ SECURITY_CAPTCHA_ENABLED: z.literal(true),
+ SECURITY_CAPTCHA_SECRET: z.string().min(1, 'Must have a Captcha secret'),
+ SECURITY_CAPTCHA_PROVIDER: z.enum(['hcaptcha', 'turnstile'], {
+ required_error: 'Captcha provider must be either hcaptcha or turnstile',
+ }),
+ })
+ .merge(baseSchema)
+
+const captchaDisabledSchema = z
+ .object({
+ SECURITY_CAPTCHA_ENABLED: z.literal(false),
+ SECURITY_CAPTCHA_SECRET: z.string().optional(),
+ SECURITY_CAPTCHA_PROVIDER: z.string().optional(),
+ })
+ .merge(baseSchema)
+
+const formSchema = z.discriminatedUnion('SECURITY_CAPTCHA_ENABLED', [
+ captchaEnabledSchema,
+ captchaDisabledSchema,
+])
+type FormSchema = z.infer<typeof formSchema>
+
export const ProtectionAuthSettingsForm = () => {
const { ref: projectRef } = useParams()
const {
@@ -98,8 +132,8 @@ export const ProtectionAuthSettingsForm = () => {
'custom_config_gotrue'
)
- const protectionForm = useForm({
- resolver: yupResolver(schema),
+ const protectionForm = useForm<FormSchema>({
+ resolver: zodResolver(formSchema),
defaultValues: {
DISABLE_SIGNUP: true,
EXTERNAL_ANONYMOUS_USERS_ENABLED: false,
@@ -117,24 +151,48 @@ export const ProtectionAuthSettingsForm = () => {
},
})
+ const { isDirty } = protectionForm.formState
+
useEffect(() => {
if (authConfig && !isUpdatingConfig) {
- protectionForm.reset({
- DISABLE_SIGNUP: !authConfig.DISABLE_SIGNUP,
- EXTERNAL_ANONYMOUS_USERS_ENABLED: authConfig.EXTERNAL_ANONYMOUS_USERS_ENABLED || false,
- SECURITY_MANUAL_LINKING_ENABLED: authConfig.SECURITY_MANUAL_LINKING_ENABLED || false,
- SITE_URL: authConfig.SITE_URL || '',
- SECURITY_CAPTCHA_ENABLED: authConfig.SECURITY_CAPTCHA_ENABLED || false,
- SECURITY_CAPTCHA_SECRET: authConfig.SECURITY_CAPTCHA_SECRET || '',
- SECURITY_CAPTCHA_PROVIDER: authConfig.SECURITY_CAPTCHA_PROVIDER || 'hcaptcha',
- SESSIONS_TIMEBOX: authConfig.SESSIONS_TIMEBOX || 0,
- SESSIONS_INACTIVITY_TIMEOUT: authConfig.SESSIONS_INACTIVITY_TIMEOUT || 0,
- SESSIONS_SINGLE_PER_USER: authConfig.SESSIONS_SINGLE_PER_USER || false,
- PASSWORD_MIN_LENGTH: authConfig.PASSWORD_MIN_LENGTH || 6,
- PASSWORD_REQUIRED_CHARACTERS:
- authConfig.PASSWORD_REQUIRED_CHARACTERS || NO_REQUIRED_CHARACTERS,
- PASSWORD_HIBP_ENABLED: authConfig.PASSWORD_HIBP_ENABLED || false,
- })
+ const SECURITY_CAPTCHA_PROVIDER = (authConfig.SECURITY_CAPTCHA_PROVIDER ||
+ 'hcaptcha') as CaptchaProviders
+
+ if (authConfig.SECURITY_CAPTCHA_ENABLED) {
+ protectionForm.reset({
+ DISABLE_SIGNUP: !authConfig.DISABLE_SIGNUP,
+ EXTERNAL_ANONYMOUS_USERS_ENABLED: authConfig.EXTERNAL_ANONYMOUS_USERS_ENABLED || false,
+ SECURITY_MANUAL_LINKING_ENABLED: authConfig.SECURITY_MANUAL_LINKING_ENABLED || false,
+ SITE_URL: authConfig.SITE_URL || '',
+ SECURITY_CAPTCHA_ENABLED: authConfig.SECURITY_CAPTCHA_ENABLED,
+ SECURITY_CAPTCHA_SECRET: authConfig.SECURITY_CAPTCHA_SECRET || '',
+ SECURITY_CAPTCHA_PROVIDER,
+ SESSIONS_TIMEBOX: authConfig.SESSIONS_TIMEBOX || 0,
+ SESSIONS_INACTIVITY_TIMEOUT: authConfig.SESSIONS_INACTIVITY_TIMEOUT || 0,
+ SESSIONS_SINGLE_PER_USER: authConfig.SESSIONS_SINGLE_PER_USER || false,
+ PASSWORD_MIN_LENGTH: authConfig.PASSWORD_MIN_LENGTH || 6,
+ PASSWORD_REQUIRED_CHARACTERS:
+ authConfig.PASSWORD_REQUIRED_CHARACTERS || NO_REQUIRED_CHARACTERS,
+ PASSWORD_HIBP_ENABLED: authConfig.PASSWORD_HIBP_ENABLED || false,
+ })
+ } else {
+ protectionForm.reset({
+ DISABLE_SIGNUP: !authConfig.DISABLE_SIGNUP,
+ EXTERNAL_ANONYMOUS_USERS_ENABLED: authConfig.EXTERNAL_ANONYMOUS_USERS_ENABLED || false,
+ SECURITY_MANUAL_LINKING_ENABLED: authConfig.SECURITY_MANUAL_LINKING_ENABLED || false,
+ SITE_URL: authConfig.SITE_URL || '',
+ SECURITY_CAPTCHA_ENABLED: authConfig.SECURITY_CAPTCHA_ENABLED,
+ SECURITY_CAPTCHA_SECRET: authConfig.SECURITY_CAPTCHA_SECRET || '',
+ SECURITY_CAPTCHA_PROVIDER,
+ SESSIONS_TIMEBOX: authConfig.SESSIONS_TIMEBOX || 0,
+ SESSIONS_INACTIVITY_TIMEOUT: authConfig.SESSIONS_INACTIVITY_TIMEOUT || 0,
+ SESSIONS_SINGLE_PER_USER: authConfig.SESSIONS_SINGLE_PER_USER || false,
+ PASSWORD_MIN_LENGTH: authConfig.PASSWORD_MIN_LENGTH || 6,
+ PASSWORD_REQUIRED_CHARACTERS:
+ authConfig.PASSWORD_REQUIRED_CHARACTERS || NO_REQUIRED_CHARACTERS,
+ PASSWORD_HIBP_ENABLED: authConfig.PASSWORD_HIBP_ENABLED || false,
+ })
+ }
}
}, [authConfig, isUpdatingConfig])
@@ -149,6 +207,11 @@ export const ProtectionAuthSettingsForm = () => {
updateAuthConfig({ projectRef: projectRef!, config: payload })
}
+ const SECURITY_CAPTCHA_ENABLED = useWatch({
+ name: 'SECURITY_CAPTCHA_ENABLED',
+ control: protectionForm.control,
+ })
+
if (isError) {
return (
<PageSection>
@@ -212,7 +275,7 @@ export const ProtectionAuthSettingsForm = () => {
/>
</CardContent>
- {protectionForm.watch('SECURITY_CAPTCHA_ENABLED') && (
+ {SECURITY_CAPTCHA_ENABLED && (
<>
<CardContent>
<FormField_Shadcn_
@@ -304,7 +367,7 @@ export const ProtectionAuthSettingsForm = () => {
</CardContent>
<CardFooter className="justify-end space-x-2">
- {protectionForm.formState.isDirty && (
+ {isDirty && (
<Button type="default" onClick={() => protectionForm.reset()}>
Cancel
</Button>
@@ -312,9 +375,7 @@ export const ProtectionAuthSettingsForm = () => {
<Button
type="primary"
htmlType="submit"
- disabled={
- !canUpdateConfig || isUpdatingConfig || !protectionForm.formState.isDirty
- }
+ disabled={!canUpdateConfig || isUpdatingConfig || !isDirty}
loading={isUpdatingConfig}
>
Save changes
diff --git a/apps/studio/components/interfaces/Auth/SiteUrl/SiteUrl.tsx b/apps/studio/components/interfaces/Auth/SiteUrl/SiteUrl.tsx
index 04aaee19792..a78fa239503 100644
--- a/apps/studio/components/interfaces/Auth/SiteUrl/SiteUrl.tsx
+++ b/apps/studio/components/interfaces/Auth/SiteUrl/SiteUrl.tsx
@@ -1,4 +1,4 @@
-import { yupResolver } from '@hookform/resolvers/yup'
+import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import { useEffect, useState } from 'react'
@@ -23,15 +23,15 @@ import {
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
-import { object, string } from 'yup'
+import * as z from 'zod'
import AlertError from '@/components/ui/AlertError'
import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
import { useAuthConfigUpdateMutation } from '@/data/auth/auth-config-update-mutation'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
-const schema = object({
- SITE_URL: string().required('Must have a Site URL'),
+const schema = z.object({
+ SITE_URL: z.string().min(1, 'Must have a Site URL'),
})
const SiteUrl = () => {
@@ -51,11 +51,12 @@ const SiteUrl = () => {
)
const siteUrlForm = useForm({
- resolver: yupResolver(schema),
+ resolver: zodResolver(schema),
defaultValues: {
SITE_URL: '',
},
})
+ const { isDirty } = siteUrlForm.formState
useEffect(() => {
if (authConfig && !isUpdatingSiteUrl) {
@@ -133,7 +134,7 @@ const SiteUrl = () => {
</CardContent>
<CardFooter className="justify-end space-x-2">
- {siteUrlForm.formState.isDirty && (
+ {isDirty && (
<Button type="default" onClick={() => siteUrlForm.reset()}>
Cancel
</Button>
@@ -141,7 +142,7 @@ const SiteUrl = () => {
<Button
type="primary"
htmlType="submit"
- disabled={!canUpdateConfig || isUpdatingSiteUrl || !siteUrlForm.formState.isDirty}
+ disabled={!canUpdateConfig || isUpdatingSiteUrl || !isDirty}
loading={isUpdatingSiteUrl}
>
Save changes
diff --git a/apps/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx b/apps/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx
index 9d5343a4301..f6116672150 100644
--- a/apps/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx
+++ b/apps/studio/components/interfaces/Auth/SmtpForm/SmtpForm.tsx
@@ -1,8 +1,8 @@
-import { yupResolver } from '@hookform/resolvers/yup'
+import { zodResolver } from '@hookform/resolvers/zod'
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
import { useEffect, useState } from 'react'
-import { useForm } from 'react-hook-form'
+import { SubmitHandler, useForm } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
@@ -23,7 +23,7 @@ import {
import { Admonition, PageSection, PageSectionContent } from 'ui-patterns'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
-import * as yup from 'yup'
+import * as z from 'zod'
import { urlRegex } from '../Auth.constants'
import { defaultDisabledSmtpFormValues } from './SmtpForm.constants'
@@ -35,16 +35,63 @@ import { useAuthConfigQuery } from '@/data/auth/auth-config-query'
import { useAuthConfigUpdateMutation } from '@/data/auth/auth-config-update-mutation'
import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
-interface SmtpFormValues {
- SMTP_ADMIN_EMAIL?: string
- SMTP_SENDER_NAME?: string
- SMTP_HOST?: string
- SMTP_PORT?: number
- SMTP_MAX_FREQUENCY?: number
- SMTP_USER?: string
- SMTP_PASS?: string
- ENABLE_SMTP: boolean
-}
+const smtpEnabledSchema = z.object({
+ ENABLE_SMTP: z.literal(true),
+ SMTP_ADMIN_EMAIL: z
+ .string()
+ .trim()
+ .min(1, 'Sender email address is required')
+ .email('Must be a valid email'),
+ SMTP_SENDER_NAME: z.string().trim().min(1, 'Sender name is required'),
+ SMTP_HOST: z
+ .string()
+ .trim()
+ .min(1, 'Host URL is required')
+ .regex(urlRegex({ excludeSimpleDomains: false }), 'Must be a valid URL or IP address'),
+ SMTP_PORT: z.preprocess(
+ (val) => (val === '' || val == null ? undefined : val),
+ z.coerce
+ .number({
+ required_error: 'Port number is required',
+ invalid_type_error: 'Port number is required',
+ })
+ .min(1, 'Must be a valid port number more than 0')
+ .max(65535, 'Must be a valid port number no more than 65535')
+ ),
+ SMTP_MAX_FREQUENCY: z.preprocess(
+ (val) => (val === '' || val == null ? undefined : val),
+ z.coerce
+ .number({
+ required_error: 'Rate limit is required',
+ invalid_type_error: 'Rate limit is required',
+ })
+ .min(1, 'Must be more than 0')
+ .max(32767, 'Must not be more than 32,767 an hour')
+ ),
+ SMTP_USER: z.string().trim().min(1, 'SMTP Username is required'),
+ SMTP_PASS: z.string().trim().optional(),
+})
+
+const smtpDisabledSchema = z.object({
+ ENABLE_SMTP: z.literal(false),
+ SMTP_ADMIN_EMAIL: z.string().optional(),
+ SMTP_SENDER_NAME: z.string().optional(),
+ SMTP_HOST: z.string().optional(),
+ SMTP_PORT: z.preprocess(
+ (val) => (val === '' || val == null ? undefined : val),
+ z.coerce.number().optional()
+ ),
+ SMTP_MAX_FREQUENCY: z.preprocess(
+ (val) => (val === '' || val == null ? undefined : val),
+ z.coerce.number().optional()
+ ),
+ SMTP_USER: z.string().optional(),
+ SMTP_PASS: z.string().optional(),
+})
+
+const smtpSchema = z.discriminatedUnion('ENABLE_SMTP', [smtpEnabledSchema, smtpDisabledSchema])
+
+type SmtpFormValues = z.infer<typeof smtpSchema>
export const SmtpForm = () => {
const { ref: projectRef } = useParams()
@@ -62,67 +109,20 @@ export const SmtpForm = () => {
'custom_config_gotrue'
)
- const smtpSchema = yup.object({
- SMTP_ADMIN_EMAIL: yup
- .string()
- .trim()
- .when('ENABLE_SMTP', {
- is: true,
- then: (schema) =>
- schema.email('Must be a valid email').required('Sender email address is required'),
- otherwise: (schema) => schema,
- }),
- SMTP_SENDER_NAME: yup
- .string()
- .trim()
- .when('ENABLE_SMTP', {
- is: true,
- then: (schema) => schema.required('Sender name is required'),
- otherwise: (schema) => schema,
- }),
- SMTP_HOST: yup
- .string()
- .trim()
- .when('ENABLE_SMTP', {
- is: true,
- then: (schema) =>
- schema
- .matches(urlRegex({ excludeSimpleDomains: false }), 'Must be a valid URL or IP address')
- .required('Host URL is required.'),
- otherwise: (schema) => schema,
- }),
- SMTP_PORT: yup.number().when('ENABLE_SMTP', {
- is: true,
- then: (schema) =>
- schema
- .required('Port number is required.')
- .min(1, 'Must be a valid port number more than 0')
- .max(65535, 'Must be a valid port number no more than 65535'),
- otherwise: (schema) => schema,
- }),
- SMTP_MAX_FREQUENCY: yup.number().when('ENABLE_SMTP', {
- is: true,
- then: (schema) =>
- schema
- .required('Rate limit is required.')
- .min(1, 'Must be more than 0')
- .max(32767, 'Must not be more than 32,767 an hour'),
- otherwise: (schema) => schema,
- }),
- SMTP_USER: yup
- .string()
- .trim()
- .when('ENABLE_SMTP', {
- is: true,
- then: (schema) => schema.required('SMTP Username is required'),
- otherwise: (schema) => schema,
- }),
- SMTP_PASS: yup.string().trim(),
- ENABLE_SMTP: yup.boolean().required(),
- })
-
const form = useForm<SmtpFormValues>({
- resolver: yupResolver(smtpSchema),
+ resolver: zodResolver(
+ smtpSchema.superRefine((data, ctx) => {
+ const isEnablingSmtp = data.ENABLE_SMTP && !isSmtpEnabled(authConfig)
+
+ if (isEnablingSmtp && !data.SMTP_PASS) {
+ ctx.addIssue({
+ code: 'custom',
+ message: 'SMTP Password is required',
+ path: ['SMTP_PASS'],
+ })
+ }
+ })
+ ),
defaultValues: {
SMTP_ADMIN_EMAIL: '',
SMTP_SENDER_NAME: '',
@@ -135,14 +135,12 @@ export const SmtpForm = () => {
},
})
+ const { isDirty } = form.formState
+
// Update form values when auth config is loaded
useEffect(() => {
if (authConfig) {
const formValues = generateFormValues(authConfig)
- // Convert SMTP_PORT from string to number if it exists
- if (formValues.SMTP_PORT) {
- formValues.SMTP_PORT = Number(formValues.SMTP_PORT) as any
- }
form.reset({
...formValues,
ENABLE_SMTP: isSmtpEnabled(authConfig),
@@ -161,7 +159,7 @@ export const SmtpForm = () => {
return () => subscription.unsubscribe()
}, [form])
- const onSubmit = (values: SmtpFormValues) => {
+ const onSubmit: SubmitHandler<SmtpFormValues> = (values) => {
const { ENABLE_SMTP, ...rest } = values
const basePayload = ENABLE_SMTP ? rest : defaultDisabledSmtpFormValues
@@ -377,7 +375,7 @@ export const SmtpForm = () => {
<Input_Shadcn_
type="number"
value={field.value}
- onChange={(e) => field.onChange(Number(e.target.value))}
+ onChange={(e) => field.onChange(e.target.value)}
placeholder="587"
disabled={!canUpdateConfig}
/>
@@ -399,7 +397,7 @@ export const SmtpForm = () => {
<FormInputGroupInput
type="number"
value={field.value}
- onChange={(e) => field.onChange(Number(e.target.value))}
+ onChange={(e) => field.onChange(e.target.value)}
disabled={!canUpdateConfig}
/>
<InputGroupAddon align="inline-end">
@@ -468,7 +466,7 @@ export const SmtpForm = () => {
</p>
))}
<div className="flex items-center gap-x-2">
- {form.formState.isDirty && (
+ {isDirty && (
<Button
type="default"
onClick={() => {
@@ -483,7 +481,7 @@ export const SmtpForm = () => {
type="primary"
htmlType="submit"
loading={isUpdatingConfig}
- disabled={!canUpdateConfig || !form.formState.isDirty}
+ disabled={!canUpdateConfig || !isDirty}
>
Save changes
</Button>
diff --git a/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx b/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx
index 53d8864751d..47cf34ca5e4 100644
--- a/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx
+++ b/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx
@@ -42,7 +42,7 @@ import {
BreadcrumbPage,
BreadcrumbSeparator,
} from 'ui/src/components/shadcn/ui/breadcrumb'
-import { z } from 'zod'
+import * as z from 'zod'
import { TEMPLATES_SCHEMAS } from '@/components/interfaces/Auth/AuthTemplatesValidation'
import { slugifyTitle } from '@/components/interfaces/Auth/EmailTemplates/EmailTemplates.utils'