Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
]).then(([fieldErrors, schemaErrors, validateErrors]) => {
const combinedErrors = deepmerge.all<FormikErrors<Values>>(
[fieldErrors, schemaErrors, validateErrors],
{ arrayMerge }
{ arrayMerge, isMergeableObject }
);
return combinedErrors;
});
Expand Down Expand Up @@ -1119,6 +1119,35 @@ export function prepareDataForValidation<T extends FormikValues>(
return data;
}

/**
* Custom `isMergeableObject` for deepmerge that excludes React elements.
*
* Validation error values may be arbitrary React nodes (e.g. rich error
* messages rendered in the UI). React elements are plain objects whose
* `props` can contain nested elements, so deepmerge would recurse into them
* indefinitely and throw "Maximum call stack size exceeded". Marking elements
* (and other non-plain objects) as non-mergeable lets them pass through
* untouched.
*/
const REACT_ELEMENT_TYPE =
typeof Symbol === 'function' && Symbol.for
? Symbol.for('react.element')
: 0xeac7;

function isMergeableObject(value: any): boolean {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
return false;
}
if ((value as any).$$typeof === REACT_ELEMENT_TYPE) {
return false;
}
return (
!(value instanceof Date) &&
!(value instanceof RegExp) &&
typeof value.then !== 'function'
);
}

/**
* deepmerge array merging algorithm
* https://github.com/KyleAMathews/deepmerge#combine-array
Expand Down