Enterprise-CMCS/macpro-mako

View on GitHub
react-app/src/zodIdValidator.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { z, SuperRefinement } from "zod";

export const validId = (
  idRegex: RegExp,
  message: string,
): SuperRefinement<string> => {
  const correctFormatSchema = z.string().regex(idRegex, {
    message,
  });

  return (val, ctx) => {
    const result = correctFormatSchema.safeParse(val);

    if (!result.success) {
      ctx.addIssue({
        message: result.error.issues.at(0)?.message,
        code: z.ZodIssueCode.custom,
        fatal: true,
      });
    }
  };
};