Skip to content

Commit 9c354ae

Browse files
fix: add default values to nested schema
1 parent 8bb26b8 commit 9c354ae

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

src/schema.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@ import { capitalize, writeFile } from "./utils";
1010
const compileOptions: Partial<CompilerOptions> = { bannerComment: "" };
1111
const defaultSchema = { type: "object", additionalProperties: false };
1212

13-
function addDefaultValueToSchema(schema: any) {
14-
return {
15-
...schema,
16-
additionalProperties: schema.additionalProperties || false,
17-
};
13+
export function addDefaultValueToSchema(
14+
schema: Record<string, any>
15+
): Record<string, any> {
16+
return Object.entries(schema).reduce((acc, [key, value]) => {
17+
if (value !== null && typeof value == "object") {
18+
acc[key] = addDefaultValueToSchema(value);
19+
} else {
20+
acc[key] = value;
21+
}
22+
23+
if (key === "properties" && !acc.additionalProperties) {
24+
acc.additionalProperties = false;
25+
}
26+
27+
return acc;
28+
}, {} as any);
1829
}
1930

2031
export const defaultOptions = {

tests/schema.spec.ts

+102
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
generateReplyInterfaces,
44
generateInterfaces,
55
defaultOptions,
6+
addDefaultValueToSchema,
67
} from "../src/schema";
78

89
describe("#generateReplyInterfaces", () => {
@@ -344,3 +345,104 @@ describe("#generateInterfaces", () => {
344345
).toMatchSnapshot();
345346
});
346347
});
348+
349+
describe("#addDefaultValueToSchema", () => {
350+
it("should add default values to schema", () => {
351+
const res = addDefaultValueToSchema({
352+
properties: {
353+
success: {
354+
type: "string",
355+
},
356+
message: {
357+
type: "string",
358+
},
359+
},
360+
});
361+
362+
expect(res).toEqual({
363+
properties: {
364+
success: {
365+
type: "string",
366+
},
367+
message: {
368+
type: "string",
369+
},
370+
},
371+
additionalProperties: false,
372+
});
373+
});
374+
375+
it("should add default values to nested schema", () => {
376+
const res = addDefaultValueToSchema({
377+
properties: {
378+
success: {
379+
type: "object",
380+
properties: {
381+
ok: {
382+
type: "boolean",
383+
},
384+
},
385+
},
386+
message: {
387+
type: "string",
388+
},
389+
},
390+
});
391+
392+
expect(res).toEqual({
393+
properties: {
394+
success: {
395+
type: "object",
396+
additionalProperties: false,
397+
properties: {
398+
ok: {
399+
type: "boolean",
400+
},
401+
},
402+
},
403+
message: {
404+
type: "string",
405+
},
406+
},
407+
additionalProperties: false,
408+
});
409+
});
410+
411+
it("should not overide values if already present", () => {
412+
const res = addDefaultValueToSchema({
413+
properties: {
414+
success: {
415+
type: "object",
416+
additionalProperties: true,
417+
properties: {
418+
ok: {
419+
type: "boolean",
420+
},
421+
},
422+
},
423+
message: {
424+
type: "string",
425+
},
426+
},
427+
additionalProperties: true,
428+
});
429+
430+
expect(res).toEqual({
431+
properties: {
432+
success: {
433+
type: "object",
434+
additionalProperties: true,
435+
properties: {
436+
ok: {
437+
type: "boolean",
438+
},
439+
},
440+
},
441+
message: {
442+
type: "string",
443+
},
444+
},
445+
additionalProperties: true,
446+
});
447+
});
448+
});

0 commit comments

Comments
 (0)