From e341f2d3da2adcb39820da4fdac3d0afc34917f7 Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Mon, 3 Feb 2025 14:37:55 +0100 Subject: [PATCH 1/2] feat: replace joi with zod captureVar --- .../chat/validation-rules/is-valid-capture.ts | 42 ++++--------------- api/src/utils/test/mocks/block.ts | 2 +- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/api/src/chat/validation-rules/is-valid-capture.ts b/api/src/chat/validation-rules/is-valid-capture.ts index 664280ea..3956af1e 100644 --- a/api/src/chat/validation-rules/is-valid-capture.ts +++ b/api/src/chat/validation-rules/is-valid-capture.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,43 +12,17 @@ import { ValidatorConstraint, ValidatorConstraintInterface, } from 'class-validator'; -import Joi from 'joi'; -type Tentity = -1 | -2; - -export interface CaptureVar { - // entity=`-1` to match text message - // entity=`-2` for postback payload - // entity is `String` for NLP entities - entity: Tentity | string; - context_var: string; -} - -const allowedEntityValues: Tentity[] = [-1, -2]; +import { CaptureVar, captureVarSchema } from '../schemas/types/capture-var'; export function isValidVarCapture(vars: CaptureVar[]) { - const captureSchema = Joi.array().items( - Joi.object().keys({ - entity: Joi.alternatives().try( - // `-1` to match text message & `-2` for postback payload - Joi.number() - .valid(...allowedEntityValues) - .required(), - // String for NLP entities - Joi.string().required(), - ), - context_var: Joi.string() - .regex(/^[a-z][a-z_0-9]*$/) - .required(), - }), - ); - - const captureCheck = captureSchema.validate(vars); - if (captureCheck.error) { - // eslint-disable-next-line - console.log('Capture vars validation failed!', captureCheck.error); + if (!Array.isArray(vars)) { + return false; } - return !captureCheck.error; + + return vars.every( + (captureVar) => captureVarSchema.safeParse(captureVar).success, + ); } @ValidatorConstraint({ async: false }) diff --git a/api/src/utils/test/mocks/block.ts b/api/src/utils/test/mocks/block.ts index 556d0399..7562e327 100644 --- a/api/src/utils/test/mocks/block.ts +++ b/api/src/utils/test/mocks/block.ts @@ -13,6 +13,7 @@ import { import { BlockFull } from '@/chat/schemas/block.schema'; import { FileType } from '@/chat/schemas/types/attachment'; import { ButtonType } from '@/chat/schemas/types/button'; +import { CaptureVar } from '@/chat/schemas/types/capture-var'; import { OutgoingMessageFormat, PayloadType, @@ -20,7 +21,6 @@ import { import { BlockOptions, ContentOptions } from '@/chat/schemas/types/options'; import { Pattern } from '@/chat/schemas/types/pattern'; import { QuickReplyType } from '@/chat/schemas/types/quick-reply'; -import { CaptureVar } from '@/chat/validation-rules/is-valid-capture'; import { modelInstance } from './misc'; From 5108169174d5081e79c96cf68353d53569228d7d Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Mon, 3 Feb 2025 15:14:22 +0100 Subject: [PATCH 2/2] fix: add regex for context_var --- api/src/chat/schemas/types/capture-var.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/chat/schemas/types/capture-var.ts b/api/src/chat/schemas/types/capture-var.ts index f6044cce..54a821e6 100644 --- a/api/src/chat/schemas/types/capture-var.ts +++ b/api/src/chat/schemas/types/capture-var.ts @@ -13,7 +13,7 @@ import { z } from 'zod'; // entity is `String` for NLP entities export const captureVarSchema = z.object({ entity: z.union([z.number().min(-2).max(-1), z.string()]), - context_var: z.string(), + context_var: z.string().regex(/^[a-z][a-z_0-9]*$/), }); export type CaptureVar = z.infer;