Just getting used to trpc. Say I have a standard procedure:
update: t.procedure
.input(z.object({
id: z.string(),
type: z.string(),
content: z.object({ title: z.string() }).nullish()
}).nullish())
Now what I am interested in is the content
attribute. In my mysql schema, that content
field is a JSON field. Depending on the type
field, it will contain different values.
For example. If type
is blogPost
, it will contain attributes like title
& category
, but if the type
is set to landingPage
those attributes will change. (just an example to make it more straightforward, my use case isn't about blogPosts etc.)
As far as I understand, I can solve this with discriminatedUnion
:
z.discriminatedUnion("type", [
z.object({ type: z.literal("a"), a: z.string() }),
z.object({ type: z.literal("b"), b: z.string() }),
])
.parse({ type: "a", a: "abc" });
but i am not sure how to apply this to my case? I want it to only apply to the content field and keep the other attributes the same.