File size: 500 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { createLoggerWithContext } from "@midday/logger";

const logger = createLoggerWithContext("api");

import type { ZodSchema } from "zod";

export const validateResponse = <T>(data: any, schema: ZodSchema<T>): T => {
  const result = schema.safeParse(data);

  if (!result.success) {
    const cause = result.error.flatten();

    logger.error("Response validation failed", { cause });

    throw new Error(`Response validation failed: ${JSON.stringify(cause)}`);
  }

  return result.data;
};