Skip to content

Commit 00c5a35

Browse files
fix: handle arrays correctly
Co-authored-by: gdgunnars <[email protected]>
1 parent 9c354ae commit 00c5a35

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/schema.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export function addDefaultValueToSchema(
1414
schema: Record<string, any>
1515
): Record<string, any> {
1616
return Object.entries(schema).reduce((acc, [key, value]) => {
17-
if (value !== null && typeof value == "object") {
17+
if (
18+
value !== null &&
19+
typeof value == "object" &&
20+
Array.isArray(value) === false
21+
) {
1822
acc[key] = addDefaultValueToSchema(value);
1923
} else {
2024
acc[key] = value;

tests/schema.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,42 @@ describe("#addDefaultValueToSchema", () => {
445445
additionalProperties: true,
446446
});
447447
});
448+
449+
it("should allow both objects and lists", () => {
450+
const res = addDefaultValueToSchema({
451+
properties: {
452+
success: {
453+
type: "object",
454+
required: ["ok"],
455+
properties: {
456+
ok: {
457+
type: "boolean",
458+
},
459+
},
460+
},
461+
message: {
462+
type: "string",
463+
},
464+
},
465+
});
466+
467+
expect(res).toEqual({
468+
properties: {
469+
success: {
470+
type: "object",
471+
required: ["ok"],
472+
additionalProperties: false,
473+
properties: {
474+
ok: {
475+
type: "boolean",
476+
},
477+
},
478+
},
479+
message: {
480+
type: "string",
481+
},
482+
},
483+
additionalProperties: false,
484+
});
485+
});
448486
});

0 commit comments

Comments
 (0)