mirror of
https://github.com/hexastack/hexabot
synced 2025-02-22 12:28:26 +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 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([
|
export const patternSchema = z.union([
|
||||||
z.string(),
|
stringRegexPatternSchema,
|
||||||
z.instanceof(RegExp),
|
|
||||||
payloadPatternSchema,
|
payloadPatternSchema,
|
||||||
z.array(nlpPatternSchema),
|
z.array(nlpPatternSchema),
|
||||||
]);
|
]);
|
||||||
|
@ -31,7 +31,7 @@ import {
|
|||||||
OutgoingMessageFormat,
|
OutgoingMessageFormat,
|
||||||
StdOutgoingEnvelope,
|
StdOutgoingEnvelope,
|
||||||
} from '../schemas/types/message';
|
} 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 { Payload, StdQuickReply } from '../schemas/types/quick-reply';
|
||||||
import { SubscriberContext } from '../schemas/types/subscriberContext';
|
import { SubscriberContext } from '../schemas/types/subscriberContext';
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ export class BlockService extends BaseService<
|
|||||||
block: Block | BlockFull,
|
block: Block | BlockFull,
|
||||||
): (RegExpMatchArray | string)[] | false {
|
): (RegExpMatchArray | string)[] | false {
|
||||||
// Filter text patterns & Instanciate Regex patterns
|
// Filter text patterns & Instanciate Regex patterns
|
||||||
const patterns: undefined | Pattern[] = block.patterns?.map((pattern) => {
|
const patterns = block.patterns?.map((pattern) => {
|
||||||
if (
|
if (
|
||||||
typeof pattern === 'string' &&
|
typeof pattern === 'string' &&
|
||||||
pattern.endsWith('/') &&
|
pattern.endsWith('/') &&
|
||||||
|
Loading…
Reference in New Issue
Block a user