TRPC good practice, single schema, multiple similar updates.
I'm using TRPC and zod.
I have two similar forms which update the user object in my database.
On one I want to update firstName and lastName.
On the other I want to update firstName, lastName and email.
Should these two different updates be their own separate functions or should they be one which can share a schema.
2 Replies
I would like to server side have one schema which says the ID is required and any of the update fields are optional.
However I'm using Mantine forms on the frontend and have previously shared a schema.
Therefore having the email as optional isn't valid as an email is always needed.
Can't you call the zod schema as a function and pass a parameter
isEdit: boolean
which is then used in .refine()
to determine if the email field should be required or not. I believe something like that should work. Quick example:
I haven't tested it but it looks like it should work. Here is the documentation on refine - https://zod.dev/?id=refine . You might also have to use superRefine.