// shared/parse.ts — Schema-validated JSON parsing (replaces unsafe `as` casts) // biome-ignore-all lint/plugin: parse implementations require raw try/catch around JSON.parse import * as v from "valibot"; import { isPlainObject } from "./type-guards"; /** * Parse a JSON string and validate it against a valibot schema. * Returns the validated value, or null if parsing/validation fails. */ export function parseJsonWith>>( text: string, schema: T, ): v.InferOutput | null { try { return v.parse(schema, JSON.parse(text)); } catch { return null; } } /** * Parse a JSON string and return it as a Record or null. * Rejects non-object results (arrays, primitives). * Use for API responses that are always a JSON object. */ export function parseJsonObj(text: string): Record | null { try { const val: unknown = JSON.parse(text); if (isPlainObject(val)) { return val; } return null; } catch { return null; } }