mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge pull request #739 from Hexastack/feat/refactor-zod-validation
feat: refactor zod validation
This commit is contained in:
commit
457d49ca27
@ -309,14 +309,9 @@ export type StdOutgoingEnvelope = z.infer<typeof stdOutgoingEnvelopeSchema>;
|
|||||||
|
|
||||||
// is-valid-message-text validation
|
// is-valid-message-text validation
|
||||||
export const validMessageTextSchema = z.object({
|
export const validMessageTextSchema = z.object({
|
||||||
message: z.string(),
|
text: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// is-message validation
|
|
||||||
const MESSAGE_REGEX = /^function \(context\) \{[^]+\}/;
|
|
||||||
|
|
||||||
export const messageRegexSchema = z.string().regex(MESSAGE_REGEX);
|
|
||||||
|
|
||||||
export const textSchema = z.array(z.string().max(1000));
|
export const textSchema = z.array(z.string().max(1000));
|
||||||
|
|
||||||
const quickReplySchema = z
|
const quickReplySchema = z
|
||||||
@ -391,6 +386,7 @@ const attachmentBlockMessageSchema = z.object({
|
|||||||
|
|
||||||
// BlockMessage Schema
|
// BlockMessage Schema
|
||||||
export const blockMessageObjectSchema = z.union([
|
export const blockMessageObjectSchema = z.union([
|
||||||
|
textSchema,
|
||||||
pluginBlockMessageSchema,
|
pluginBlockMessageSchema,
|
||||||
textBlockMessageSchema,
|
textBlockMessageSchema,
|
||||||
buttonMessageSchema,
|
buttonMessageSchema,
|
||||||
|
@ -16,36 +16,11 @@ import {
|
|||||||
import {
|
import {
|
||||||
BlockMessage,
|
BlockMessage,
|
||||||
blockMessageObjectSchema,
|
blockMessageObjectSchema,
|
||||||
messageRegexSchema,
|
|
||||||
textSchema,
|
|
||||||
} from '../schemas/types/message';
|
} from '../schemas/types/message';
|
||||||
|
|
||||||
/* eslint-disable no-console */
|
|
||||||
export function isValidMessage(msg: any) {
|
export function isValidMessage(msg: any) {
|
||||||
if (typeof msg === 'string' && msg !== '') {
|
return blockMessageObjectSchema.safeParse(msg).success;
|
||||||
const result = messageRegexSchema.safeParse(msg);
|
|
||||||
if (!result.success) {
|
|
||||||
console.error('Block Model: Invalid custom code.', result.error);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
} else if (Array.isArray(msg)) {
|
|
||||||
const result = textSchema.safeParse(msg);
|
|
||||||
if (!result.success) {
|
|
||||||
console.error('Block Model: Invalid text message array.', result.error);
|
|
||||||
}
|
|
||||||
return result.success;
|
|
||||||
} else if (typeof msg === 'object' && msg !== null) {
|
|
||||||
const result = blockMessageObjectSchema.safeParse(msg);
|
|
||||||
if (!result.success) {
|
|
||||||
console.error('Block Model: Object validation failed!', result.error);
|
|
||||||
}
|
|
||||||
return result.success;
|
|
||||||
}
|
|
||||||
console.log('Validation reached default false');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/* eslint-enable no-console */
|
|
||||||
|
|
||||||
@ValidatorConstraint({ async: false })
|
@ValidatorConstraint({ async: false })
|
||||||
export class MessageValidator implements ValidatorConstraintInterface {
|
export class MessageValidator implements ValidatorConstraintInterface {
|
||||||
|
Loading…
Reference in New Issue
Block a user