Skip to content

Commit

Permalink
feat: set additionalProperties to false by default unless set to true
Browse files Browse the repository at this point in the history
BREAKING CHANGE: additionalProperties is set to false by default
  • Loading branch information
thomasthiebaud committed Sep 2, 2020
1 parent aacb360 commit 5b26168
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from "fs";
import glob from "glob";
import yaml from "js-yaml";
import { compile } from "json-schema-to-typescript";
import { compile, Options as CompilerOptions } from "json-schema-to-typescript";
import path from "path";
import { promisify } from "util";

const compileOptions = { bannerComment: "" };
const compileOptions: Partial<CompilerOptions> = { bannerComment: "" };
const defaultSchema = { type: "object", additionalProperties: false };

export interface Options {
Expand All @@ -15,6 +15,13 @@ export interface Options {
module: string;
}

function addDefaultValueToSchema(schema: any) {
return {
...schema,
additionalProperties: schema.additionalProperties || false,
};
}

export async function generateReplyInterfaces(
prefix: string,
replies: Record<any, any> = {}
Expand All @@ -25,7 +32,7 @@ export async function generateReplyInterfaces(
generatedReplyNames.push(prefix + "Reply" + replyCode.toUpperCase());
generatedInterfaces.push(
await compile(
replySchema || defaultSchema,
addDefaultValueToSchema(replySchema || defaultSchema),
prefix + "Reply" + replyCode.toUpperCase(),
compileOptions
)
Expand Down Expand Up @@ -72,17 +79,17 @@ import { RouteHandler } from "${options.module}"
${importOrWriteSchema(parsedPath, schema, options, isYaml)}
${await compile(
schema.params || defaultSchema,
addDefaultValueToSchema(schema.params || defaultSchema),
options.prefix + "Params",
compileOptions
)}
${await compile(
schema.querystring || schema.query || defaultSchema,
addDefaultValueToSchema(schema.querystring || schema.query || defaultSchema),
options.prefix + "Query",
compileOptions
)}
${await compile(
schema.body || defaultSchema,
addDefaultValueToSchema(schema.body || defaultSchema),
options.prefix + "Body",
compileOptions
)}
Expand Down

0 comments on commit 5b26168

Please sign in to comment.