Skip to content

Commit

Permalink
fix issue with set api (#11915)
Browse files Browse the repository at this point in the history
* fix issue with set api

* remove test file

* add test coverage and update api contract
  • Loading branch information
bluebill1049 authored May 21, 2024
1 parent e035a5c commit 3be826c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions reports/api-extractor.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ export type ResolverSuccess<TFieldValues extends FieldValues = FieldValues> = {
errors: {};
};

// @public (undocumented)
export const set: (object: FieldValues, path: string, value?: unknown) => FieldValues | undefined;

// @public (undocumented)
export type SetFieldValue<TFieldValues extends FieldValues> = FieldValue<TFieldValues>;

Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/utils/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ describe('set', () => {
},
});
});

it('should not populate prototype', () => {
set({}, '__proto__[test2]', '456');
expect(Object.prototype).toEqual({});
});
});
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import get from './get';
import set from './set';

export { get };
export { get, set };
5 changes: 5 additions & 0 deletions src/utils/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export default (object: FieldValues, path: string, value?: unknown) => {
? []
: {};
}

if (key === '__proto__') {
return;
}

object[key] = newValue;
object = object[key];
}
Expand Down

0 comments on commit 3be826c

Please sign in to comment.