From 011bc69db917f200c5d3e0531407508c07c47458 Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Mon, 3 Feb 2025 14:58:57 +0100 Subject: [PATCH] feat: channelSchema validation with zod --- .../chat/validation-rules/is-channel-data.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/api/src/chat/validation-rules/is-channel-data.ts b/api/src/chat/validation-rules/is-channel-data.ts index 3dbd4c2d..57f67128 100644 --- a/api/src/chat/validation-rules/is-channel-data.ts +++ b/api/src/chat/validation-rules/is-channel-data.ts @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Hexastack. All rights reserved. + * Copyright © 2025 Hexastack. All rights reserved. * * Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. @@ -12,18 +12,21 @@ import { ValidatorConstraint, ValidatorConstraintInterface, } from 'class-validator'; +import { z } from 'zod'; -export function isChannelData(channel: any) { - return ( - typeof channel === 'object' && - channel.name && - typeof channel.name === 'string' - ); +export const channelSchema = z.object({ + name: z.string(), +}); + +export type Channel = z.infer; + +export function isChannelData(channel: Channel) { + return channelSchema.safeParse(channel).success; } @ValidatorConstraint({ async: false }) export class ChannelDataValidator implements ValidatorConstraintInterface { - validate(channel: any) { + validate(channel: Channel) { return isChannelData(channel); } }