mirror of
https://github.com/hexastack/hexabot
synced 2025-02-22 20:38:32 +00:00
Merge pull request #766 from Hexastack/765-bug---zod-block-patterns-validation
fix(api): update string and regex string zod validations
This commit is contained in:
commit
dd89d97f3a
@ -32,9 +32,26 @@ export const nlpPatternSchema = z.discriminatedUnion('match', [
|
||||
|
||||
export type NlpPattern = z.infer<typeof nlpPatternSchema>;
|
||||
|
||||
export const stringRegexPatternSchema = z.string().refine(
|
||||
(value) => {
|
||||
if (value.startsWith('/') && value.endsWith('/')) {
|
||||
if (value.length === 2) return false;
|
||||
try {
|
||||
new RegExp(value.slice(1, -1), 'gi');
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return value !== '';
|
||||
},
|
||||
{
|
||||
message: 'Invalid regex or empty string',
|
||||
},
|
||||
);
|
||||
|
||||
export const patternSchema = z.union([
|
||||
z.string(),
|
||||
z.instanceof(RegExp),
|
||||
stringRegexPatternSchema,
|
||||
payloadPatternSchema,
|
||||
z.array(nlpPatternSchema),
|
||||
]);
|
||||
|
@ -31,7 +31,7 @@ import {
|
||||
OutgoingMessageFormat,
|
||||
StdOutgoingEnvelope,
|
||||
} from '../schemas/types/message';
|
||||
import { NlpPattern, Pattern, PayloadPattern } from '../schemas/types/pattern';
|
||||
import { NlpPattern, PayloadPattern } from '../schemas/types/pattern';
|
||||
import { Payload, StdQuickReply } from '../schemas/types/quick-reply';
|
||||
import { SubscriberContext } from '../schemas/types/subscriberContext';
|
||||
|
||||
@ -194,7 +194,7 @@ export class BlockService extends BaseService<
|
||||
block: Block | BlockFull,
|
||||
): (RegExpMatchArray | string)[] | false {
|
||||
// Filter text patterns & Instanciate Regex patterns
|
||||
const patterns: undefined | Pattern[] = block.patterns?.map((pattern) => {
|
||||
const patterns = block.patterns?.map((pattern) => {
|
||||
if (
|
||||
typeof pattern === 'string' &&
|
||||
pattern.endsWith('/') &&
|
||||
|
Loading…
Reference in New Issue
Block a user