Skip to content

Commit 7291196

Browse files
committed
fix objectGuard edge case: null values
1 parent 64e0d4f commit 7291196

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

libraries/objectGuard/objectGuard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function writeProtectRule(ruleDef) {
9292
run(root, path, object, property, applies) {
9393
const origHasProp = object && object.hasOwnProperty(property);
9494
const original = origHasProp ? object[property] : undefined;
95-
const origCopy = origHasProp && typeof original === 'object' ? deepClone(original) : original;
95+
const origCopy = origHasProp && original != null && typeof original === 'object' ? deepClone(original) : original;
9696
return function () {
9797
const object = path == null ? root : deepAccess(root, path);
9898
const finalHasProp = object && isData(object[property]);

test/spec/activities/objectGuard_spec.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('objectGuard', () => {
9595
expect(obj).to.eql({bar: {nested: 'val'}, other: 'allowed'});
9696
});
9797

98+
9899
it('should undo top-level deletes', () => {
99100
const obj = {foo: {nested: 'val'}, bar: 'val'};
100101
const guard = objectGuard([rule])(obj);
@@ -122,14 +123,23 @@ describe('objectGuard', () => {
122123
}
123124
}
124125
})
125-
})
126+
});
127+
126128
it('should undo nested deletes', () => {
127129
const obj = {outer: {inner: {foo: {nested: 'val'}, bar: 'val'}}};
128130
const guard = objectGuard([rule])(obj);
129131
delete guard.obj.outer.inner.foo.nested;
130132
delete guard.obj.outer.inner.bar;
131133
guard.verify();
132134
expect(obj).to.eql({outer: {inner: {foo: {nested: 'val'}, bar: 'val'}}})
133-
})
135+
});
136+
137+
it('should work on null properties', () => {
138+
const obj = {foo: null};
139+
const guard = objectGuard([rule])(obj);
140+
guard.obj.foo = 'denied';
141+
guard.verify();
142+
expect(obj).to.eql({foo: null});
143+
});
134144
});
135145
});

0 commit comments

Comments
 (0)