You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
751 B
27 lines
751 B
1 year ago
|
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;
|