feat: add validation to widget edit modal inputs (#879)

* feat: add validation to widget edit modal inputs

* chore: remove unused console.log statements
This commit is contained in:
Meier Lukas
2024-07-28 19:38:19 +02:00
committed by GitHub
parent 851f5e486d
commit 9cb6200895
5 changed files with 74 additions and 43 deletions

View File

@@ -11,10 +11,11 @@ export const zodErrorMap = <
) => {
return (issue: z.ZodIssueOptionalMessage, ctx: ErrorMapCtx) => {
const error = handleZodError(issue, ctx);
if ("message" in error && error.message)
if ("message" in error && error.message) {
return {
message: error.message,
};
}
return {
message: t(error.key ? `common.zod.${error.key}` : "common.zod.errors.default", error.params ?? {}),
};
@@ -103,6 +104,7 @@ const handleZodError = (issue: z.ZodIssueOptionalMessage, ctx: ErrorMapCtx) => {
params: {},
} as const;
}
if (issue.code === ZodIssueCode.invalid_string) {
return handleStringError(issue);
}
@@ -112,6 +114,12 @@ const handleZodError = (issue: z.ZodIssueOptionalMessage, ctx: ErrorMapCtx) => {
if (issue.code === ZodIssueCode.too_big) {
return handleTooBigError(issue);
}
if (issue.code === ZodIssueCode.invalid_type && ctx.data === "") {
return {
key: "errors.required",
params: {},
} as const;
}
if (issue.code === ZodIssueCode.custom && issue.params?.i18n) {
const { i18n } = issue.params as CustomErrorParams;
return {

View File

@@ -14,9 +14,18 @@ const searchCityInput = z.object({
query: z.string(),
});
const searchCityOutput = z.object({
results: z.array(citySchema),
});
const searchCityOutput = z
.object({
results: z.array(citySchema),
})
.or(
z
.object({
generationtime_ms: z.number(),
})
.refine((data) => Object.keys(data).length === 1, { message: "Invalid response" })
.transform(() => ({ results: [] })), // We fallback to empty array if no results
);
export const locationSchemas = {
searchCity: {