From 26616337481ab2887013e4d38cd76b7534d1488b Mon Sep 17 00:00:00 2001 From: Beier Luo Date: Sun, 11 Dec 2022 18:32:28 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix=20#9571=20validation=20issue?= =?UTF-8?q?=20with=20unregister=20input=20with=20valueAsNumber?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/logic/validateField.test.tsx | 16 ++++++++++++++++ src/logic/validateField.ts | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/__tests__/logic/validateField.test.tsx b/src/__tests__/logic/validateField.test.tsx index 35ba933ec66..dbdfca19efe 100644 --- a/src/__tests__/logic/validateField.test.tsx +++ b/src/__tests__/logic/validateField.test.tsx @@ -57,6 +57,22 @@ describe('validateField', () => { }, }); + expect( + await validateField( + { + _f: { + valueAsNumber: true, + mount: true, + name: 'test', + ref: { name: 'test' }, + required: 'required', + }, + }, + 2, + false, + ), + ).toEqual({}); + expect( await validateField( { diff --git a/src/logic/validateField.ts b/src/logic/validateField.ts index 2591f66f8dd..13e66e4bc1d 100644 --- a/src/logic/validateField.ts +++ b/src/logic/validateField.ts @@ -17,6 +17,7 @@ import isObject from '../utils/isObject'; import isRadioInput from '../utils/isRadioInput'; import isRegex from '../utils/isRegex'; import isString from '../utils/isString'; +import isUndefined from '../utils/isUndefined'; import appendErrors from './appendErrors'; import getCheckboxValue from './getCheckboxValue'; @@ -61,7 +62,9 @@ export default async ( const isCheckBox = isCheckBoxInput(ref); const isRadioOrCheckbox = isRadio || isCheckBox; const isEmpty = - ((valueAsNumber || isFileInput(ref)) && !ref.value) || + ((valueAsNumber || isFileInput(ref)) && + isUndefined(ref.value) && + isUndefined(inputValue)) || inputValue === '' || (Array.isArray(inputValue) && !inputValue.length); const appendErrorsCurry = appendErrors.bind(