admin管理员组

文章数量:1353001

Is it possible to add custom keys to the Zod ctx.addIssue that can then be retrieved in the Form.formState.errors from react-hook-form?

For example here I want to add "foo" as a custom entry:

MyConfig.superRefine((data, ctx) => {
            ctx.addIssue({
              ...issue,
              code: z.ZodIssueCode.custom,
              params: {
                context: "foo",
              },
              message: `${issue.message} | ${Module.label}`,
              path: [...pathPrefix, index],
            });
});

And then be able to retrieve that in the corresponding react-hook-form errors. But when I log the errors it seems like the whole params (or any other key I add for that matter) are gone:

console.log(Form.formState.errors);
// => {
    "some": {
        "path": [
            {
                "message": "Some error message",
                "type": "custom",
                "ref": {
                    "name": "config.some.path.0",
                    "value": {
                        ...the value
                    }
                }
            }
        ]
    },
}

Thank you!

本文标签: reactjsCustom error keys in reacthookform with zod (superRefine amp ctxaddIssue)Stack Overflow