Skip to content

Commit 5b26168

Browse files
feat: set additionalProperties to false by default unless set to true
BREAKING CHANGE: additionalProperties is set to false by default
1 parent aacb360 commit 5b26168

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/index.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import fs from "fs";
22
import glob from "glob";
33
import yaml from "js-yaml";
4-
import { compile } from "json-schema-to-typescript";
4+
import { compile, Options as CompilerOptions } from "json-schema-to-typescript";
55
import path from "path";
66
import { promisify } from "util";
77

8-
const compileOptions = { bannerComment: "" };
8+
const compileOptions: Partial<CompilerOptions> = { bannerComment: "" };
99
const defaultSchema = { type: "object", additionalProperties: false };
1010

1111
export interface Options {
@@ -15,6 +15,13 @@ export interface Options {
1515
module: string;
1616
}
1717

18+
function addDefaultValueToSchema(schema: any) {
19+
return {
20+
...schema,
21+
additionalProperties: schema.additionalProperties || false,
22+
};
23+
}
24+
1825
export async function generateReplyInterfaces(
1926
prefix: string,
2027
replies: Record<any, any> = {}
@@ -25,7 +32,7 @@ export async function generateReplyInterfaces(
2532
generatedReplyNames.push(prefix + "Reply" + replyCode.toUpperCase());
2633
generatedInterfaces.push(
2734
await compile(
28-
replySchema || defaultSchema,
35+
addDefaultValueToSchema(replySchema || defaultSchema),
2936
prefix + "Reply" + replyCode.toUpperCase(),
3037
compileOptions
3138
)
@@ -72,17 +79,17 @@ import { RouteHandler } from "${options.module}"
7279
${importOrWriteSchema(parsedPath, schema, options, isYaml)}
7380
7481
${await compile(
75-
schema.params || defaultSchema,
82+
addDefaultValueToSchema(schema.params || defaultSchema),
7683
options.prefix + "Params",
7784
compileOptions
7885
)}
7986
${await compile(
80-
schema.querystring || schema.query || defaultSchema,
87+
addDefaultValueToSchema(schema.querystring || schema.query || defaultSchema),
8188
options.prefix + "Query",
8289
compileOptions
8390
)}
8491
${await compile(
85-
schema.body || defaultSchema,
92+
addDefaultValueToSchema(schema.body || defaultSchema),
8693
options.prefix + "Body",
8794
compileOptions
8895
)}

0 commit comments

Comments
 (0)