Skip to content

Map refined error to multiple fields #2041

Answered by JacobWeisenburger
p-runge asked this question in Q&A
Discussion options

You must be logged in to vote
MySchema.superRefine(({ product: { pages }, positions }, ctx) => {
    positions.forEach(({ page }, index) => {
      if (page > pages) {
        ctx.addIssue({
          type: "number",
          code: z.ZodIssueCode.too_big,
          maximum: pages,
          inclusive: true,
          message: `Product has only ${pages} pages`,
          path: ["positions", index, "page"],
        });
      }
    });
  });

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@p-runge
Comment options

Comment options

You must be logged in to vote
1 reply
@p-runge
Comment options

Comment options

You must be logged in to vote
0 replies
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
3 participants