I am following the React for Beginners course and working on form validation with Zod. The “name” filed should be required and is because of this code
const schema = z.object({
name: z.string().min(3, { message: “Name must be at least 3 characters” }),
age: z
.number({ invalid_type_error: “Age field is required” })
.min(18, { message: “Age must be at least 18” }),
});
If I do this:
const schema = z.object({
name: z.string(),
age: z
.number({ invalid_type_error: “Age field is required” })
.min(18, { message: “Age must be at least 18” }),
});
When I run the form, I don’t get a message that the name field is required. According to the Zod documents, all fields are required unless you specify the .required modifier. How can I make any filed required using Zod and display the correct message without using another modifier such as min()?
I am using the latest versions of React(18) and Zod and Typescript
Thanks,
Jim