Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isEmpty check in validateField for custom field components #11179

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/__tests__/logic/validateFieldArrayState.test.tsx
@@ -0,0 +1,40 @@
import validateField from '../../logic/validateField';
import * as isHTMLElement from '../../utils/isHTMLElement';

jest.mock('../../utils/isHTMLElement', () => ({
__esModule: true,
default: jest.fn(),
}));

describe('validateField', () => {
it('should not detect isEmpty when handling a custom built select', async () => {
jest.spyOn(isHTMLElement, 'default').mockImplementation(() => {
return true;
});

/**
* With a custom built select component it can happen that the
* referenced element does not have a value attribute.
* But the state is correctly set at the moment of validation.
*/
expect(
await validateField(
{
_f: {
mount: true,
name: 'test',
ref: { value: '', name: 'test' },
required: true,
},
},
{
test: [
{ label: 'Blackberry', value: 'blackberry' },
{ label: 'Banana', value: 'banana' },
],
},
false,
),
).toEqual({});
});
});
12 changes: 8 additions & 4 deletions src/logic/validateField.ts
Expand Up @@ -15,7 +15,6 @@ import isCheckBoxInput from '../utils/isCheckBoxInput';
import isEmptyObject from '../utils/isEmptyObject';
import isFileInput from '../utils/isFileInput';
import isFunction from '../utils/isFunction';
import isHTMLElement from '../utils/isHTMLElement';
import isMessage from '../utils/isMessage';
import isNullOrUndefined from '../utils/isNullOrUndefined';
import isObject from '../utils/isObject';
Expand Down Expand Up @@ -67,13 +66,18 @@ export default async <T extends FieldValues>(
const isRadio = isRadioInput(ref);
const isCheckBox = isCheckBoxInput(ref);
const isRadioOrCheckbox = isRadio || isCheckBox;
const isInputValueEmpty =
isUndefined(inputValue) ||
inputValue === null ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the additional check is just check null?

inputValue === '' ||
(Array.isArray(inputValue) && !inputValue.length);

const isEmpty =
((valueAsNumber || isFileInput(ref)) &&
isUndefined(ref.value) &&
isUndefined(inputValue)) ||
(isHTMLElement(ref) && ref.value === '') ||
inputValue === '' ||
(Array.isArray(inputValue) && !inputValue.length);
isInputValueEmpty;

const appendErrorsCurry = appendErrors.bind(
null,
name,
Expand Down