0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(console): input invalid format content in multitextinput will not crash the app

This commit is contained in:
Charles Zhao 2022-09-07 16:46:53 +08:00
parent 2e0ab8988a
commit 035be481cc
No known key found for this signature in database
GPG key ID: 4858774754C92DF2
2 changed files with 10 additions and 2 deletions

View file

@ -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>;

View file

@ -1,5 +1,7 @@
import { t } from 'i18next';
import { safeParseJson } from '@/utilities/json';
import { MultiTextInputError, multiTextInputErrorGuard, MultiTextInputRule } from './types';
export const validate = (
@ -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'));