From 372c7a49e1ee5d2b8d22dd6e638d75d8b2dc9f66 Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Sun, 24 Nov 2024 20:08:30 +0100 Subject: [PATCH] Added validation for location as well --- i18n.config.ts | 2 ++ models/CalendarEvent.ts | 2 +- server/api/calendars/events/[id].patch.ts | 2 +- server/api/calendars/events/create.post.ts | 18 +++++++++++++----- supabase/migrations/202401_init.sql | 3 ++- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/i18n.config.ts b/i18n.config.ts index 7653fcd..e655bfc 100644 --- a/i18n.config.ts +++ b/i18n.config.ts @@ -144,6 +144,7 @@ export default defineI18nConfig(() => ({ toastTitle: "Event wasn't updated.", title_too_big: "Title should be less than 240 characters long.", description_too_big: "Description should be less than 1200 characters long.", + location_too_big: "Location should be less than 240 characters long.", }, deleteDialog: { title: "Delete this event", @@ -357,6 +358,7 @@ export default defineI18nConfig(() => ({ toastTitle: "L'évènement n'a pas été modifié", title_too_big: "Le titre doit être inférieur à 240 caractères.", description_too_big: "La description doit être inférieure à 1200 caractères.", + location_too_big: "La localisation doit être inférieure à 240 caractères.", }, deleteDialog: { title: "Supprimer l'évènement", diff --git a/models/CalendarEvent.ts b/models/CalendarEvent.ts index 43cfc9e..cd6dfc2 100644 --- a/models/CalendarEvent.ts +++ b/models/CalendarEvent.ts @@ -22,7 +22,7 @@ export const postEventBodySchema = z.object({ event: z.object({ title: z.string().max(240), description: z.string().max(1200).optional().nullable(), - location: z.string().optional().nullable(), + location: z.string().max(240).optional().nullable(), startDate: dateSchema.required(), endDate: dateSchema.optional().nullable(), hidden: z.boolean().optional().nullable(), diff --git a/server/api/calendars/events/[id].patch.ts b/server/api/calendars/events/[id].patch.ts index 46ed8c2..ca0aa9a 100644 --- a/server/api/calendars/events/[id].patch.ts +++ b/server/api/calendars/events/[id].patch.ts @@ -27,7 +27,7 @@ export default defineEventHandler(async (event) => { fatal: false, statusCode: 401, statusMessage: "Validation Error", - message: "Erreur de validation du schéma, cela est probablement dû à une erreur utilisateur.", + message: "Erreur de validation du schéma, probablement dûe à une erreur utilisateur.", data: { errors: bodyError.issues.map(issue => ({ path: issue.path, diff --git a/server/api/calendars/events/create.post.ts b/server/api/calendars/events/create.post.ts index 46efe29..c0fe7f0 100644 --- a/server/api/calendars/events/create.post.ts +++ b/server/api/calendars/events/create.post.ts @@ -7,14 +7,22 @@ export default defineEventHandler(async (event) => { const { data: bodyData, error: schemaError } = await readValidatedBody(event, body => postEventBodySchema.safeParse(body)) if (schemaError) { - throw createError({ + const error = createError({ cause: "Utilisateur", fatal: false, - message: "Le schéma de la requête n'est pas complet ou mal renseigné.", - data: schemaError.issues, - statusMessage: "test", - status: 401, + statusCode: 401, + statusMessage: "Validation Error", + message: "Erreur de validation du schéma, probablement dûe à une erreur utilisateur.", + data: { + errors: schemaError.issues.map(issue => ({ + path: issue.path, + message: issue.message, + code: issue.code + })) + } }) + + throw error } try { diff --git a/supabase/migrations/202401_init.sql b/supabase/migrations/202401_init.sql index efb4546..96d19a4 100644 --- a/supabase/migrations/202401_init.sql +++ b/supabase/migrations/202401_init.sql @@ -93,7 +93,8 @@ alter table public.calendar_events add constraint calendar_events_maxlen_check check ( char_length(title) <= 240 AND - (description IS NULL OR char_length(description) <= 1200) + (description IS NULL OR char_length(description) <= 1200) AND + (location IS NULL OR char_length(location) <= 240) ); -- Link table for events - categories