mirror of
https://github.com/hexastack/hexabot
synced 2025-02-22 12:28:26 +00:00
feat: replace joi with zod captureVar
This commit is contained in:
parent
a3cecf0977
commit
e341f2d3da
@ -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 })
|
||||
|
@ -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';
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user