From 588d151c25cf2174dec354504bc17a9b304c9253 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 17 Jun 2020 19:11:38 +0200 Subject: [PATCH] Fix broken required-properties check --- index.js | 10 +++++++++- package.json | 1 + yarn.lock | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b705a24..693f80c 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const asExpression = require("as-expression"); const defaultValue = require("default-value"); const assureArray = require("assure-array"); const flatten = require("flatten"); +const arrayUnion = require("array-union"); const ValidationError = require("@validatem/error"); const validationResult = require("@validatem/validation-result"); @@ -55,7 +56,14 @@ module.exports = function hasShape(rules) { let errors = []; let newObject = {}; - for (let [ key, value ] of Object.entries(object)) { + // We need to consider the keys from the ruleset (for detecting missing required properties) *and* the keys from the actual object (for handling extraneous values). + let allKeys = arrayUnion( + Object.keys(rules), + Object.keys(object) + ); + + for (let key of allKeys) { + let value = object[key]; let rule = rules[key]; if (rule != null) { diff --git a/package.json b/package.json index 5cf6e12..15a7d52 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@validatem/combinator": "^0.1.0", "@validatem/error": "^1.0.0", "@validatem/validation-result": "^0.1.1", + "array-union": "^2.1.0", "as-expression": "^1.0.0", "assure-array": "^1.0.0", "default-value": "^1.0.0", diff --git a/yarn.lock b/yarn.lock index 14107bb..c17fe5f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -135,6 +135,11 @@ resolved "https://registry.yarnpkg.com/@validatem/virtual-property/-/virtual-property-0.1.0.tgz#880540dfd149f98ecf1095d93912e34443381fe4" integrity sha512-JUUvWtdqoSkOwlsl20oB3qFHYIL05a/TAfdY4AJcs55QeVTiX5iI1b8IoQW644sIWWooBuLv+XwoxjRsQFczlQ== +array-union@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" + integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw== + as-expression@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/as-expression/-/as-expression-1.0.0.tgz#7bc620ca4cb2fe0ee90d86729bd6add33b8fd831"