Refactor ToolsTab to handle cleared fields as undefined instead of empty strings; update tests for new behavior

This commit is contained in:
olaservo 2025-09-27 20:21:03 -07:00
commit 45a1bde010
2 changed files with 48 additions and 16 deletions

View file

@ -159,17 +159,21 @@ const ToolsTab = ({
id={key} id={key}
name={key} name={key}
placeholder={prop.description} placeholder={prop.description}
value={(params[key] as string) ?? ""} value={
params[key] === undefined
? ""
: String(params[key])
}
onChange={(e) => { onChange={(e) => {
const value = e.target.value; const value = e.target.value;
if (value === "" && !required) { if (value === "") {
// Optional field cleared - set to undefined to omit from request // Field cleared - set to undefined
setParams({ setParams({
...params, ...params,
[key]: undefined, [key]: undefined,
}); });
} else { } else {
// Field has value or is required - keep as string // Field has value - keep as string
setParams({ setParams({
...params, ...params,
[key]: value, [key]: value,
@ -206,22 +210,26 @@ const ToolsTab = ({
id={key} id={key}
name={key} name={key}
placeholder={prop.description} placeholder={prop.description}
value={(params[key] as string) ?? ""} value={
params[key] === undefined
? ""
: String(params[key])
}
onChange={(e) => { onChange={(e) => {
const value = e.target.value; const value = e.target.value;
if (value === "" && !required) { if (value === "") {
// Optional field cleared - set to undefined to omit from request // Field cleared - set to undefined
setParams({ setParams({
...params, ...params,
[key]: undefined, [key]: undefined,
}); });
} else { } else {
// Field has value or is required - convert to number // Field has value - convert to number (never store strings)
const num = Number(value); const num = Number(value);
if (!isNaN(num) || value === "") { if (!isNaN(num)) {
setParams({ setParams({
...params, ...params,
[key]: value === "" ? "" : num, [key]: num,
}); });
} }
} }

View file

@ -2,7 +2,7 @@ import { cleanParams } from "../paramUtils";
import type { JsonSchemaType } from "../jsonUtils"; import type { JsonSchemaType } from "../jsonUtils";
describe("cleanParams", () => { describe("cleanParams", () => {
it("should preserve required fields even when empty", () => { it("should preserve required fields even when undefined", () => {
const schema: JsonSchemaType = { const schema: JsonSchemaType = {
type: "object", type: "object",
required: ["requiredString", "requiredNumber"], required: ["requiredString", "requiredNumber"],
@ -15,17 +15,17 @@ describe("cleanParams", () => {
}; };
const params = { const params = {
requiredString: "", requiredString: undefined,
requiredNumber: 0, requiredNumber: undefined,
optionalString: "", optionalString: undefined,
optionalNumber: undefined, optionalNumber: undefined,
}; };
const cleaned = cleanParams(params, schema); const cleaned = cleanParams(params, schema);
expect(cleaned).toEqual({ expect(cleaned).toEqual({
requiredString: "", requiredString: undefined,
requiredNumber: 0, requiredNumber: undefined,
// optionalString and optionalNumber should be omitted // optionalString and optionalNumber should be omitted
}); });
}); });
@ -181,4 +181,28 @@ describe("cleanParams", () => {
optionalNumber: 0, optionalNumber: 0,
}); });
}); });
it("should handle the new undefined-first approach (no empty strings)", () => {
const schema: JsonSchemaType = {
type: "object",
required: ["requiredField"],
properties: {
requiredField: { type: "string" },
optionalField: { type: "string" },
},
};
// New behavior: cleared fields are undefined, never empty strings
const params = {
requiredField: undefined, // cleared required field
optionalField: undefined, // cleared optional field
};
const cleaned = cleanParams(params, schema);
expect(cleaned).toEqual({
requiredField: undefined, // required field preserved as undefined
// optionalField omitted entirely
});
});
}); });