Skip to content

How to make a schema from dynamic and static fields #3287

Answered by mr-scrpt
mr-scrpt asked this question in Q&A
Discussion options

You must be logged in to vote

So far, I've solved my problem this way. In a schema that contains fields that are precisely defined, I added the propertyList field

export const productFormSchema = z.object({
  name: z
    .string()
    .min(3)
    .max(30)
    .transform((name) => name.trim()),
  description: z.string(),
  about: z.string(),
  img: z.array(z.string()),
  categoryList: z.array(
    z.object({
      id: z.string(),
      name: z.string(),
    }),
  ),
  propertyList: z.object({}),
});

export type ProductFormValues = z.infer<typeof productFormSchema>;

Next I create a schema for the dynamic fields, based on the data

  const dynamicOptionSchema: Record<string, z.ZodType<any, any>> = {};

  for (const opti…

Replies: 8 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@tiguere
Comment options

@sergey-shch
Comment options

@sergey-shch
Comment options

Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #3267 on February 29, 2024 14:50.