Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 21b5714

Browse files
committed
Fixing async validity merging issues. Fixes #834
1 parent 23c20e4 commit 21b5714

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/components/control-component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import shallowEqual from '../utils/shallow-equal';
77
import _get from '../utils/get';
88
import merge from '../utils/merge';
99
import mapValues from '../utils/map-values';
10-
import isPlainObject from '../utils/is-plain-object';
10+
import isPlainObject, { isObjectLike } from '../utils/is-plain-object';
1111
import i from 'icepick';
1212
import omit from '../utils/omit';
1313
import actionTypes from '../action-types';
@@ -36,7 +36,7 @@ const disallowedProps = ['changeAction', 'getFieldFromState', 'store'];
3636

3737
function mergeOrSetErrors(model, errors, options) {
3838
return actions.setErrors(model, errors, {
39-
merge: isPlainObject(errors),
39+
merge: isObjectLike(errors),
4040
...options,
4141
});
4242
}

src/utils/is-plain-object.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Adapted from https://github.com/jonschlinkert/is-plain-object
2+
export function isObjectLike(val) {
3+
return val != null && typeof val === 'object';
4+
}
5+
26
function isObject(val) {
3-
return val != null && typeof val === 'object' && Array.isArray(val) === false;
7+
return isObjectLike(val) && Array.isArray(val) === false;
48
}
59

610
function isObjectObject(o) {

src/utils/merge-validity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import isPlainObject from './is-plain-object';
2-
import merge from './merge';
1+
import { isObjectLike } from './is-plain-object';
2+
import { merge } from 'icepick';
33

44
export default function mergeValidity(fieldValidity, actionValidity) {
5-
if (!isPlainObject(fieldValidity) || !isPlainObject(actionValidity)) {
5+
if (!isObjectLike(fieldValidity) || !isObjectLike(actionValidity)) {
66
// can't merge string/boolean validity with keyed validity
77
return actionValidity;
88
}
99

10-
return merge({ ...fieldValidity }, actionValidity);
10+
return merge(fieldValidity, actionValidity);
1111
}

test/form-reducer-actions-spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,36 @@ describe('formReducer() (V1)', () => {
425425
validated: true,
426426
},
427427
},
428+
{
429+
label: 'merging errors from array',
430+
action: actions.setErrors,
431+
model: 'user',
432+
args: [{ from: 'object' }, { merge: true }],
433+
initialState: {
434+
$form: {
435+
...initialFieldState,
436+
errors: ['array'],
437+
},
438+
},
439+
expectedForm: {
440+
errors: { 0: 'array', from: 'object' },
441+
},
442+
},
443+
{
444+
label: 'merging errors from object',
445+
action: actions.setErrors,
446+
model: 'user',
447+
args: [['array'], { merge: true }],
448+
initialState: {
449+
$form: {
450+
...initialFieldState,
451+
errors: { from: 'object' },
452+
},
453+
},
454+
expectedForm: {
455+
errors: { 0: 'array', from: 'object' },
456+
},
457+
},
428458
],
429459
[actionTypes.SET_FIELDS_VALIDITY]: [
430460
{

0 commit comments

Comments
 (0)