If I had a property that should be limited to only a few possible values, using Yup I could do something like:
prop: Yup.string().oneOf([5, 10, 15])
I can not seem to find something similar in Zod. Of course, I can do that validation in the following way:
const allowedValues = [5, 10, 15];
const schema = z.object({
prop: z.number().superRefine((val, ctx) => {
if (allowedValues.includes(val)) return true;
ctx.addIssue({
message: `${ctx.path} must be one of ${allowedValues}`,
code: "custom",
});
return false;
}),
});
But I was wondering if it could be done by writing less code.