Is there a built-in way to prevent ID duplicates using Zod?

Let's say that your data is an array of objects with "id" and "name" fields:
[
{
id: 1,
name: "Joe"
},
{
id: 1,
name: "Schmoe"
}
]
[
{
id: 1,
name: "Joe"
},
{
id: 1,
name: "Schmoe"
}
]
as you can see, the id is duplicated but it should be unique. Does Zod have a built-in method to validate against this kind of issue? I wasn't able to find any. Or am I forced to use .refine and create a new Set with all the ids in my data?
3 Replies
upio
upio4mo ago
GitHub
Validate items uniqueness in z.array · colinhacks zod · Discussio...
I was checking the docs to see if there was any way to validate if there was any duplicate items on an array, perhaps we could implement a unique() function that does just that for arrays? const sc...
Muhct
MuhctOP4mo ago
yup I saw that discussion but no one mentions a built-in way to prevent duplicates, just workarounds that would be last resource for me
upio
upio4mo ago
there are no built in ways to do that
Want results from more Discord servers?
Add your server