parent
62b2f69aa4
commit
e1d6567097
4 changed files with 34 additions and 30 deletions
@ -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…
Reference in new issue