Skip to content

Commit

Permalink
updating to throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Oct 1, 2024
1 parent 70bb748 commit 734ae86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ describe('object_utils', () => {
const test11 = setNestedProperty(getFalseyObject(), 'the.other_nested.value', 'update');
expect(test11.the.other_nested.value).toBe('update');

const test12 = setNestedProperty(getTestObj(), 'the.__proto__', 'update');
expect(test12.the).toBe('update');
expect(test12.the.__proto__.update).toBe(undefined);

const test13 = setNestedProperty(getTestObj(), 'the.prototype', 'update');
expect(test13.the).toBe('update');
expect(test13.the.prototype?.update).toBe(undefined);
expect(() => {
setNestedProperty(getTestObj(), 'the.__proto__', 'update');
}).toThrow('Invalid accessor');
expect(() => {
setNestedProperty(getTestObj(), 'the.prototype', 'update');
}).toThrow('Invalid accessor');
expect(() => {
setNestedProperty(getTestObj(), 'the.constructor', 'update');
}).toThrow('Invalid accessor');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
* 2.0.
*/

const INVALID_ACCESSORS = ['__proto__', 'prototype', 'constructor'];

export const setNestedProperty = (obj: Record<string, any>, accessor: string, value: any) => {
let ref = obj;
const accessors = accessor.split('.').filter((a) => a !== '__proto__' && a !== 'prototype');
const accessors = accessor.split('.');
if (accessors.some((a) => INVALID_ACCESSORS.includes(a))) {
throw new Error('Invalid accessor');
}

const len = accessors.length;
for (let i = 0; i < len - 1; i++) {
const attribute = accessors[i];
Expand Down

0 comments on commit 734ae86

Please sign in to comment.