react-hook-form 7.41.8-beta.0 → 7.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -665,8 +665,9 @@ var getValueAndMessage = (validationData) => isObject(validationData) && !isRege
665
665
  message: '',
666
666
  };
667
667
 
668
- var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
668
+ var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
669
669
  const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
670
+ const inputValue = get(formValues, name);
670
671
  if (!mount || disabled) {
671
672
  return {};
672
673
  }
@@ -797,7 +798,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
797
798
  }
798
799
  if (validate) {
799
800
  if (isFunction(validate)) {
800
- const result = await validate(inputValue);
801
+ const result = await validate(inputValue, formValues);
801
802
  const validateError = getValidateError(result, inputRef);
802
803
  if (validateError) {
803
804
  error[name] = {
@@ -816,7 +817,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
816
817
  if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
817
818
  break;
818
819
  }
819
- const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
820
+ const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
820
821
  if (validateError) {
821
822
  validationResult = {
822
823
  ...validateError,
@@ -906,31 +907,21 @@ function isEmptyArray(obj) {
906
907
  return true;
907
908
  }
908
909
  function unset(object, path) {
909
- const updatePath = isKey(path) ? [path] : stringToPath(path);
910
- const childObject = updatePath.length == 1 ? object : baseGet(object, updatePath);
911
- const key = updatePath[updatePath.length - 1];
912
- let previousObjRef;
910
+ const paths = Array.isArray(path)
911
+ ? path
912
+ : isKey(path)
913
+ ? [path]
914
+ : stringToPath(path);
915
+ const childObject = paths.length === 1 ? object : baseGet(object, paths);
916
+ const index = paths.length - 1;
917
+ const key = paths[index];
913
918
  if (childObject) {
914
919
  delete childObject[key];
915
920
  }
916
- for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
917
- let index = -1;
918
- let objectRef;
919
- const currentPaths = updatePath.slice(0, -(k + 1));
920
- const currentPathsLength = currentPaths.length - 1;
921
- if (k > 0) {
922
- previousObjRef = object;
923
- }
924
- while (++index < currentPaths.length) {
925
- const item = currentPaths[index];
926
- objectRef = objectRef ? objectRef[item] : object[item];
927
- if (currentPathsLength === index &&
928
- ((isObject(objectRef) && isEmptyObject(objectRef)) ||
929
- (Array.isArray(objectRef) && isEmptyArray(objectRef)))) {
930
- previousObjRef ? delete previousObjRef[item] : delete object[item];
931
- }
932
- previousObjRef = objectRef;
933
- }
921
+ if (index !== 0 &&
922
+ ((isObject(childObject) && isEmptyObject(childObject)) ||
923
+ (Array.isArray(childObject) && isEmptyArray(childObject)))) {
924
+ unset(object, paths.slice(0, -1));
934
925
  }
935
926
  return object;
936
927
  }
@@ -1112,7 +1103,7 @@ function useFieldArray(props) {
1112
1103
  else {
1113
1104
  const field = get(control._fields, name);
1114
1105
  if (field && field._f) {
1115
- validateField(field, get(control._formValues, name), control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1106
+ validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1116
1107
  control._subjects.state.next({
1117
1108
  errors: updateFieldArrayRootError(control._formState.errors, error, name),
1118
1109
  }));
@@ -1612,7 +1603,7 @@ function createFormControl(props = {}, flushRootRender) {
1612
1603
  const { _f, ...fieldValue } = field;
1613
1604
  if (_f) {
1614
1605
  const isFieldArrayRoot = _names.array.has(_f.name);
1615
- const fieldError = await validateField(field, get(_formValues, _f.name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
1606
+ const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
1616
1607
  if (fieldError[_f.name]) {
1617
1608
  context.valid = false;
1618
1609
  if (shouldOnlyCheckValid) {
@@ -1792,7 +1783,7 @@ function createFormControl(props = {}, flushRootRender) {
1792
1783
  isValid = isEmptyObject(errors);
1793
1784
  }
1794
1785
  else {
1795
- error = (await validateField(field, get(_formValues, name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1786
+ error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1796
1787
  if (error) {
1797
1788
  isValid = false;
1798
1789
  }
@@ -1994,48 +1985,37 @@ function createFormControl(props = {}, flushRootRender) {
1994
1985
  e.preventDefault && e.preventDefault();
1995
1986
  e.persist && e.persist();
1996
1987
  }
1997
- let hasNoPromiseError = true;
1998
1988
  let fieldValues = cloneObject(_formValues);
1999
1989
  _subjects.state.next({
2000
1990
  isSubmitting: true,
2001
1991
  });
2002
- try {
2003
- if (_options.resolver) {
2004
- const { errors, values } = await _executeSchema();
2005
- _formState.errors = errors;
2006
- fieldValues = values;
2007
- }
2008
- else {
2009
- await executeBuiltInValidation(_fields);
2010
- }
2011
- if (isEmptyObject(_formState.errors)) {
2012
- _subjects.state.next({
2013
- errors: {},
2014
- isSubmitting: true,
2015
- });
2016
- await onValid(fieldValues, e);
2017
- }
2018
- else {
2019
- if (onInvalid) {
2020
- await onInvalid({ ..._formState.errors }, e);
2021
- }
2022
- _focusError();
2023
- }
1992
+ if (_options.resolver) {
1993
+ const { errors, values } = await _executeSchema();
1994
+ _formState.errors = errors;
1995
+ fieldValues = values;
2024
1996
  }
2025
- catch (err) {
2026
- hasNoPromiseError = false;
2027
- throw err;
1997
+ else {
1998
+ await executeBuiltInValidation(_fields);
2028
1999
  }
2029
- finally {
2030
- _formState.isSubmitted = true;
2000
+ if (isEmptyObject(_formState.errors)) {
2031
2001
  _subjects.state.next({
2032
- isSubmitted: true,
2033
- isSubmitting: false,
2034
- isSubmitSuccessful: isEmptyObject(_formState.errors) && hasNoPromiseError,
2035
- submitCount: _formState.submitCount + 1,
2036
- errors: _formState.errors,
2002
+ errors: {},
2037
2003
  });
2004
+ await onValid(fieldValues, e);
2005
+ }
2006
+ else {
2007
+ if (onInvalid) {
2008
+ await onInvalid({ ..._formState.errors }, e);
2009
+ }
2010
+ _focusError();
2038
2011
  }
2012
+ _subjects.state.next({
2013
+ isSubmitted: true,
2014
+ isSubmitting: false,
2015
+ isSubmitSuccessful: isEmptyObject(_formState.errors),
2016
+ submitCount: _formState.submitCount + 1,
2017
+ errors: _formState.errors,
2018
+ });
2039
2019
  };
2040
2020
  const resetField = (name, options = {}) => {
2041
2021
  if (get(_fields, name)) {