I have React app where I get list of objects from server. I use them to populate react-select.
interface Country {
code: string;
name: string;
}
What I want to do using Zod is to check if user selected something from Select, so probably just to validate if object is not null and defined, but not necessarily validate fields of that object. I tried something like this but it does not work.
const schema = z
.object({
//Other fields
country: z.object("Country required")
})
If it needs to be an object, but there are no required properties on it, then you can do that with z.object({})
const schema = z.object({
// Other fields
country: z.object({})
});
Note that if you use the schema to parse something, it will throw out any excess properties, and in this case they're all excess properties.
const val = {
country: {
name: 'Zanzibar',
}
}
try {
const parsed = schema.parse(val);
// parsed is: { country: {} }. The name property has been removed.
} catch (error) {
// Doesn't get here because it's valid
}
If you need to keep those properties, consider using .passthrough