Skip to content

Commit 39df5c3

Browse files
fix(ui): accept scientific notation in config form number coercion
The decimal grammar now mirrors the HTML valid floating-point number grammar: scientific spellings (1e5, -2.5E-3, .5e2) coerce exactly as Number() reads them, while radix and JS-only spellings (0x10, 0b1010, 0o17, 1_000, Infinity, NaN, leading +, 1e, e5) still stay strings for schema validation to reject. Rejecting 1e5 was a compatibility break with the HTML number input grammar and standard Number() spelling. Co-Authored-By: Claude <[email protected]>
1 parent 32c7ddf commit 39df5c3

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎ui/src/components/config-form.browser.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe("config form renderer", () => {
124124
expect(onPatch).toHaveBeenCalledWith(["bind"], "tailnet");
125125
});
126126

127-
it("keeps nondecimal spellings typed into plain number inputs as strings", () => {
127+
it("coerces decimal and scientific spellings typed into plain number inputs", () => {
128128
const onPatch = vi.fn();
129129
const container = document.createElement("div");
130130
const analysis = analyzeConfigSchema({
@@ -152,12 +152,11 @@ describe("config form renderer", () => {
152152
const portInput = expectElement(numberInputs[0], "integer field input");
153153
const ratioInput = expectElement(numberInputs[1], "number field input");
154154

155-
// The HTML number grammar accepts 1e5-style spellings; the typed text must
156-
// reach the form state verbatim so schema validation can reject it instead
157-
// of a bare Number() silently persisting 100000.
155+
// Scientific notation is part of the HTML number grammar and the standard
156+
// Number() spelling, so it coerces like any plain decimal.
158157
portInput.value = "1e5";
159158
portInput.dispatchEvent(new Event("input", { bubbles: true }));
160-
expect(onPatch).toHaveBeenCalledWith(["port"], "1e5");
159+
expect(onPatch).toHaveBeenCalledWith(["port"], 100000);
161160

162161
portInput.value = "42";
163162
portInput.dispatchEvent(new Event("input", { bubbles: true }));
@@ -172,6 +171,10 @@ describe("config form renderer", () => {
172171
ratioInput.dispatchEvent(new Event("input", { bubbles: true }));
173172
expect(onPatch).toHaveBeenCalledWith(["ratio"], 42.5);
174173

174+
ratioInput.value = "-2.5E-3";
175+
ratioInput.dispatchEvent(new Event("input", { bubbles: true }));
176+
expect(onPatch).toHaveBeenCalledWith(["ratio"], -0.0025);
177+
175178
ratioInput.value = "";
176179
ratioInput.dispatchEvent(new Event("input", { bubbles: true }));
177180
expect(onPatch).toHaveBeenCalledWith(["ratio"], undefined);

‎ui/src/components/config-form.shared.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ export function defaultValue(schema?: JsonSchema): unknown {
5858
}
5959

6060
// openclaw.json stores plain decimals; a bare Number() would also reinterpret
61-
// 0x10/0b1010/1e3 spellings and silently persist a value that differs from the
62-
// typed text. Non-matches stay strings so schema validation rejects them.
63-
const CONFIG_FORM_DECIMAL_NUMBER_RE = /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
61+
// 0x10/0b1010/1_000-style radix or JS-only spellings and silently persist a
62+
// value that differs from the typed text. The grammar mirrors the HTML valid
63+
// floating-point number grammar (including scientific notation like 1e5), so
64+
// anything a real number input can hold coerces exactly as Number() reads it.
65+
// Non-matches stay strings so schema validation rejects them.
66+
const CONFIG_FORM_DECIMAL_NUMBER_RE = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
6467

6568
/**
6669
* Normalizes numeric config input text at the earliest boundary (the input
6770
* handler, before any bare Number() conversion). Returns undefined for empty
68-
* text, a number for plain decimal spellings, and the original text otherwise
69-
* so the gateway's schema validation — not silent reinterpretation — decides.
71+
* text, a number for plain decimal or scientific spellings, and the original
72+
* text otherwise so the gateway's schema validation — not silent
73+
* reinterpretation — decides.
7074
*/
7175
export function coerceConfigFormNumberString(
7276
value: string,

‎ui/src/lib/config/index.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("createRuntimeConfigCapability", () => {
198198
runtimeConfig.dispose();
199199
});
200200

201-
it.each(["0x10", "0b1010", "0o17", "1e3", "+5"])(
201+
it.each(["0x10", "0b1010", "0o17", "+5", "1_000", "Infinity", "NaN", "1e", "e5"])(
202202
"keeps nondecimal spelling %s as a string instead of coercing",
203203
async (spelling) => {
204204
const submitted: Array<{ method: string; params: unknown }> = [];
@@ -244,7 +244,11 @@ describe("createRuntimeConfigCapability", () => {
244244
["42.5", 42.5],
245245
["-7", -7],
246246
[".5", 0.5],
247-
])("still coerces decimal spelling %s to %s", async (spelling, expected) => {
247+
["1e3", 1000],
248+
["1e5", 100000],
249+
[".5e2", 50],
250+
["-2.5E-3", -0.0025],
251+
])("still coerces decimal or scientific spelling %s to %s", async (spelling, expected) => {
248252
const submitted: Array<{ method: string; params: unknown }> = [];
249253
const request = vi.fn(async (method: string, params?: unknown) => {
250254
if (method === "config.get") {
@@ -275,7 +279,7 @@ describe("createRuntimeConfigCapability", () => {
275279
});
276280

277281
it.each([
278-
["1e5", "1e5"],
282+
["1e5", 100000],
279283
["0x10", "0x10"],
280284
["42", 42],
281285
])("string-or-number union field keeps typed spelling %s as %s", async (spelling, expected) => {
@@ -309,8 +313,8 @@ describe("createRuntimeConfigCapability", () => {
309313
const submission = submitted.find((entry) => entry.method === "config.set");
310314
const raw = (submission?.params as { raw?: unknown } | undefined)?.raw;
311315
expect(typeof raw).toBe("string");
312-
// Nondecimal spellings stay strings (a string variant accepts them
313-
// verbatim); only plain decimals coerce to numbers.
316+
// Radix spellings stay strings (a string variant accepts them verbatim);
317+
// decimal and scientific spellings coerce to numbers.
314318
expect(JSON.parse(raw as string)).toEqual({ threshold: expected });
315319
runtimeConfig.dispose();
316320
});

0 commit comments

Comments
 (0)