diff --git a/striker-ui/components/Files/AddFileForm.tsx b/striker-ui/components/Files/AddFileForm.tsx index 186e1e6f..9a96e4cc 100644 --- a/striker-ui/components/Files/AddFileForm.tsx +++ b/striker-ui/components/Files/AddFileForm.tsx @@ -13,9 +13,9 @@ import { v4 as uuidv4 } from 'uuid'; import ActionGroup from '../ActionGroup'; import api from '../../lib/api'; import ContainedButton from '../ContainedButton'; -import convertFormikErrorsToMessages from '../../lib/convertFormikErrorsToMessages'; import FileInputGroup from './FileInputGroup'; import FlexBox from '../FlexBox'; +import getFormikErrorMessages from '../../lib/getFormikErrorMessages'; import handleAPIError from '../../lib/handleAPIError'; import MessageBox from '../MessageBox'; import MessageGroup from '../MessageGroup'; @@ -111,7 +111,7 @@ const AddFileForm: FC = (props) => { }); const formikErrors = useMemo( - () => convertFormikErrorsToMessages(formik.errors), + () => getFormikErrorMessages(formik.errors), [formik.errors], ); diff --git a/striker-ui/components/Files/EditFileForm.tsx b/striker-ui/components/Files/EditFileForm.tsx index 4d540e58..a3ac96ab 100644 --- a/striker-ui/components/Files/EditFileForm.tsx +++ b/striker-ui/components/Files/EditFileForm.tsx @@ -3,9 +3,9 @@ import { FC, useCallback, useMemo, useRef } from 'react'; import ActionGroup from '../ActionGroup'; import api from '../../lib/api'; -import convertFormikErrorsToMessages from '../../lib/convertFormikErrorsToMessages'; import FileInputGroup from './FileInputGroup'; import FlexBox from '../FlexBox'; +import getFormikErrorMessages from '../../lib/getFormikErrorMessages'; import handleAPIError from '../../lib/handleAPIError'; import MessageGroup, { MessageGroupForwardedRefContent } from '../MessageGroup'; import fileListSchema from './schema'; @@ -131,7 +131,7 @@ const EditFileForm: FC = (props) => { }); const formikErrors = useMemo( - () => convertFormikErrorsToMessages(formik.errors), + () => getFormikErrorMessages(formik.errors), [formik.errors], ); diff --git a/striker-ui/lib/convertFormikErrorsToMessages.ts b/striker-ui/lib/convertFormikErrorsToMessages.ts deleted file mode 100644 index fdae0b56..00000000 --- a/striker-ui/lib/convertFormikErrorsToMessages.ts +++ /dev/null @@ -1,26 +0,0 @@ -const convertFormikErrorsToMessages = ( - errors: Tree, - { - build = (mkey, err) => ({ children: err, type: 'warning' }), - chain = '', - }: { - build?: (msgkey: keyof Tree, error: Leaf) => Messages[keyof Messages]; - chain?: keyof Tree; - } = {}, -): Messages => - Object.entries(errors).reduce((previous, [key, value]) => { - const extended = String(chain).length ? [chain, key].join('.') : key; - - if (typeof value === 'object') { - return { - ...previous, - ...convertFormikErrorsToMessages(value, { chain: extended }), - }; - } - - previous[extended] = build(extended, value); - - return previous; - }, {}); - -export default convertFormikErrorsToMessages; diff --git a/striker-ui/lib/getFormikErrorMessages.ts b/striker-ui/lib/getFormikErrorMessages.ts new file mode 100644 index 00000000..0707fb4e --- /dev/null +++ b/striker-ui/lib/getFormikErrorMessages.ts @@ -0,0 +1,30 @@ +const getFormikErrorMessages = ( + errors: object, + { + build = (field, error) => ({ children: error, type: 'warning' }), + chain = '', + skip, + }: { + build?: (field: string, error: unknown) => Message; + chain?: string; + skip?: (field: string) => boolean; + } = {}, +): Messages => + Object.entries(errors).reduce((previous, [key, value]) => { + const field = [chain, key].filter((part) => Boolean(part)).join('.'); + + if (value !== null && typeof value === 'object') { + return { + ...previous, + ...getFormikErrorMessages(value, { build, chain: field, skip }), + }; + } + + if (!skip?.call(null, field)) { + previous[field] = build(field, value); + } + + return previous; + }, {}); + +export default getFormikErrorMessages;