refactor(browser): unify fill field normalization
This commit is contained in:
@@ -2,10 +2,24 @@ 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.each([
|
||||
{
|
||||
name: "keeps explicit type",
|
||||
fields: '[{"ref":"6","type":"textbox","value":"hello"}]',
|
||||
expected: [{ ref: "6", type: "textbox", value: "hello" }],
|
||||
},
|
||||
{
|
||||
name: "defaults missing type to text",
|
||||
fields: '[{"ref":"7","value":"world"}]',
|
||||
expected: [{ ref: "7", type: "text", value: "world" }],
|
||||
},
|
||||
{
|
||||
name: "defaults blank type to text",
|
||||
fields: '[{"ref":"8","type":" ","value":"blank"}]',
|
||||
expected: [{ ref: "8", type: "text", value: "blank" }],
|
||||
},
|
||||
])("$name", async ({ fields, expected }) => {
|
||||
await expect(readFields({ fields })).resolves.toEqual(expected);
|
||||
});
|
||||
|
||||
it("requires ref", async () => {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import type { Command } from "commander";
|
||||
import type { BrowserFormField } from "../../browser/client-actions-core.js";
|
||||
import {
|
||||
normalizeBrowserFormField,
|
||||
normalizeBrowserFormFieldValue,
|
||||
} from "../../browser/form-fields.js";
|
||||
import { danger } from "../../globals.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { callBrowserRequest, type BrowserParentOpts } from "../browser-cli-shared.js";
|
||||
@@ -68,21 +72,16 @@ export async function readFields(opts: {
|
||||
throw new Error(`fields[${index}] must be an object`);
|
||||
}
|
||||
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) {
|
||||
const parsedField = normalizeBrowserFormField(rec);
|
||||
if (!parsedField) {
|
||||
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"
|
||||
rec.value === undefined ||
|
||||
rec.value === null ||
|
||||
normalizeBrowserFormFieldValue(rec.value) !== undefined
|
||||
) {
|
||||
return { ref, type: resolvedType, value: rec.value };
|
||||
}
|
||||
if (rec.value === undefined || rec.value === null) {
|
||||
return { ref, type: resolvedType };
|
||||
return parsedField;
|
||||
}
|
||||
throw new Error(`fields[${index}].value must be string, number, boolean, or null`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user