Browse Source

Don't scatter explicit undefineds through objects because of unused rules

master
Sven Slootweg 11 months ago
parent
commit
7472f83d0c
2 changed files with 7 additions and 2 deletions
  1. +2
    -1
      example.js
  2. +5
    -1
      index.js

+ 2
- 1
example.js View File

@ -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 = {


+ 5
- 1
index.js View File

@ -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;


Loading…
Cancel
Save