import { z } from "zod"; import { AnalysisInputError } from "../domain/analysisErrorCore.js"; import { err, ok, type Result } from "../domain/result.js"; import { safeParseJson } from "../domain/safeJson.js"; /** Decode JSON text before the caller validates the option's value. */ export const cliJsonText = (invalidMessage: string) => z.string().transform((text, context) => { const parsed = safeParseJson(text); if (parsed.ok) return parsed.value; context.addIssue({ code: "custom", message: invalidMessage }); return z.NEVER; }); /** Decode one named CLI option before using the selected inputs. */ export const parseCliJsonOption = ( operation: string, name: string, text: string | undefined, message: string, ): Result => { if (text === undefined) return ok(undefined); const parsed = safeParseJson(text); return parsed.ok ? parsed : err( new AnalysisInputError(operation, { cause: parsed.cause }, [ { path: [name], reason: "invalid_format", expected: "JSON", message }, ]), ); };