Skip to content

Commit eb53116

Browse files
committed
feat: support zod effects and update password token prop
1 parent f63f2bf commit eb53116

File tree

6 files changed

+82
-10
lines changed

6 files changed

+82
-10
lines changed

.changeset/afraid-maps-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@saas-ui/forms': minor
3+
---
4+
5+
Added support for ZodEffects schema to Form

.changeset/green-fishes-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@saas-ui/auth-provider': patch
3+
---
4+
5+
Added optional token prop to update password handler

packages/saas-ui-auth-provider/src/provider.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ export interface AuthParams {
2525

2626
export interface ResetPasswordParams {
2727
email: string
28+
[key: string]: any
2829
}
2930

3031
export interface UpdatePasswordParams {
3132
password: string
33+
token?: string
34+
[key: string]: any
3235
}
3336

3437
export interface OtpParams {
3538
otp: string
39+
[key: string]: any
3640
}
3741

3842
export type ExtraAuthOptions = Record<string, unknown>

packages/saas-ui-auth-provider/tests/provider.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ const createAuthService = (): AuthProviderProps => {
4949

5050
throw new Error('Sign up failed')
5151
},
52+
onResetPassword: async (params) => {
53+
const { email } = params
54+
return { email }
55+
},
56+
onUpdatePassword: async (params) => {
57+
const { password, token } = params
58+
return { password, token }
59+
},
5260
onGetToken: async () => {
5361
return token
5462
},

packages/saas-ui-forms/stories/form.stories.tsx

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { LuInfo } from 'react-icons/lu'
2222
import { splitProps } from '@saas-ui/core/utils'
2323

2424
import { createYupForm } from '../yup/src'
25-
import { createZodForm } from '../zod/src'
25+
import { createZodForm, zodMeta } from '../zod/src'
2626
import { JTDDataType, createAjvForm } from '../ajv/src'
2727

2828
import {
@@ -55,15 +55,31 @@ export default {
5555

5656
type Story = StoryObj<typeof Form>
5757

58-
const loginSchema = yup.object({
59-
email: yup.string().email().required().label('Email address'),
60-
password: yup
61-
.string()
62-
.required()
63-
.label('Password')
64-
.meta({ type: 'password' }),
58+
const loginSchema = z.object({
59+
email: z.string().email().describe('Email address'),
60+
password: z.string().describe(
61+
zodMeta({
62+
label: 'Password',
63+
type: 'password',
64+
})
65+
),
6566
})
6667

68+
const signupSchema = z
69+
.object({
70+
email: z.string().email().describe('Email address'),
71+
password: z.string().describe('Password').min(8),
72+
confirmPassword: z.string().describe('Confirm password'),
73+
})
74+
.superRefine((data, ctx) => {
75+
if (data.password !== data.confirmPassword) {
76+
ctx.addIssue({
77+
code: z.ZodIssueCode.custom,
78+
message: 'Passwords do not match',
79+
})
80+
}
81+
})
82+
6783
export const Basic: Story = {
6884
render() {
6985
return (
@@ -266,6 +282,35 @@ export const WithZodSchema: StoryObj<typeof ZodForm> = {
266282
},
267283
}
268284

285+
export const ZodSuperRefine: StoryObj<typeof ZodForm> = {
286+
render(props) {
287+
return (
288+
<ZodForm
289+
schema={signupSchema}
290+
defaultValues={{
291+
email: '',
292+
password: '',
293+
confirmPassword: '',
294+
}}
295+
onSubmit={props?.onSubmit || onSubmit}
296+
>
297+
{({ Field }) => (
298+
<FormLayout>
299+
<Field name="email" label="Email" />
300+
<Field name="password" label="Password" type="password" />
301+
<Field
302+
name="confirmPassword"
303+
type="password"
304+
label="Confirm password"
305+
/>
306+
<SubmitButton>Sign up</SubmitButton>
307+
</FormLayout>
308+
)}
309+
</ZodForm>
310+
)
311+
},
312+
}
313+
269314
const YupForm = createYupForm({
270315
fields: { custom: CustomField },
271316
getBaseField,

packages/saas-ui-forms/zod/src/create-zod-form.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ export interface CreateZodFormProps<
1818
resolverOptions?: ResolverArgs[2]
1919
}
2020

21+
type InferObjectSchema<T extends z.ZodTypeAny | z.ZodEffects<z.ZodTypeAny>> =
22+
T extends z.ZodEffects<infer TSchema> ? z.infer<TSchema> : z.infer<T>
23+
2124
export type ZodFormType<
2225
FieldDefs,
2326
ExtraProps = object,
2427
ExtraFieldProps extends object = object,
2528
ExtraOverrides = object,
2629
Type extends 'zod' = 'zod',
2730
> = (<
28-
TSchema extends z.AnyZodObject = z.AnyZodObject,
29-
TFieldValues extends z.infer<TSchema> = z.infer<TSchema>,
31+
TSchema extends
32+
| z.AnyZodObject
33+
| z.ZodEffects<z.AnyZodObject> = z.AnyZodObject,
34+
TFieldValues extends InferObjectSchema<TSchema> = InferObjectSchema<TSchema>,
3035
TContext extends object = object,
3136
>(
3237
props: WithFields<

0 commit comments

Comments
 (0)