Skip to content
0

katanakit-js / useReadJsonFile

Function: useReadJsonFile() ​

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

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

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

Type Parameters ​

Type ParameterDefault typeDescription
TunknownExpected parsed (and validated) shape.

Parameters ​

ParameterTypeDescription
pathstringFile path.
schema?ZodType<T, unknown, $ZodTypeInternals<T, unknown>>Optional Zod schema; invalid shapes fail with ERR_VALIDATION.

Returns ​

Promise<FilesystemResult<T>>

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

Example ​

ts
import { z } from "zod";

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

Released under the MIT License.