8000 Fix getIn unexpected value (#3237) · jaredpalmer/formik@f086b5a · GitHub
[go: up one dir, main page]

Skip to content

Commit f086b5a

Browse files
piepluquantizor
andauthored
Fix getIn unexpected value (#3237)
* Fix getIn unexpected value As described in #3181 * Add changeset #3237 * Fix formating, add some test for getIn utils function #3181 * Correct test path getIn #3181 --------- Co-authored-by: Evan Jacobs <570070+probablyup@users.noreply.github.com>
1 parent 75c69c1 commit f086b5a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

.changeset/kind-poems-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'formik': patch
3+
---
4+
5+
Changed `getIn` to return undefined when it can't find a value AND a parent of that value is "falsy" ( "" / 0 / null / false )

packages/formik/src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export function getIn(
7676
while (obj && p < path.length) {
7777
obj = obj[path[p++]];
7878
}
79+
80+
// check if path is not in the end
81+
if (p !== path.length && !obj) {
82+
return def;
83+
}
84+
7985
return obj === undefined ? def : obj;
8086
}
8187

packages/formik/test/utils.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ describe('utils', () => {
152152
const obj = {
153153
a: {
154154
b: 2,
155+
c: false,
156+
d: null,
155157
},
158+
t: true,
159+
s: 'a random string',
156160
};
157161

158162
it('gets a value by array path', () => {
@@ -166,6 +170,22 @@ describe('utils', () => {
166170
it('return "undefined" if value was not found using given path', () => {
167171
expect(getIn(obj, 'a.z')).toBeUndefined();
168172
});
173+
174+
it('return "undefined" if value was not found using given path and an intermediate value is "false"', () => {
175+
expect(getIn(obj, 'a.c.z')).toBeUndefined();
176+
});
177+
178+
it('return "undefined" if value was not found using given path and an intermediate value is "null"', () => {
179+
expect(getIn(obj, 'a.d.z')).toBeUndefined();
180+
});
181+
182+
it('return "undefined" if value was not found using given path and an intermediate value is "true"', () => {
183+
expect(getIn(obj, 't.z')).toBeUndefined();
184+
});
185+
186+
it('return "undefined" if value was not found using given path and an intermediate value is a string', () => {
187+
expect(getIn(obj, 's.z')).toBeUndefined();
188+
});
169189
});
170190

171191
describe('setIn', () => {

0 commit comments

Comments
 (0)
0