From 990dfbb375f3056b6e49345102ce941c8314901f Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 20 Sep 2023 00:08:12 -0400 Subject: [PATCH] fix(striker-ui): add formik errors converter --- .../lib/convertFormikErrorsToMessages.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 striker-ui/lib/convertFormikErrorsToMessages.ts diff --git a/striker-ui/lib/convertFormikErrorsToMessages.ts b/striker-ui/lib/convertFormikErrorsToMessages.ts new file mode 100644 index 00000000..fdae0b56 --- /dev/null +++ b/striker-ui/lib/convertFormikErrorsToMessages.ts @@ -0,0 +1,26 @@ +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;