-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with validation of identical fields in the schema #3752
Comments
You can switch const UserSchema = z.union([secondUser, firstUser]); Zod will iterate through all the options in a union array. If any of the options pass validation, the entire result is considered valid. However, if any of the options fail validation, it will return the result of the first invalid option and ignore the rest. For example, if you use |
@sunnylost it could be a working solution, but if we change the order of zod schemas in the array and then decide to add a property with the same key but with different validation schema, validation will raise error in the |
So the issue is that you want to use the |
Yes, you are right! We want to apply the schema to object depends on some key (for example, id from enum). Unfortunately, zod union do not support the determination of validation schema according to key property and its value. We have |
Currently, there is an issue in the schema where, if identical fields are present in the user's schemas, Zod incorrectly validates the second testProperty field. Is there a way to fix this? At the moment, the only solution I've found is to change the field name (testProperty) to a unique one.
https://codesandbox.io/p/sandbox/zodschemas-forked-76tlcr?file=%2Fsrc%2FApp.tsx
`import { useCallback, VFC } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const stringSchema = z.string().min(1, { message: "fieldIsRequired" });
const firstUser = z.object({
firstName: stringSchema,
lastName: stringSchema,
testProperty: stringSchema,
});
const secondUser = z.object({
firstName: stringSchema,
lastName: stringSchema,
testProperty: stringSchema.or(z.string().length(0)),
testPropertySecond: stringSchema.or(z.string().length(0)),
});
const UserSchema = z.union([firstUser, secondUser]);
const usersSchema = z.object({
users: UserSchema.array(),
});
type UsersForm = z.infer;
export const App: VFC = () => {
const { register, handleSubmit, formState, control } = useForm({
resolver: zodResolver(usersSchema),
defaultValues: {
users: [
{
firstName: "",
lastName: "",
testProperty: "",
},
{
firstName: "",
lastName: "",
testProperty: "",
testPropertySecond: "",
},
],
},
});
const errors = formState.errors;
console.log(errors, formState, control);
const onSubmit: SubmitHandler = useCallback(async (value) => {},
[]);
return (
<form
onSubmit={handleSubmit(onSubmit)}
style={{
display: "flex",
flexDirection: "column",
width: 512,
margin: "0 auto",
}}
>
First name first user
<input {...register("users.0.firstName")} />
{errors?.users?.[0]?.firstName?.message}
Last name first user
<input {...register("users.0.lastName")} />
{errors?.users?.[0]?.lastName?.message}
Test property first user
<input {...register("users.0.testProperty")} />
{errors?.users?.[0]?.testProperty?.message}
);
};
`
The text was updated successfully, but these errors were encountered: