FetchWithValidation

Function: FetchWithValidation()

FetchWithValidation<T>(schema, url): Promise<SafeParseReturnType<T, T>>

Defined in: src/lib/fetchWithValidation.tsx:20

Fetches data from a specified URL and validates it against a provided Zod schema.

Type Parameters

T

T

The type of the data to be validated.

Parameters

schema

ZodType<T>

The Zod schema to validate the fetched data against.

url

string

The URL to fetch data from.

Returns

Promise<SafeParseReturnType<T, T>>
  • A promise that resolves to a SafeParseReturnType containing the validated data or an error.

Remarks

If the fetch request fails (response.ok is false), the function will return an error object with the message from the backend response. The backend should ensure that error messages don't contain sensitive information.