@@ -124,6 +124,25 @@ export interface IJsonFileParseOptions {
124124 */
125125export interface IJsonFileLoadAndValidateOptions extends IJsonFileParseOptions , IJsonSchemaValidateOptions { }
126126
127+ /**
128+ * A structural validator interface that matches the parse/safeParse contract of
129+ * popular schema libraries such as zod.
130+ *
131+ * @remarks
132+ * `JsonFile.loadAndParse()` accepts any object whose `parse()` method takes an
133+ * `unknown` input and returns a typed value (throwing on validation failure).
134+ * Using a structural type here avoids forcing `node-core-library` to take a
135+ * runtime dependency on a specific schema-validation library or major version.
136+ *
137+ * @public
138+ */
139+ export interface IJsonFileTypeValidator < T > {
140+ /**
141+ * Validate `input` and return it as the validated type, or throw if validation fails.
142+ */
143+ parse ( input : unknown ) : T ;
144+ }
145+
127146/**
128147 * Options for {@link JsonFile.stringify}
129148 *
@@ -319,6 +338,45 @@ export class JsonFile {
319338 return jsonObject ;
320339 }
321340
341+ /**
342+ * Loads a JSON file and validates it using a structural validator (such as a
343+ * zod schema), returning the strongly-typed result.
344+ *
345+ * @remarks
346+ * `validator` is any object exposing a `parse(input: unknown): T` method.
347+ * The validator is responsible for both runtime validation and the resulting
348+ * TypeScript type. This indirection lets `node-core-library` accept zod
349+ * schemas without taking a runtime dependency on zod.
350+ *
351+ * @example
352+ * ```ts
353+ * import { z } from 'zod';
354+ * const schema = z.object({ name: z.string() });
355+ * const data = JsonFile.loadAndParse('config.json', schema);
356+ * // data is typed as { name: string }
357+ * ```
358+ */
359+ public static loadAndParse < T > (
360+ jsonFilename : string ,
361+ validator : IJsonFileTypeValidator < T > ,
362+ options ?: IJsonFileParseOptions
363+ ) : T {
364+ const jsonObject : JsonObject = JsonFile . load ( jsonFilename , options ) ;
365+ return validator . parse ( jsonObject ) ;
366+ }
367+
368+ /**
369+ * An async version of {@link JsonFile.loadAndParse}.
370+ */
371+ public static async loadAndParseAsync < T > (
372+ jsonFilename : string ,
373+ validator : IJsonFileTypeValidator < T > ,
374+ options ?: IJsonFileParseOptions
375+ ) : Promise < T > {
376+ const jsonObject : JsonObject = await JsonFile . loadAsync ( jsonFilename , options ) ;
377+ return validator . parse ( jsonObject ) ;
378+ }
379+
322380 /**
323381 * Serializes the specified JSON object to a string buffer.
324382 * @param jsonObject - the object to be serialized
0 commit comments