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.
This commit is contained in:
Lorenz Hilpert
2025-10-21 14:33:03 +02:00
parent 2b5da00249
commit a3835dd688

View File

@@ -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<typeof NotificationChannelSchema>;
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<number>(
(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<typeof NotificationChannelSchema>;