const BookingSchema = z.object({
roomType: z.string(),
dueDate: z
.string({
message: "invalid due date",
})
.date('date format is wrong'),
numberOfGuests: z
.number()
.min(1, "there must be at least one guest")
.max(4, "at most 4 guests are allowed"),
price: z.number().positive("negative price, huh?"),
});
type Booking = z.infer<typeof BookingSchema>;
const validBooking: Booking = {
roomType: "suite",
dueDate: "2025-12-31",
numberOfGuests: 3,
price: 300,
};
try {
// BookingSchema.parse({ ...validBooking, numberOfGuests: 0 });
// BookingSchema.parse({ ...validBooking, price: -100 });
BookingSchema.parse({ ...validBooking, dueDate: "-100" });
} catch (e) {
if (e instanceof z.ZodError) {
for (const zodIssue of e.issues) {
console.error(zodIssue.code); // invalid_string
console.error(zodIssue.message); // date format is wrong
console.error(zodIssue.path); // ['dueDate']
}
}
}
标签:Typescript,Zod,validBooking,BookingSchema,error,zodIssue,message,dueDate From: https://www.cnblogs.com/Answer1215/p/18554996