Skip to content

Commit

Permalink
fix(console): input invalid format content in multitextinput will not…
Browse files Browse the repository at this point in the history
… crash the app
  • Loading branch information
charIeszhao committed Sep 7, 2022
1 parent 2e0ab89 commit 035be48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/console/src/components/MultiTextInput/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';

export const multiTextInputErrorGuard = z.object({
required: z.string().optional(),
inputs: z.record(z.number(), z.string().optional()).optional(),
inputs: z.record(z.number().or(z.string()), z.string().optional()).optional(),
});

export type MultiTextInputError = z.infer<typeof multiTextInputErrorGuard>;
Expand Down
10 changes: 9 additions & 1 deletion packages/console/src/components/MultiTextInput/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { t } from 'i18next';

import { safeParseJson } from '@/utilities/json';

import { MultiTextInputError, multiTextInputErrorGuard, MultiTextInputRule } from './types';

export const validate = (
Expand Down Expand Up @@ -57,7 +59,13 @@ export const convertRhfErrorMessage = (errorMessage?: string): MultiTextInputErr
return;
}

const result = multiTextInputErrorGuard.safeParse(errorMessage);
const jsonParseResult = safeParseJson(errorMessage);

if (!jsonParseResult.success) {
throw new Error(t('admin_console.errors.invalid_error_message_format'));
}

const result = multiTextInputErrorGuard.safeParse(jsonParseResult.data);

if (!result.success) {
throw new Error(t('admin_console.errors.invalid_error_message_format'));
Expand Down

0 comments on commit 035be48

Please sign in to comment.