perf: 文件名特殊字符限制输入

This commit is contained in:
xiaojunnuo
2024-10-25 22:49:05 +08:00
parent a90d1e68ee
commit c4164c66e2
4 changed files with 65 additions and 11 deletions
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { isDomain } from "/@/plugin/validator";
import { isDomain, isFilePath } from "/@/plugin/validator";
describe("domain_validator", () => {
it("ok", () => {
@@ -41,4 +41,35 @@ describe("domain_validator", () => {
value = [".cc.com"];
expect(test).to.throw(Error, "域名有误:.cc.com,请输入正确的域名");
});
it("isFilePath", () => {
let value = "/a/$/bc";
function test() {
return isFilePath({}, value);
}
expect(test()).to.be.true;
value = "/a/&/bc";
expect(test()).to.be.true;
//*?“<>|等特殊字符
value = "/a/&/b>c.txt";
const errorMessage = '文件名不能包含*?"<>|等特殊字符';
expect(test).to.throw(Error, errorMessage);
value = "/a/&/b<c.txt";
expect(test).to.throw(Error, errorMessage);
value = "/a/&/b|c.txt";
expect(test).to.throw(Error, errorMessage);
value = "/a/&/b?c.txt";
expect(test).to.throw(Error, errorMessage);
value = "/a/&/b*c.txt";
expect(test).to.throw(Error, errorMessage);
});
});
@@ -21,5 +21,18 @@ export function isDomain(rule: any, value: any) {
}
return true;
}
// 注册自定义验证器
Validator.register("domains", isDomain);
export function isFilePath(rule: any, value: any) {
if (value == null) {
return true;
}
// 文件名不能用*?"<>|等特殊符号
if (!/^[^*?"<>|]*$/.test(value)) {
throw new Error(`文件名不能包含*?"<>|等特殊字符`);
}
return true;
}
Validator.register("filepath", isFilePath);