Skip to content

Commit

Permalink
fix objectGuard edge case: null values
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Apr 13, 2023
1 parent 64e0d4f commit 8ea2042
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libraries/objectGuard/objectGuard.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function writeProtectRule(ruleDef) {
run(root, path, object, property, applies) {
const origHasProp = object && object.hasOwnProperty(property);
const original = origHasProp ? object[property] : undefined;
const origCopy = origHasProp && typeof original === 'object' ? deepClone(original) : original;
const origCopy = origHasProp && original != null && typeof original === 'object' ? deepClone(original) : original;
return function () {
const object = path == null ? root : deepAccess(root, path);
const finalHasProp = object && isData(object[property]);
Expand Down
13 changes: 11 additions & 2 deletions test/spec/activities/objectGuard_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,23 @@ describe('objectGuard', () => {
}
}
})
})
});

it('should undo nested deletes', () => {
const obj = {outer: {inner: {foo: {nested: 'val'}, bar: 'val'}}};
const guard = objectGuard([rule])(obj);
delete guard.obj.outer.inner.foo.nested;
delete guard.obj.outer.inner.bar;
guard.verify();
expect(obj).to.eql({outer: {inner: {foo: {nested: 'val'}, bar: 'val'}}})
})
});

it('should work on null properties', () => {
const obj = {foo: null};
const guard = objectGuard([rule])(obj);
guard.obj.foo = 'denied';
guard.verify();
expect(obj).to.eql({foo: null});
});
});
});

0 comments on commit 8ea2042

Please sign in to comment.