Skip to content

Commit 8ea2042

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

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,23 @@ describe('objectGuard', () => {
122122
}
123123
}
124124
})
125-
})
125+
});
126+
126127
it('should undo nested deletes', () => {
127128
const obj = {outer: {inner: {foo: {nested: 'val'}, bar: 'val'}}};
128129
const guard = objectGuard([rule])(obj);
129130
delete guard.obj.outer.inner.foo.nested;
130131
delete guard.obj.outer.inner.bar;
131132
guard.verify();
132133
expect(obj).to.eql({outer: {inner: {foo: {nested: 'val'}, bar: 'val'}}})
133-
})
134+
});
135+
136+
it('should work on null properties', () => {
137+
const obj = {foo: null};
138+
const guard = objectGuard([rule])(obj);
139+
guard.obj.foo = 'denied';
140+
guard.verify();
141+
expect(obj).to.eql({foo: null});
142+
});
134143
});
135144
});

0 commit comments

Comments
 (0)