fix: browser fill default type parity (#27662) (thanks @Uface11)

This commit is contained in:
Peter Steinberger
2026-02-26 22:14:06 +01:00
parent a0b12f2ba7
commit 2ed9d633b3
3 changed files with 22 additions and 4 deletions

View File

@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { readFields } from "./shared.js";
describe("readFields", () => {
it("defaults missing type to text", async () => {
await expect(readFields({ fields: '[{"ref":"7","value":"world"}]' })).resolves.toEqual([
{ ref: "7", type: "text", value: "world" },
]);
});
it("requires ref", async () => {
await expect(readFields({ fields: '[{"type":"textbox","value":"world"}]' })).rejects.toThrow(
"fields[0] must include ref",
);
});
});

View File

@@ -70,18 +70,19 @@ export async function readFields(opts: {
const rec = entry as Record<string, unknown>;
const ref = typeof rec.ref === "string" ? rec.ref.trim() : "";
const type = typeof rec.type === "string" ? rec.type.trim() : "";
if (!ref || !type) {
throw new Error(`fields[${index}] must include ref and type`);
if (!ref) {
throw new Error(`fields[${index}] must include ref`);
}
const resolvedType = type || "text";
if (
typeof rec.value === "string" ||
typeof rec.value === "number" ||
typeof rec.value === "boolean"
) {
return { ref, type, value: rec.value };
return { ref, type: resolvedType, value: rec.value };
}
if (rec.value === undefined || rec.value === null) {
return { ref, type };
return { ref, type: resolvedType };
}
throw new Error(`fields[${index}].value must be string, number, boolean, or null`);
});