Skip to content

UseReadJsonFile

katanakit-js / useReadJsonFile

function useReadJsonFile<T>(path, schema?): Promise<FilesystemResult<T>>;

Defined in: infrastructure/filesystem/filesystem.service.ts:282

Reads and parses a JSON file, optionally validating it with a Zod schema.

Type Parameter Default type Description
T unknown Expected parsed (and validated) shape.
Parameter Type Description
path string File path.
schema? ZodType<T, unknown, $ZodTypeInternals<T, unknown>> Optional Zod schema; invalid shapes fail with ERR_VALIDATION.

Promise<FilesystemResult<T>>

Safe Result with the parsed data, or ERR_INVALID_JSON / ERR_VALIDATION.

import { z } from "zod";
const BookSchema = z.object({ title: z.string() });
const result = await useReadJsonFile("data/books.json", BookSchema);