From 60e5ec30fabc24571afb170d1aac2f2e186294cb Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Tue, 21 May 2019 16:56:45 -0700 Subject: [PATCH 1/2] Default type to `object` if undefined Fixes #1265 --- src/utils.js | 4 ++++ test/utils_test.js | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 22f08e1863..3711c9dec3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -76,6 +76,10 @@ export function getSchemaType(schema) { return "string"; } + if ((!type && schema.properties) || schema.additionalProperties) { + return "object"; + } + if (type instanceof Array && type.length === 2 && type.includes("null")) { return type.find(type => type !== "null"); } diff --git a/test/utils_test.js b/test/utils_test.js index 5acfcc229d..8b69e58b22 100644 --- a/test/utils_test.js +++ b/test/utils_test.js @@ -1544,11 +1544,22 @@ describe("utils", () => { schema: { type: ["integer", "null"] }, expected: "integer", }, + { + schema: { properties: {} }, + expected: "object", + }, + { + schema: { additionalProperties: {} }, + expected: "object", + }, ]; it("should correctly guess the type of a schema", () => { for (const test of cases) { - expect(getSchemaType(test.schema)).eql(test.expected); + expect(getSchemaType(test.schema)).eql( + test.expected, + `${JSON.stringify(test.schema)} should guess type of ${test.expected}` + ); } }); }); From 151fb76e05f59fccb4290851ee1700c2ddfde794 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Tue, 28 May 2019 17:38:25 -0700 Subject: [PATCH 2/2] Tidy up if statement expression as per PR review --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 3711c9dec3..cf9600d5f4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -76,7 +76,7 @@ export function getSchemaType(schema) { return "string"; } - if ((!type && schema.properties) || schema.additionalProperties) { + if (!type && (schema.properties || schema.additionalProperties)) { return "object"; }