import type { z } from 'zod'; export type AssertUniqueType> = z.ZodEffects>, T[], unknown>; export class AssertUnique> { itemSchema: z.ZodSchema; uniqueValues?: (keyof T)[]; constructor(itemSchema: z.ZodSchema, uniqueValues?: (keyof T)[]) { this.itemSchema = itemSchema; this.uniqueValues = uniqueValues; } fromSchema() { return AssertUnique.fromSchema(this.itemSchema, this.uniqueValues) satisfies AssertUniqueType; } static fromSchema>( itemSchema: z.ZodSchema, uniqueValues?: (keyof T)[] ) { return itemSchema.array().refine((val) => { for (const key of uniqueValues ?? []) { const values = val.map((item) => item[key as keyof T]); return values.length === new Set(values).size; } }, { message: "Encountered duplicate values in unique field" }) satisfies AssertUniqueType; } }