mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: initial commit
This commit is contained in:
42
api/src/chat/validation-rules/is-channel-data.ts
Normal file
42
api/src/chat/validation-rules/is-channel-data.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright © 2024 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.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
|
||||
*/
|
||||
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
} from 'class-validator';
|
||||
|
||||
export function isChannelData(channel: any) {
|
||||
return (
|
||||
typeof channel === 'object' &&
|
||||
channel.name &&
|
||||
typeof channel.name === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class ChannelDataValidator implements ValidatorConstraintInterface {
|
||||
validate(channel: any) {
|
||||
return isChannelData(channel);
|
||||
}
|
||||
}
|
||||
|
||||
export function IsChannelData(validationOptions?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: ChannelDataValidator,
|
||||
});
|
||||
};
|
||||
}
|
||||
131
api/src/chat/validation-rules/is-message.ts
Normal file
131
api/src/chat/validation-rules/is-message.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright © 2024 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.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
|
||||
*/
|
||||
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
} from 'class-validator';
|
||||
import Joi from 'joi';
|
||||
|
||||
import { BlockMessage } from '../schemas/types/message';
|
||||
|
||||
export function isValidMessage(msg: any) {
|
||||
if (typeof msg === 'string' && msg !== '') {
|
||||
// Custom code
|
||||
const MESSAGE_REGEX = /^function \(context\) \{[^]+\}/;
|
||||
if (!MESSAGE_REGEX.test(msg)) {
|
||||
// eslint-disable-next-line
|
||||
console.error('Block Model : Invalid custom code.', msg);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else if (Array.isArray(msg)) {
|
||||
// Simple text message
|
||||
const textSchema = Joi.array().items(Joi.string().max(1000).required());
|
||||
const textCheck = textSchema.validate(msg);
|
||||
return !textCheck.error;
|
||||
} else if (typeof msg === 'object') {
|
||||
if ('plugin' in msg) {
|
||||
return true;
|
||||
} else {
|
||||
const buttonsSchema = Joi.array().items(
|
||||
Joi.object().keys({
|
||||
type: Joi.string().valid('postback', 'web_url').required(),
|
||||
title: Joi.string().max(20),
|
||||
payload: Joi.alternatives().conditional('type', {
|
||||
is: 'postback',
|
||||
then: Joi.string().max(1000).required(),
|
||||
otherwise: Joi.forbidden(),
|
||||
}),
|
||||
url: Joi.alternatives().conditional('type', {
|
||||
is: 'web_url',
|
||||
then: Joi.string().uri(),
|
||||
otherwise: Joi.forbidden(),
|
||||
}),
|
||||
messenger_extensions: Joi.alternatives().conditional('type', {
|
||||
is: 'web_url',
|
||||
then: Joi.boolean(),
|
||||
otherwise: Joi.forbidden(),
|
||||
}),
|
||||
webview_height_ratio: Joi.alternatives().conditional('type', {
|
||||
is: 'web_url',
|
||||
then: Joi.string().valid('compact', 'tall', 'full'),
|
||||
otherwise: Joi.forbidden(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
// Attachment message
|
||||
const objectSchema = Joi.object().keys({
|
||||
text: Joi.string().max(1000),
|
||||
attachment: Joi.object().keys({
|
||||
type: Joi.string()
|
||||
.valid('image', 'audio', 'video', 'file', 'unknown')
|
||||
.required(),
|
||||
payload: Joi.object().keys({
|
||||
url: Joi.string().uri(),
|
||||
attachment_id: Joi.string(),
|
||||
}),
|
||||
}),
|
||||
elements: Joi.boolean(),
|
||||
cards: Joi.object().keys({
|
||||
default_action: buttonsSchema.max(1),
|
||||
buttons: buttonsSchema.max(3),
|
||||
}),
|
||||
buttons: buttonsSchema.max(3),
|
||||
quickReplies: Joi.array()
|
||||
.items(
|
||||
Joi.object().keys({
|
||||
content_type: Joi.string()
|
||||
.valid('text', 'location', 'user_phone_number', 'user_email')
|
||||
.required(),
|
||||
title: Joi.alternatives().conditional('content_type', {
|
||||
is: 'text',
|
||||
then: Joi.string().max(20).required(),
|
||||
}),
|
||||
payload: Joi.alternatives().conditional('content_type', {
|
||||
is: 'text',
|
||||
then: Joi.string().max(1000).required(),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.max(11),
|
||||
});
|
||||
const objectCheck = objectSchema.validate(msg);
|
||||
if (objectCheck.error) {
|
||||
// eslint-disable-next-line
|
||||
console.log('Message validation failed! ', objectCheck);
|
||||
}
|
||||
return !objectCheck.error;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class MessageValidator implements ValidatorConstraintInterface {
|
||||
validate(msg: BlockMessage) {
|
||||
return isValidMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
export function IsMessage(validationOptions?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: MessageValidator,
|
||||
});
|
||||
};
|
||||
}
|
||||
91
api/src/chat/validation-rules/is-pattern-list.ts
Normal file
91
api/src/chat/validation-rules/is-pattern-list.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright © 2024 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.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
|
||||
*/
|
||||
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
} from 'class-validator';
|
||||
import Joi from 'joi';
|
||||
|
||||
import { Pattern } from '../schemas/types/pattern';
|
||||
|
||||
export function isPatternList(patterns: Pattern[]) {
|
||||
return (
|
||||
Array.isArray(patterns) &&
|
||||
patterns.every((pattern) => {
|
||||
if (typeof pattern === 'string') {
|
||||
// Check if valid regex
|
||||
if (pattern.endsWith('/') && pattern.startsWith('/')) {
|
||||
try {
|
||||
new RegExp(pattern.slice(1, -1), 'gi');
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Check if valid string (Equals/Like)
|
||||
return pattern !== '';
|
||||
} else if (Array.isArray(pattern)) {
|
||||
// Check if valid NLP pattern
|
||||
const nlpSchema = Joi.array()
|
||||
.items(
|
||||
Joi.object().keys({
|
||||
entity: Joi.string().required(),
|
||||
match: Joi.string().valid('entity', 'value').required(),
|
||||
value: Joi.string().required(),
|
||||
}),
|
||||
)
|
||||
.min(1);
|
||||
const nlpCheck = nlpSchema.validate(pattern);
|
||||
if (nlpCheck.error) {
|
||||
// console.log('Message validation failed! ', nlpCheck);
|
||||
}
|
||||
return !nlpCheck.error;
|
||||
} else if (typeof pattern === 'object') {
|
||||
// Invalid structure?
|
||||
const payloadSchema = Joi.object().keys({
|
||||
label: Joi.string().required(),
|
||||
value: Joi.any().required(),
|
||||
type: Joi.string(),
|
||||
});
|
||||
const payloadCheck = payloadSchema.validate(pattern);
|
||||
if (payloadCheck.error) {
|
||||
// console.log(
|
||||
// 'Message validation failed! ',
|
||||
// payloadCheck,
|
||||
// );
|
||||
}
|
||||
return !payloadCheck.error;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class PatternListValidator implements ValidatorConstraintInterface {
|
||||
validate(patterns: Pattern[]) {
|
||||
return isPatternList(patterns);
|
||||
}
|
||||
}
|
||||
|
||||
export function IsPatternList(validationOptions?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: PatternListValidator,
|
||||
});
|
||||
};
|
||||
}
|
||||
48
api/src/chat/validation-rules/is-position.ts
Normal file
48
api/src/chat/validation-rules/is-position.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright © 2024 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.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
|
||||
*/
|
||||
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
ValidatorConstraint,
|
||||
ValidatorConstraintInterface,
|
||||
} from 'class-validator';
|
||||
|
||||
import { Position } from '../schemas/types/position';
|
||||
|
||||
export function isPosition(position: Position) {
|
||||
return (
|
||||
typeof position === 'object' &&
|
||||
!isNaN(position.x) &&
|
||||
!isNaN(position.y) &&
|
||||
position.x !== Infinity &&
|
||||
position.x !== -Infinity &&
|
||||
position.y !== Infinity &&
|
||||
position.y !== -Infinity
|
||||
);
|
||||
}
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class PositionValidator implements ValidatorConstraintInterface {
|
||||
validate(position: Position) {
|
||||
return isPosition(position);
|
||||
}
|
||||
}
|
||||
|
||||
export function IsPosition(validationOptions?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: PositionValidator,
|
||||
});
|
||||
};
|
||||
}
|
||||
72
api/src/chat/validation-rules/is-valid-capture.ts
Normal file
72
api/src/chat/validation-rules/is-valid-capture.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright © 2024 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.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
|
||||
*/
|
||||
|
||||
import {
|
||||
registerDecorator,
|
||||
ValidationOptions,
|
||||
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];
|
||||
|
||||
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);
|
||||
}
|
||||
return !captureCheck.error;
|
||||
}
|
||||
|
||||
@ValidatorConstraint({ async: false })
|
||||
export class CaptureVarValidator implements ValidatorConstraintInterface {
|
||||
validate(vars: CaptureVar[]) {
|
||||
return isValidVarCapture(vars);
|
||||
}
|
||||
}
|
||||
|
||||
export function IsVarCapture(validationOptions?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: CaptureVarValidator,
|
||||
});
|
||||
};
|
||||
}
|
||||
30
api/src/chat/validation-rules/is-valid-message-text.ts
Normal file
30
api/src/chat/validation-rules/is-valid-message-text.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright © 2024 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.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
* 3. SaaS Restriction: This software, or any derivative of it, may not be used to offer a competing product or service (SaaS) without prior written consent from Hexastack. Offering the software as a service or using it in a commercial cloud environment without express permission is strictly prohibited.
|
||||
*/
|
||||
|
||||
import { registerDecorator, ValidationOptions } from 'class-validator';
|
||||
|
||||
import {
|
||||
StdIncomingMessage,
|
||||
StdOutgoingTextMessage,
|
||||
} from '../schemas/types/message';
|
||||
|
||||
export function IsValidMessageText(validationOptions?: ValidationOptions) {
|
||||
return function (object: object, propertyName: string) {
|
||||
registerDecorator({
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
validator: {
|
||||
validate(message: StdOutgoingTextMessage | StdIncomingMessage) {
|
||||
return !!(message as StdOutgoingTextMessage).text;
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user