-
-
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
Generating an object from schema #62
Comments
Looking for this too kinda similar to YUP’s cast function. |
This will eventually be achievable with Zod transformations: #100 |
This is possible with the const formSchema = z.object({
name: z.string().default(''),
email: z.string().default(''),
});
type FormState = z.infer<typeof formSchema>;
const initialForm = formSchema.parse({});
console.log(initialForm);
const [formState, setFormState] = useState<FormState>(initialForm); |
@colinhacks Im quite late to this threat, But doesn't this defeat te purpose of validating the object afterwards? so for example i want to create an form with name, email like above const formSchema = z.object({
name: z.string().default(''),
email: z.string().email().default(''),
});
type FormState = z.infer<typeof formSchema>;
const initialForm = formSchema.parse({});
const submitForm = async () => {
try {
formSchema.parse(initialForm)
// handle submit
} catch (e) {
// Handle form errors
}
} |
Also need this, ability to create an empty object with the validation intacted. |
I also need this, as I would like to use invalid default values and .parse({}) crashes the app (as validation fails, as expected), instead of creating the default values. Proposed API: formSchema.create() |
Was just looking for something like this as well, after realizing I am duplicating very similar code. |
How about:
|
Has this become a feature yet? I ran into an error with the schadcn-ui form which requires defaults and I would love to pull said defaults off my scheme. thanks! |
same thoughts as @senpro-ingwersenk , is there a solution to this today? |
This may not be a feature that makes sense to build, but it would be incredibly useful.
A normal workflow is to create a schema for your form state, generate a type, and then write the same object over again to use as the initial form state:
And now as your form grows so do these 2 objects.
I would love to be able to do something like this:
Currently I have a utility function I wrote that does this, it's not recursive and not very robust, but it does what I need and as a result my code is much cleaner. Everything can be updated simply by changing the Zod schema. Any thoughts on a feature like this?
The text was updated successfully, but these errors were encountered: