fix(striker-ui): correct types in get formik error messages

main
Tsu-ba-me 11 months ago
parent 62b2f69aa4
commit e1d6567097
  1. 4
      striker-ui/components/Files/AddFileForm.tsx
  2. 4
      striker-ui/components/Files/EditFileForm.tsx
  3. 26
      striker-ui/lib/convertFormikErrorsToMessages.ts
  4. 30
      striker-ui/lib/getFormikErrorMessages.ts

@ -13,9 +13,9 @@ import { v4 as uuidv4 } from 'uuid';
import ActionGroup from '../ActionGroup'; import ActionGroup from '../ActionGroup';
import api from '../../lib/api'; import api from '../../lib/api';
import ContainedButton from '../ContainedButton'; import ContainedButton from '../ContainedButton';
import convertFormikErrorsToMessages from '../../lib/convertFormikErrorsToMessages';
import FileInputGroup from './FileInputGroup'; import FileInputGroup from './FileInputGroup';
import FlexBox from '../FlexBox'; import FlexBox from '../FlexBox';
import getFormikErrorMessages from '../../lib/getFormikErrorMessages';
import handleAPIError from '../../lib/handleAPIError'; import handleAPIError from '../../lib/handleAPIError';
import MessageBox from '../MessageBox'; import MessageBox from '../MessageBox';
import MessageGroup from '../MessageGroup'; import MessageGroup from '../MessageGroup';
@ -111,7 +111,7 @@ const AddFileForm: FC<AddFileFormProps> = (props) => {
}); });
const formikErrors = useMemo<Messages>( const formikErrors = useMemo<Messages>(
() => convertFormikErrorsToMessages(formik.errors), () => getFormikErrorMessages(formik.errors),
[formik.errors], [formik.errors],
); );

@ -3,9 +3,9 @@ import { FC, useCallback, useMemo, useRef } from 'react';
import ActionGroup from '../ActionGroup'; import ActionGroup from '../ActionGroup';
import api from '../../lib/api'; import api from '../../lib/api';
import convertFormikErrorsToMessages from '../../lib/convertFormikErrorsToMessages';
import FileInputGroup from './FileInputGroup'; import FileInputGroup from './FileInputGroup';
import FlexBox from '../FlexBox'; import FlexBox from '../FlexBox';
import getFormikErrorMessages from '../../lib/getFormikErrorMessages';
import handleAPIError from '../../lib/handleAPIError'; import handleAPIError from '../../lib/handleAPIError';
import MessageGroup, { MessageGroupForwardedRefContent } from '../MessageGroup'; import MessageGroup, { MessageGroupForwardedRefContent } from '../MessageGroup';
import fileListSchema from './schema'; import fileListSchema from './schema';
@ -131,7 +131,7 @@ const EditFileForm: FC<EditFileFormProps> = (props) => {
}); });
const formikErrors = useMemo<Messages>( const formikErrors = useMemo<Messages>(
() => convertFormikErrorsToMessages(formik.errors), () => getFormikErrorMessages(formik.errors),
[formik.errors], [formik.errors],
); );

@ -1,26 +0,0 @@
const convertFormikErrorsToMessages = <Leaf extends string | undefined>(
errors: Tree<Leaf>,
{
build = (mkey, err) => ({ children: err, type: 'warning' }),
chain = '',
}: {
build?: (msgkey: keyof Tree, error: Leaf) => Messages[keyof Messages];
chain?: keyof Tree<Leaf>;
} = {},
): Messages =>
Object.entries(errors).reduce<Messages>((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;

@ -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<Messages>((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;
Loading…
Cancel
Save