Skip to content

Commit

Permalink
fix: handle arrays correctly
Browse files Browse the repository at this point in the history
Co-authored-by: gdgunnars <[email protected]>
  • Loading branch information
thomasthiebaud and gdgunnars committed Apr 1, 2022
1 parent 9c354ae commit 00c5a35
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export function addDefaultValueToSchema(
schema: Record<string, any>
): Record<string, any> {
return Object.entries(schema).reduce((acc, [key, value]) => {
if (value !== null && typeof value == "object") {
if (
value !== null &&
typeof value == "object" &&
Array.isArray(value) === false
) {
acc[key] = addDefaultValueToSchema(value);
} else {
acc[key] = value;
Expand Down
38 changes: 38 additions & 0 deletions tests/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,42 @@ describe("#addDefaultValueToSchema", () => {
additionalProperties: true,
});
});

it("should allow both objects and lists", () => {
const res = addDefaultValueToSchema({
properties: {
success: {
type: "object",
required: ["ok"],
properties: {
ok: {
type: "boolean",
},
},
},
message: {
type: "string",
},
},
});

expect(res).toEqual({
properties: {
success: {
type: "object",
required: ["ok"],
additionalProperties: false,
properties: {
ok: {
type: "boolean",
},
},
},
message: {
type: "string",
},
},
additionalProperties: false,
});
});
});

0 comments on commit 00c5a35

Please sign in to comment.