Closed KolbySisk closed 4 years ago
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 .default
function in Zod 2: https://github.com/vriad/zod/tree/v2 (now in beta)
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:
formSchema.optional().parse({})
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?