react-hook-form 7.41.2 → 7.42.0-next.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.
@@ -656,8 +656,9 @@ var getValueAndMessage = (validationData) => isObject(validationData) && !isRege
656
656
  message: '',
657
657
  };
658
658
 
659
- var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
659
+ var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
660
660
  const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
661
+ const inputValue = get(formValues, name);
661
662
  if (!mount || disabled) {
662
663
  return {};
663
664
  }
@@ -788,7 +789,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
788
789
  }
789
790
  if (validate) {
790
791
  if (isFunction(validate)) {
791
- const result = await validate(inputValue);
792
+ const result = await validate(inputValue, formValues);
792
793
  const validateError = getValidateError(result, inputRef);
793
794
  if (validateError) {
794
795
  error[name] = {
@@ -807,7 +808,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
807
808
  if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
808
809
  break;
809
810
  }
810
- const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
811
+ const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
811
812
  if (validateError) {
812
813
  validationResult = {
813
814
  ...validateError,
@@ -897,31 +898,21 @@ function isEmptyArray(obj) {
897
898
  return true;
898
899
  }
899
900
  function unset(object, path) {
900
- const updatePath = isKey(path) ? [path] : stringToPath(path);
901
- const childObject = updatePath.length == 1 ? object : baseGet(object, updatePath);
902
- const key = updatePath[updatePath.length - 1];
903
- let previousObjRef;
901
+ const paths = Array.isArray(path)
902
+ ? path
903
+ : isKey(path)
904
+ ? [path]
905
+ : stringToPath(path);
906
+ const childObject = paths.length === 1 ? object : baseGet(object, paths);
907
+ const index = paths.length - 1;
908
+ const key = paths[index];
904
909
  if (childObject) {
905
910
  delete childObject[key];
906
911
  }
907
- for (let k = 0; k < updatePath.slice(0, -1).length; k++) {
908
- let index = -1;
909
- let objectRef;
910
- const currentPaths = updatePath.slice(0, -(k + 1));
911
- const currentPathsLength = currentPaths.length - 1;
912
- if (k > 0) {
913
- previousObjRef = object;
914
- }
915
- while (++index < currentPaths.length) {
916
- const item = currentPaths[index];
917
- objectRef = objectRef ? objectRef[item] : object[item];
918
- if (currentPathsLength === index &&
919
- ((isObject(objectRef) && isEmptyObject(objectRef)) ||
920
- (Array.isArray(objectRef) && isEmptyArray(objectRef)))) {
921
- previousObjRef ? delete previousObjRef[item] : delete object[item];
922
- }
923
- previousObjRef = objectRef;
924
- }
912
+ if (index !== 0 &&
913
+ ((isObject(childObject) && isEmptyObject(childObject)) ||
914
+ (Array.isArray(childObject) && isEmptyArray(childObject)))) {
915
+ unset(object, paths.slice(0, -1));
925
916
  }
926
917
  return object;
927
918
  }
@@ -1103,7 +1094,7 @@ function useFieldArray(props) {
1103
1094
  else {
1104
1095
  const field = get(control._fields, name);
1105
1096
  if (field && field._f) {
1106
- validateField(field, get(control._formValues, name), control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1097
+ validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) &&
1107
1098
  control._subjects.state.next({
1108
1099
  errors: updateFieldArrayRootError(control._formState.errors, error, name),
1109
1100
  }));
@@ -1609,7 +1600,7 @@ function createFormControl(props = {}, flushRootRender) {
1609
1600
  const { _f, ...fieldValue } = field;
1610
1601
  if (_f) {
1611
1602
  const isFieldArrayRoot = _names.array.has(_f.name);
1612
- const fieldError = await validateField(field, get(_formValues, _f.name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
1603
+ const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
1613
1604
  if (fieldError[_f.name]) {
1614
1605
  context.valid = false;
1615
1606
  if (shouldOnlyCheckValid) {
@@ -1696,8 +1687,7 @@ function createFormControl(props = {}, flushRootRender) {
1696
1687
  }
1697
1688
  (options.shouldDirty || options.shouldTouch) &&
1698
1689
  updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
1699
- options.shouldValidate &&
1700
- trigger(name);
1690
+ options.shouldValidate && trigger(name);
1701
1691
  };
1702
1692
  const setValues = (name, value, options) => {
1703
1693
  for (const fieldKey in value) {
@@ -1790,7 +1780,7 @@ function createFormControl(props = {}, flushRootRender) {
1790
1780
  isValid = isEmptyObject(errors);
1791
1781
  }
1792
1782
  else {
1793
- error = (await validateField(field, get(_formValues, name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1783
+ error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1794
1784
  if (error) {
1795
1785
  isValid = false;
1796
1786
  }
@@ -1992,48 +1982,37 @@ function createFormControl(props = {}, flushRootRender) {
1992
1982
  e.preventDefault && e.preventDefault();
1993
1983
  e.persist && e.persist();
1994
1984
  }
1995
- let hasNoPromiseError = true;
1996
1985
  let fieldValues = cloneObject(_formValues);
1997
1986
  _subjects.state.next({
1998
1987
  isSubmitting: true,
1999
1988
  });
2000
- try {
2001
- if (_options.resolver) {
2002
- const { errors, values } = await _executeSchema();
2003
- _formState.errors = errors;
2004
- fieldValues = values;
2005
- }
2006
- else {
2007
- await executeBuiltInValidation(_fields);
2008
- }
2009
- if (isEmptyObject(_formState.errors)) {
2010
- _subjects.state.next({
2011
- errors: {},
2012
- isSubmitting: true,
2013
- });
2014
- await onValid(fieldValues, e);
2015
- }
2016
- else {
2017
- if (onInvalid) {
2018
- await onInvalid({ ..._formState.errors }, e);
2019
- }
2020
- _focusError();
2021
- }
1989
+ if (_options.resolver) {
1990
+ const { errors, values } = await _executeSchema();
1991
+ _formState.errors = errors;
1992
+ fieldValues = values;
2022
1993
  }
2023
- catch (err) {
2024
- hasNoPromiseError = false;
2025
- throw err;
1994
+ else {
1995
+ await executeBuiltInValidation(_fields);
2026
1996
  }
2027
- finally {
2028
- _formState.isSubmitted = true;
1997
+ if (isEmptyObject(_formState.errors)) {
2029
1998
  _subjects.state.next({
2030
- isSubmitted: true,
2031
- isSubmitting: false,
2032
- isSubmitSuccessful: isEmptyObject(_formState.errors) && hasNoPromiseError,
2033
- submitCount: _formState.submitCount + 1,
2034
- errors: _formState.errors,
1999
+ errors: {},
2035
2000
  });
2001
+ await onValid(fieldValues, e);
2002
+ }
2003
+ else {
2004
+ if (onInvalid) {
2005
+ await onInvalid({ ..._formState.errors }, e);
2006
+ }
2007
+ _focusError();
2036
2008
  }
2009
+ _subjects.state.next({
2010
+ isSubmitted: true,
2011
+ isSubmitting: false,
2012
+ isSubmitSuccessful: isEmptyObject(_formState.errors),
2013
+ submitCount: _formState.submitCount + 1,
2014
+ errors: _formState.errors,
2015
+ });
2037
2016
  };
2038
2017
  const resetField = (name, options = {}) => {
2039
2018
  if (get(_fields, name)) {