From 55bb09ba1a5762097d3d41e0c4f2e8f375872fe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 13 Nov 2019 16:55:54 +0100 Subject: [PATCH] [FIX] component: fix invalid prop validation situations closes #453 --- src/component/props_validation.ts | 4 ++-- tests/component/props_validation.test.ts | 26 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/component/props_validation.ts b/src/component/props_validation.ts index b83ba13c..55d1136d 100644 --- a/src/component/props_validation.ts +++ b/src/component/props_validation.ts @@ -90,12 +90,12 @@ function isValidProp(prop, propDef): boolean { return true; } let result = isValidProp(prop, propDef.type); - if (propDef.type === Array) { + if (propDef.type === Array && propDef.element) { for (let i = 0, iLen = prop.length; i < iLen; i++) { result = result && isValidProp(prop[i], propDef.element); } } - if (propDef.type === Object) { + if (propDef.type === Object && propDef.shape) { const shape = propDef.shape; for (let key in shape) { result = result && isValidProp(prop[key], shape[key]); diff --git a/tests/component/props_validation.test.ts b/tests/component/props_validation.test.ts index 03753cf1..22645bac 100644 --- a/tests/component/props_validation.test.ts +++ b/tests/component/props_validation.test.ts @@ -581,6 +581,32 @@ describe("props validation", () => { }).toThrow(); }); + test("props with type array, and no element", async () => { + class TestWidget extends Widget { + static props = { myprop: { type: Array } }; + } + + expect(() => { + QWeb.utils.validateProps(TestWidget, { myprop: [1] }); + }).not.toThrow(); + expect(() => { + QWeb.utils.validateProps(TestWidget, { myprop: 1 }); + }).toThrow(`Props 'myprop' of invalid type in component 'TestWidget'`); + }); + + test("props with type object, and no shape", async () => { + class TestWidget extends Widget { + static props = { myprop: { type: Object } }; + } + + expect(() => { + QWeb.utils.validateProps(TestWidget, { myprop: { a: 3 } }); + }).not.toThrow(); + expect(() => { + QWeb.utils.validateProps(TestWidget, { myprop: false }); + }).toThrow(`Props 'myprop' of invalid type in component 'TestWidget'`); + }); + test("props: extra props cause an error", async () => { class TestWidget extends Widget { static props = ["message"];