inspector/clients/web/src/utils/jsonObjectDraft.ts
cliffhall 7f5992081e fix: reject non-finite numbers before they reach _meta
`StrictJsonValue` removed `undefined`, but the type cannot express the rest of
the gap: TypeScript has one `number`, and JSON has no encoding for `NaN` or
`±Infinity`, so `JSON.stringify` rewrites them to `null`.

This is reachable from ordinary input, not a careless caller.
`JSON.parse('{"a":1e400}')` returns `Infinity` on overflow — so text a user
types parses cleanly, satisfies the type, and reaches the server as `null`
while the editor still shows `1e400`.

`isSerializableJson` in `core/json` is the runtime half of `StrictJsonValue`,
applied at both boundaries that turn text into metadata:

- the editor's draft parser, which now reports "Numbers must be finite" inline
  and withholds the value rather than emitting one it cannot send;
- the CLI's `--metadata` / `--tool-metadata` parser, which throws. Silently
  sending `null` would be wrong, and so would falling back to the literal
  string — neither is what was asked for.

Tests cover the validator directly (including that `JSON.stringify` really does
write `null`, which is the reason it matters), the editor parser, and the CLI
flag.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0177mPHAdECD18nLLCTwR5rh
Signed-off-by: cliffhall <cliff@futurescale.com>
2026-08-23 19:43:39 -04:00

64 lines
2.3 KiB
TypeScript

import type { StrictJsonObject } from "@inspector/core/json/jsonUtils.js";
import { isSerializableJson } from "@inspector/core/json/jsonUtils.js";
/**
* What a JSON-object editor's draft text currently means: the object to emit,
* or why it cannot be emitted.
*/
export type JsonObjectDraft =
| { ok: true; value: StrictJsonObject }
| { ok: false; error: string };
/**
* A plain JSON object, i.e. not an array and not a scalar.
*
* The `StrictJsonObject` narrowing is sound because the only caller parses with
* `JSON.parse`, whose output cannot contain `undefined` — the value that
* separates the strict type from `JsonValue`.
*/
export function isJsonObject(value: unknown): value is StrictJsonObject {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
/**
* Read a JSON-object editor's draft text.
*
* Empty text is the object `{}` rather than an error — clearing the box is how
* "send nothing" is spelled, and an empty editor wearing a red error would read
* as broken.
*
* The two failure modes are reported separately because they are different
* mistakes: text that is not JSON at all is usually mid-edit, while text that
* is valid JSON but not an object (`[1,2]`, `"a"`, `42`) is a shape the caller
* cannot use no matter how finished it is.
*
* Lives in `utils` rather than beside its component because it is a pure
* transform — and because exporting it from the component's module trips
* `react-refresh/only-export-components`.
*/
export function parseJsonObjectDraft(text: string): JsonObjectDraft {
if (text.trim() === "") return { ok: true, value: {} };
let parsed: unknown;
try {
parsed = JSON.parse(text);
} catch {
return { ok: false, error: "Not valid JSON — changes are not applied" };
}
if (!isJsonObject(parsed)) {
return {
ok: false,
error: "Must be a JSON object (`{ … }`) — changes are not applied",
};
}
// `JSON.parse` accepts numeric literals it cannot represent: `1e400` parses
// to `Infinity`, which `JSON.stringify` then writes as `null`. Accepting it
// here would show the user one value and send another.
if (!isSerializableJson(parsed)) {
return {
ok: false,
error:
"Numbers must be finite — a value like `1e400` overflows and would be sent as null",
};
}
return { ok: true, value: parsed };
}