mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Validate pattern
This commit is contained in:
@@ -2134,10 +2134,13 @@ export function initDefaultSlashCommands() {
|
|||||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
name: 'test',
|
name: 'test',
|
||||||
callback: (({ pattern }, text) => {
|
callback: (({ pattern }, text) => {
|
||||||
if (pattern === '') {
|
if (!pattern) {
|
||||||
throw new Error('Argument of \'pattern=\' cannot be empty');
|
throw new Error('Argument of \'pattern=\' cannot be empty');
|
||||||
}
|
}
|
||||||
let re = regexFromString(pattern.toString());
|
let re = regexFromString(pattern.toString());
|
||||||
|
if (!re) {
|
||||||
|
throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
|
||||||
|
}
|
||||||
return JSON.stringify(re.test(text.toString()));
|
return JSON.stringify(re.test(text.toString()));
|
||||||
}),
|
}),
|
||||||
returns: 'true | false',
|
returns: 'true | false',
|
||||||
@@ -2170,10 +2173,13 @@ export function initDefaultSlashCommands() {
|
|||||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||||
name: 'match',
|
name: 'match',
|
||||||
callback: (({ pattern }, text) => {
|
callback: (({ pattern }, text) => {
|
||||||
if (pattern === '') {
|
if (!pattern) {
|
||||||
throw new Error('Argument of \'pattern=\' cannot be empty');
|
throw new Error('Argument of \'pattern=\' cannot be empty');
|
||||||
}
|
}
|
||||||
let re = regexFromString(pattern.toString());
|
let re = regexFromString(pattern.toString());
|
||||||
|
if (!re) {
|
||||||
|
throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
|
||||||
|
}
|
||||||
if (re.flags.includes('g')) {
|
if (re.flags.includes('g')) {
|
||||||
return JSON.stringify([...text.toString().matchAll(re)]);
|
return JSON.stringify([...text.toString().matchAll(re)]);
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user