From 7472f83d0ca83208d2dfe4d874f51638385e4145 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 22 Jun 2020 02:25:19 +0200 Subject: [PATCH] Don't scatter explicit undefineds through objects because of unused rules --- example.js | 3 ++- index.js | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/example.js b/example.js index 68a8413..5a6abe0 100644 --- a/example.js +++ b/example.js @@ -5,7 +5,8 @@ const { validateValue } = require("@validatem/core"); const isString = require("@validatem/is-string"); let validator = hasShape({ - foo: [ isString ] + foo: [ isString ], + unused: [ isString ] }); let objectA = { diff --git a/index.js b/index.js index 693f80c..e45e75a 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,11 @@ module.exports = function hasShape(rules) { }); errors.push(... annotatedErrors); - newObject[key] = newValue; + + // Don't scatter explicit `undefined`s across the result object, just because an optional rule existed for some properties but the corresponding value didn't + if (newValue !== undefined) { + newObject[key] = newValue; + } } else { // Extraneous property newObject[key] = value;