feat: 增加条件节点正则匹配

- 使用 RE2/J 完成安全正则执行和分层校验

- 增加全宽多行输入、说明提示和专项测试
This commit is contained in:
2026-07-31 14:23:47 +08:00
parent 41b056b7e3
commit f0aba1eddd
11 changed files with 879 additions and 17 deletions

View File

@@ -0,0 +1,24 @@
import { describe, expect, it } from 'vitest';
import {
CONDITION_REGEX_MAX_LENGTH,
getConditionRegexError,
} from './conditionRegex';
describe('condition regex validation', () => {
it('接受常用语法、锚点和安全内联标志', () => {
expect(getConditionRegexError('(?i)^[a-z]+-\\d+$')).toBe('');
});
it('拒绝空值和超长表达式', () => {
expect(getConditionRegexError(' ')).toBe('请输入正则表达式');
expect(
getConditionRegexError('a'.repeat(CONDITION_REGEX_MAX_LENGTH + 1)),
).toContain(String(CONDITION_REGEX_MAX_LENGTH));
});
it('不使用浏览器正则语法拦截服务端负责的语法校验', () => {
expect(getConditionRegexError('VIP(?=用户)')).toBe('');
expect(getConditionRegexError('(VIP)-\\1')).toBe('');
});
});

View File

@@ -0,0 +1,17 @@
export const CONDITION_REGEX_MAX_LENGTH = 512;
/**
* 返回条件节点正则输入可即时确认的错误。
*
* 完整语法由后端 RE2/J 校验,避免浏览器正则语法差异误拦截有效配置。
*/
export const getConditionRegexError = (value: unknown) => {
const regex = String(value ?? '');
if (!regex.trim()) {
return '请输入正则表达式';
}
if (regex.length > CONDITION_REGEX_MAX_LENGTH) {
return `正则表达式不能超过 ${CONDITION_REGEX_MAX_LENGTH} 个字符`;
}
return '';
};