Merge pull request #926 from bolinfest/sapling-pr-archive-bolinfest
fix: allow empty elicitation form data when all fields are optional
This commit is contained in:
commit
5778a3a93e
2 changed files with 41 additions and 4 deletions
|
|
@ -43,8 +43,38 @@ describe("generateDefaultValue", () => {
|
||||||
expect(generateDefaultValue({ type: "array" })).toBe(undefined);
|
expect(generateDefaultValue({ type: "array" })).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates undefined for optional object", () => {
|
test("generates empty object for optional root object", () => {
|
||||||
expect(generateDefaultValue({ type: "object" })).toBe(undefined);
|
expect(generateDefaultValue({ type: "object" })).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("generates undefined for nested optional object", () => {
|
||||||
|
// When called WITH propertyName and parentSchema, and the property is NOT required,
|
||||||
|
// nested optional objects should return undefined
|
||||||
|
const parentSchema = {
|
||||||
|
type: "object" as const,
|
||||||
|
required: ["otherField"],
|
||||||
|
properties: {
|
||||||
|
optionalObject: { type: "object" as const },
|
||||||
|
otherField: { type: "string" as const },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
expect(
|
||||||
|
generateDefaultValue({ type: "object" }, "optionalObject", parentSchema),
|
||||||
|
).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("generates empty object for root-level object with all optional properties", () => {
|
||||||
|
// Root-level schema with properties but no required array
|
||||||
|
// This is the exact scenario from PR #926 - elicitation with all optional fields
|
||||||
|
const schema: JsonSchemaType = {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
optionalField1: { type: "string" },
|
||||||
|
optionalField2: { type: "number" },
|
||||||
|
},
|
||||||
|
// No required array - all fields are optional
|
||||||
|
};
|
||||||
|
expect(generateDefaultValue(schema)).toEqual({});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("generates default null for unknown types", () => {
|
test("generates default null for unknown types", () => {
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,7 @@ export function generateDefaultValue(
|
||||||
propertyName && parentSchema
|
propertyName && parentSchema
|
||||||
? isPropertyRequired(propertyName, parentSchema)
|
? isPropertyRequired(propertyName, parentSchema)
|
||||||
: false;
|
: false;
|
||||||
|
const isRootSchema = propertyName === undefined && parentSchema === undefined;
|
||||||
|
|
||||||
switch (schema.type) {
|
switch (schema.type) {
|
||||||
case "string":
|
case "string":
|
||||||
|
|
@ -112,7 +113,9 @@ export function generateDefaultValue(
|
||||||
case "array":
|
case "array":
|
||||||
return isRequired ? [] : undefined;
|
return isRequired ? [] : undefined;
|
||||||
case "object": {
|
case "object": {
|
||||||
if (!schema.properties) return isRequired ? {} : undefined;
|
if (!schema.properties) {
|
||||||
|
return isRequired || isRootSchema ? {} : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
const obj: JsonObject = {};
|
const obj: JsonObject = {};
|
||||||
// Include required properties OR optional properties that declare a default
|
// Include required properties OR optional properties that declare a default
|
||||||
|
|
@ -126,7 +129,11 @@ export function generateDefaultValue(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return isRequired ? obj : Object.keys(obj).length > 0 ? obj : undefined;
|
|
||||||
|
if (Object.keys(obj).length === 0) {
|
||||||
|
return isRequired || isRootSchema ? {} : undefined;
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
case "null":
|
case "null":
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue