Theo's Typesafe CultTTC
Theo's Typesafe Cult2y ago
4 replies
Muhct

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"
    }
]


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?
Was this page helpful?