Files
mongo-validate/actions/constrained.ts
2024-01-28 21:03:19 -06:00

18 lines
435 B
TypeScript

import { z } from 'zod';
export class AssertConstrained<T extends Record<string, unknown>> {
itemSchema: z.ZodSchema<T>;
constructor(itemSchema: z.ZodSchema<T>) {
this.itemSchema = itemSchema;
}
check(item: unknown): item is T {
return this.itemSchema.safeParse(item).success;
}
checkArray(arr: unknown[]): arr is T[] {
return this.itemSchema.array().safeParse(arr).success;
}
}