import type { z } from "zod"; import type { StateUpdateResult } from "../types/state"; import { shallowMerge } from "../types/state"; /** * Update state with a partial patch. * Validates the result against the schema. */ export function updateState( currentState: S, patch: Partial, validator: z.ZodType ): StateUpdateResult { // Shallow merge the patch into current state const merged = shallowMerge( currentState as Record, patch as Record ) as S; // Validate the result const result = validator.safeParse(merged); if (!result.success) { return { success: false, state: currentState, error: result.error.message, }; } return { success: true, state: result.data, }; }