saj
saj
TTCTheo's Typesafe Cult
Created by saj on 2/19/2024 in #questions
Generate a dynamic zod schema for a user created form
If a user is creating a form we output this:
{
"sections": [
{
"id": "8fa76f1e-4ba8-4077-9265-e10f4c5f2be8",
"title": "Example Title",
"fields": [
{
"id": "85354302-d313-4c28-93d9-acfa2ef4b2aa",
"type": "textfield",
"title": "First Name",
"default": true,
"private": false,
"required": true,
"response": "",
},
{
"id": "51921ab2-8fe4-40ef-a2ec-140c6658533a",
"type": "textfield",
"title": "Last Name",
"default": true,
"private": false,
"required": true,
"response": "",
},
{
"id": "6e274a04-d63f-4913-8b3b-0a0d66aa59bf",
"type": "email",
"title": "Email Address",
"default": true,
"private": false,
"required": true,
"response": "",
},
{
"id": "78e25934-abe7-4404-b863-f0358329982f",
"type": "textfield",
"title": "Short Text",
"default": false,
"private": false,
"required": false,
"response": "",
}
],
}
]
}
{
"sections": [
{
"id": "8fa76f1e-4ba8-4077-9265-e10f4c5f2be8",
"title": "Example Title",
"fields": [
{
"id": "85354302-d313-4c28-93d9-acfa2ef4b2aa",
"type": "textfield",
"title": "First Name",
"default": true,
"private": false,
"required": true,
"response": "",
},
{
"id": "51921ab2-8fe4-40ef-a2ec-140c6658533a",
"type": "textfield",
"title": "Last Name",
"default": true,
"private": false,
"required": true,
"response": "",
},
{
"id": "6e274a04-d63f-4913-8b3b-0a0d66aa59bf",
"type": "email",
"title": "Email Address",
"default": true,
"private": false,
"required": true,
"response": "",
},
{
"id": "78e25934-abe7-4404-b863-f0358329982f",
"type": "textfield",
"title": "Short Text",
"default": false,
"private": false,
"required": false,
"response": "",
}
],
}
]
}
how do i generate a schema to use in the react-hook-form zod resolver to validate the fields? notice how we have sections->fields, and each field has a "required" which indicates true/false, and then "response" is where we capture the response. we generate a different field object to represent required/optional based on the "type" of the field, e.g. "textfield" is a basic zod object string i've tried several things
2 replies
TTCTheo's Typesafe Cult
Created by saj on 8/7/2023 in #questions
How do you revalidateTag or revalidatePath with a server component prisma fetch or server action?
I am not using next.js fetch out of the box, just a prisma get. How do I revalidate it with a tag or even the whole path when I submit a form? Next.js examples are all based on the fetch() and/or api route.
4 replies
TTCTheo's Typesafe Cult
Created by saj on 4/26/2023 in #questions
Question about Prisma Joins and Performance
I'm starting to stack multiple "joins" with prisma but am getting concerned with the approach. I read that joins in general may not be great for performance so that this could be a plus, but maybe I misread or am making that up. What is the consensus on Prisma's way of handling joins or the idea that raw query is not type safe? Is anyone regularly stacking joins with Prisma and have any pro tips? Example:
getSomething: publicProcedure
.input(inputSchema)
.query(async ({ input, ctx }): Promise<SomethingInterface[]> => {
const something = await ctx.prisma.somethings.findMany({
where: {
id: input.id,
},
select: {
value: true,

TableB1: {
select: {
value: true,

TableB2: {
select: {
value: true,
},
},
},
},
TableC: {
select: {
value: true,
},
},
TableD1: {
select: {
value: true,
TableD2: {
select: {
value: true,
},
},
},
},
},
take: 1000,
});
getSomething: publicProcedure
.input(inputSchema)
.query(async ({ input, ctx }): Promise<SomethingInterface[]> => {
const something = await ctx.prisma.somethings.findMany({
where: {
id: input.id,
},
select: {
value: true,

TableB1: {
select: {
value: true,

TableB2: {
select: {
value: true,
},
},
},
},
TableC: {
select: {
value: true,
},
},
TableD1: {
select: {
value: true,
TableD2: {
select: {
value: true,
},
},
},
},
},
take: 1000,
});
11 replies