-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
partial() doesn't work #798
Comments
I'm not familiar with this hook, but are you certain it's not trying to pass From our perspective, your code does work as intended: import test from "tape";
import { z } from "zod";
export const userLoginSchema = z.object({
email: z.string().email(),
password: z.string().min(3).max(20)
});
export const userRegisterSchema = userLoginSchema.extend({
name: z.string().min(3).max(15),
username: z.string().min(3).max(15)
});
export const userUpdateSchema = userRegisterSchema
.extend({
avatar: z.any()
})
.omit({ email: true })
.partial();
test("valid", (t) => {
t.true(userUpdateSchema.safeParse({}).success); // this succeeds
t.end();
});
test("invalid", (t) => {
t.false(userUpdateSchema.safeParse({ password: "" }).success); // this fails
t.end();
}); Closing as this appears to be more of an issue with integrating Zod into your form validation tool than any problem with Zod itself. Feel free to follow up here if you get something working or have any other questions that we might be able to help with from our side. |
In React text fields are never bound to undefined. In #310 I saw workaround for |
Zod is usable with React, but I think it takes some understanding of how it (and other runtime type systems like it) work. For forms, the concept of "required" or "optional" typically refers to having a non-empty value, where TypeScript considers For your use case, without knowing much about Updated the CodeSandbox as well const emptyStringToUndefined = (data: unknown) =>
Object.entries(data as Record<string, string>).reduce(
(acc, [key, value]) => ({
...acc,
[key]: value === "" ? undefined : value
}),
{}
);
export const userUpdateSchema = z.preprocess(
emptyStringToUndefined,
userRegisterSchema.extend({ avatar: z.any() }).omit({ email: true }).partial()
); |
What you wrote makes sense, but it is also relevant that library has elegant api for very common use case such is usage with the most popular React form library because a lot of people will try to use it for such purpose and have hard time with it. Maybe |
To people who use react-hook-form. Adding |
I absolutely agree that easing integration with things like react-hook-form is very important, but we have to be careful about what zod provides to ensure that it is:
To that end Up to this point, we've really put the requirement on the library to find ways to integrate with zod, but I hope that we can be more proactive and helpful with troubleshooting and contributing to these projects. I don't think there is much zod "core" needs to do, but there is a layer of abstraction between zod the runtime type-system and validation layers for things like forms, JSON, query strings, CSV parsing, etc. and I think we can try to be more actively helpful in those areas. Having said that, for now, this is a problem that is best served by addressing with the react-hook-form team. I'm happy to help and contribute to that project to help smooth over bumps like this!
Other than export const stringyNillable = z.union([z.undefined(), z.null(), z.literal(''), z.string()]);
stringyNillable.parse(undefined); // passes
stringyNillable.parse(null); // passes
stringyNillable.parse(""); // passes
stringyNillable.parse("a non empty string"); // passes You could make this a bit more generic and use it to create a |
…apter "Unions" This is based on colinhacks#798 (comment).
I use
userUpdateSchema
to make all fields optional and pass undefinedpassword
field but keep gettingShould be at least 3 characters
forpassword
field. The schemas are bellow:The text was updated successfully, but these errors were encountered: