fix(striker-ui): add formik errors converter

main
Tsu-ba-me 1 year ago
parent 41055cc626
commit 990dfbb375
  1. 26
      striker-ui/lib/convertFormikErrorsToMessages.ts

@ -0,0 +1,26 @@
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;
Loading…
Cancel
Save