I have an object which has a mandatory name
property, and contains nickname
or other
proprety. This is how my schema looks like,
export const studentSchema = z
.object({
name: z.string().min(1).max(20)
})
.and(
z
.object({
nickname: z.string().min(1).max(20)
})
.or(
z.object({
other: z.string().min(1).max(14)
})
)
)
The problem is, the validation passed when both nickname
and other
are passed
{
name: "John",
nickname: "any",
other: "play"
}
How can I fix the schema so that it only allow one of the properties in the object? For example,
{
name: "John",
nickname: "any"
}
or
{
name: "John",
other: "play"
}