Merge branch 'v2/main' into v2/fix/2055-ui-resource-meta-ui

This commit is contained in:
Cliff Hall 2026-08-23 22:51:16 -04:00 • committed by GitHub
commit 5eba105c67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3149 additions and 665 deletions

View file

@ -0,0 +1,65 @@
import { describe, it, expect } from "vitest";
import { parseJsonObjectDraft } from "./jsonObjectDraft";
describe("parseJsonObjectDraft", () => {
it("reads empty text as the empty object rather than an error", () => {
expect(parseJsonObjectDraft("")).toEqual({ ok: true, value: {} });
expect(parseJsonObjectDraft(" \n ")).toEqual({ ok: true, value: {} });
});
it("accepts a JSON object with values of any JSON type", () => {
const result = parseJsonObjectDraft(
'{"s":"a","n":1,"b":true,"z":null,"arr":[1,{"k":2}],"o":{"deep":{"er":1}}}',
);
expect(result).toEqual({
ok: true,
value: {
s: "a",
n: 1,
b: true,
z: null,
arr: [1, { k: 2 }],
o: { deep: { er: 1 } },
},
});
});
it.each([
["an overflowing literal", '{"n":1e400}'],
["a negative overflow", '{"n":-1e400}'],
["one nested in an array", '{"a":[1,1e400]}'],
])("rejects %s, which would be sent as null", (_label, text) => {
// `JSON.parse` accepts these; `JSON.stringify` then writes `null`, so the
// editor would show one value and the wire would carry another.
const result = parseJsonObjectDraft(text);
expect(result.ok).toBe(false);
expect(result.ok === false && result.error).toMatch(/finite/);
});
it("still accepts large-but-finite numbers", () => {
expect(parseJsonObjectDraft('{"n":1e308}')).toEqual({
ok: true,
value: { n: 1e308 },
});
});
it("rejects text that is not JSON", () => {
expect(parseJsonObjectDraft('{"a":')).toEqual({
ok: false,
error: "Not valid JSON — changes are not applied",
});
});
it.each([
["an array", "[1,2]"],
["a string", '"hello"'],
["a number", "42"],
["null", "null"],
])("rejects valid JSON that is not an object: %s", (_label, text) => {
const result = parseJsonObjectDraft(text);
expect(result.ok).toBe(false);
expect(result.ok === false && result.error).toMatch(
/Must be a JSON object/,
);
});
});

View file

@ -0,0 +1,64 @@
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 };
}