From c212858221b08ed758ffd525fcd602e46dc60232 Mon Sep 17 00:00:00 2001 From: Pierre <63404022+0-Pierre@users.noreply.github.com> Date: Sat, 15 Mar 2025 13:09:19 +0100 Subject: [PATCH] refactor: enforce type validation using z.union for tmdbId and mbId --- server/interfaces/api/watchlistCreate.ts | 19 ++++++++++++------- server/routes/blacklist.ts | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/server/interfaces/api/watchlistCreate.ts b/server/interfaces/api/watchlistCreate.ts index f11fa507..1932d70c 100644 --- a/server/interfaces/api/watchlistCreate.ts +++ b/server/interfaces/api/watchlistCreate.ts @@ -1,10 +1,15 @@ import { MediaType } from '@server/constants/media'; import { z } from 'zod'; -export const watchlistCreate = z.object({ - ratingKey: z.coerce.string().optional(), - tmdbId: z.coerce.number().optional(), - mbId: z.coerce.string().optional(), - mediaType: z.nativeEnum(MediaType), - title: z.coerce.string().optional(), -}); +export const watchlistCreate = z + .object({ + ratingKey: z.coerce.string().optional(), + mediaType: z.nativeEnum(MediaType), + title: z.coerce.string().optional(), + }) + .and( + z.union([ + z.object({ tmdbId: z.coerce.number() }), + z.object({ mbId: z.coerce.string() }), + ]) + ); diff --git a/server/routes/blacklist.ts b/server/routes/blacklist.ts index 9d9cabc0..f07f3ee3 100644 --- a/server/routes/blacklist.ts +++ b/server/routes/blacklist.ts @@ -12,13 +12,18 @@ import { z } from 'zod'; const blacklistRoutes = Router(); -export const blacklistAdd = z.object({ - tmdbId: z.coerce.number().optional(), - mbId: z.string().optional(), - mediaType: z.nativeEnum(MediaType), - title: z.coerce.string().optional(), - user: z.coerce.number(), -}); +export const blacklistAdd = z + .object({ + mediaType: z.nativeEnum(MediaType), + title: z.coerce.string().optional(), + user: z.coerce.number(), + }) + .and( + z.union([ + z.object({ tmdbId: z.coerce.number() }), + z.object({ mbId: z.coerce.string() }), + ]) + ); const blacklistGet = z.object({ take: z.coerce.number().int().positive().default(25),