From a3835dd688299b36439242c1880533cb9f7b23c5 Mon Sep 17 00:00:00 2001 From: Lorenz Hilpert Date: Tue, 21 Oct 2025 14:33:03 +0200 Subject: [PATCH] refactor(common): add validation for notification channel flag combinations Add refine validation to NotificationChannelSchema to ensure only valid flag combinations are accepted. Computes valid flags using bitwise OR of all enum values. --- .../schemas/notification-channel.schema.ts | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/libs/common/data-access/src/lib/schemas/notification-channel.schema.ts b/libs/common/data-access/src/lib/schemas/notification-channel.schema.ts index 3514ac348..3c6777823 100644 --- a/libs/common/data-access/src/lib/schemas/notification-channel.schema.ts +++ b/libs/common/data-access/src/lib/schemas/notification-channel.schema.ts @@ -1,14 +1,24 @@ -import z from 'zod'; - -export const NotificationChannel = { - NotSet: 0, - Email: 1, - SMS: 2, - Phone: 4, - Fax: 8, - Postal: 16, -} as const; - -export const NotificationChannelSchema = z.nativeEnum(NotificationChannel).describe('Notification channel'); - -export type NotificationChannel = z.infer; +import z from 'zod'; + +export const NotificationChannel = { + NotSet: 0, + Email: 1, + SMS: 2, + Phone: 4, + Fax: 8, + Postal: 16, +} as const; + +const ALL_FLAGS = Object.values(NotificationChannel).reduce( + (a, b) => a | b, + 0, +); + +export const NotificationChannelSchema = z + .nativeEnum(NotificationChannel) + .refine((val) => (val & ALL_FLAGS) === val, { + message: 'Invalid notification channel', + }) + .describe('Notification channel'); + +export type NotificationChannel = z.infer;