-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: formData change clear errorMessage (#4429)
* fix: formData change clear errorMessage fix: merge errorSchema fix: merge errorSchema * test: add tests for getChangedFields and update CHANGELOG * fix: core test and error message when formData is a string not cleared * feat: modify CHANGELOG --------- Co-authored-by: Heath C <[email protected]>
- Loading branch information
1 parent
a521990
commit b6c1825
Showing
6 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import keys from 'lodash/keys'; | ||
import pickBy from 'lodash/pickBy'; | ||
import isPlainObject from 'lodash/isPlainObject'; | ||
import get from 'lodash/get'; | ||
import difference from 'lodash/difference'; | ||
import deepEquals from './deepEquals'; | ||
|
||
/** | ||
* Compares two objects and returns the names of the fields that have changed. | ||
* This function iterates over each field of object `a`, using `_.isEqual` to compare the field value | ||
* with the corresponding field value in object `b`. If the values are different, the field name will | ||
* be included in the returned array. | ||
* | ||
* @param {unknown} a - The first object, representing the original data to compare. | ||
* @param {unknown} b - The second object, representing the updated data to compare. | ||
* @returns {string[]} - An array of field names that have changed. | ||
* | ||
* @example | ||
* const a = { name: 'John', age: 30 }; | ||
* const b = { name: 'John', age: 31 }; | ||
* const changedFields = getChangedFields(a, b); | ||
* console.log(changedFields); // Output: ['age'] | ||
*/ | ||
export default function getChangedFields(a: unknown, b: unknown): string[] { | ||
const aIsPlainObject = isPlainObject(a); | ||
const bIsPlainObject = isPlainObject(b); | ||
// If strictly equal or neither of them is a plainObject returns an empty array | ||
if (a === b || (!aIsPlainObject && !bIsPlainObject)) { | ||
return []; | ||
} | ||
if (aIsPlainObject && !bIsPlainObject) { | ||
return keys(a); | ||
} else if (!aIsPlainObject && bIsPlainObject) { | ||
return keys(b); | ||
} else { | ||
const unequalFields = keys(pickBy(a as object, (value, key) => !deepEquals(value, get(b, key)))); | ||
const diffFields = difference(keys(b), keys(a)); | ||
return [...unequalFields, ...diffFields]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { getChangedFields } from '../src'; | ||
import cloneDeep from 'lodash/cloneDeep'; | ||
|
||
const complexObject = { | ||
a: 1, | ||
b: '2', | ||
c: { c1: {}, c2: [] }, | ||
d: ['item1', 'item2', 'item2'], | ||
e: function () {}, | ||
}; | ||
const complexObjectKeys = ['a', 'b', 'c', 'd', 'e']; | ||
|
||
describe('getChangedFields()', () => { | ||
it('Empty parameter', () => { | ||
expect(getChangedFields(undefined, undefined)).toEqual([]); | ||
expect(getChangedFields(complexObject, undefined)).toEqual(complexObjectKeys); | ||
expect(getChangedFields(undefined, complexObject)).toEqual(complexObjectKeys); | ||
}); | ||
it('Both not plainObject parameter', () => { | ||
expect(getChangedFields(1, 2)).toEqual([]); | ||
expect(getChangedFields(2, '1')).toEqual([]); | ||
expect( | ||
getChangedFields( | ||
function a() {}, | ||
function b() {} | ||
) | ||
).toEqual([]); | ||
expect(getChangedFields(new Date(), new Date())).toEqual([]); | ||
}); | ||
it('One is not plainObject parameter', () => { | ||
expect(getChangedFields(1, complexObject)).toEqual(complexObjectKeys); | ||
expect(getChangedFields('1', complexObject)).toEqual(complexObjectKeys); | ||
expect(getChangedFields(function noop() {}, complexObject)).toEqual(complexObjectKeys); | ||
expect(getChangedFields(new Date(), complexObject)).toEqual(complexObjectKeys); | ||
|
||
expect(getChangedFields(complexObject, 1)).toEqual(complexObjectKeys); | ||
expect(getChangedFields(complexObject, '1')).toEqual(complexObjectKeys); | ||
expect(getChangedFields(complexObject, function noop() {})).toEqual(complexObjectKeys); | ||
expect(getChangedFields(complexObject, new Date())).toEqual(complexObjectKeys); | ||
}); | ||
it('Deep equal', () => { | ||
expect(getChangedFields(complexObject, complexObject)).toEqual([]); | ||
expect(getChangedFields(complexObject, cloneDeep(complexObject))).toEqual([]); | ||
}); | ||
it('Change one field', () => { | ||
expect(getChangedFields(complexObject, { ...cloneDeep(complexObject), a: 2 })).toEqual(['a']); | ||
expect(getChangedFields({ ...cloneDeep(complexObject), a: 2 }, complexObject)).toEqual(['a']); | ||
}); | ||
it('Change some fields', () => { | ||
expect( | ||
getChangedFields(complexObject, { | ||
a: 2, | ||
b: '3', | ||
c: { c1: {}, c2: [], c3: [] }, | ||
d: ['item1', 'item2'], | ||
e: function () {}, | ||
}) | ||
).toEqual(['a', 'b', 'c', 'd']); | ||
expect( | ||
getChangedFields( | ||
{ | ||
a: 2, | ||
b: '3', | ||
c: { c1: {}, c2: [], c3: [] }, | ||
d: ['item1', 'item2'], | ||
e: function () {}, | ||
}, | ||
complexObject | ||
) | ||
).toEqual(['a', 'b', 'c', 'd']); | ||
}); | ||
it('Delete one field', () => { | ||
expect( | ||
getChangedFields(complexObject, { | ||
a: 1, | ||
b: '2', | ||
c: { c1: {}, c2: [] }, | ||
d: ['item1', 'item2', 'item2'], | ||
}) | ||
).toEqual(['e']); | ||
expect( | ||
getChangedFields( | ||
{ | ||
a: 1, | ||
b: '2', | ||
c: { c1: {}, c2: [] }, | ||
d: ['item1', 'item2', 'item2'], | ||
}, | ||
complexObject | ||
) | ||
).toEqual(['e']); | ||
}); | ||
it('Delete some fields', () => { | ||
expect( | ||
getChangedFields(complexObject, { | ||
a: 1, | ||
b: '2', | ||
c: { c1: {}, c2: [] }, | ||
}) | ||
).toEqual(['d', 'e']); | ||
expect( | ||
getChangedFields( | ||
{ | ||
a: 1, | ||
b: '2', | ||
c: { c1: {}, c2: [] }, | ||
}, | ||
complexObject | ||
) | ||
).toEqual(['d', 'e']); | ||
}); | ||
it('Add one field', () => { | ||
expect( | ||
getChangedFields(complexObject, { | ||
...complexObject, | ||
f: {}, | ||
}) | ||
).toEqual(['f']); | ||
expect( | ||
getChangedFields( | ||
{ | ||
...complexObject, | ||
f: {}, | ||
}, | ||
complexObject | ||
) | ||
).toEqual(['f']); | ||
}); | ||
it('Add some fields', () => { | ||
expect( | ||
getChangedFields(complexObject, { | ||
...complexObject, | ||
f: {}, | ||
g: [], | ||
}) | ||
).toEqual(['f', 'g']); | ||
expect( | ||
getChangedFields( | ||
{ | ||
...complexObject, | ||
f: {}, | ||
g: [], | ||
}, | ||
complexObject | ||
) | ||
).toEqual(['f', 'g']); | ||
}); | ||
}); |