befly 3.8.20 → 3.8.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+ import { Validator } from '../lib/validator';
3
+
4
+ const validator = new Validator();
5
+
6
+ describe('Validator - 基本验证', () => {
7
+ test('验证通过', () => {
8
+ const data = { username: 'john' };
9
+ const rules = { username: { name: '用户名', type: 'string', min: 2, max: 20 } };
10
+ const result = validator.validate(data, rules);
11
+ expect(result.code).toBe(0);
12
+ expect(Object.keys(result.fields).length).toBe(0);
13
+ });
14
+
15
+ test('验证失败 - 必填字段缺失', () => {
16
+ const data = {};
17
+ const rules = { username: { name: '用户名', type: 'string', min: 2, max: 20 } };
18
+ const required = ['username'];
19
+ const result = validator.validate(data, rules, required);
20
+ expect(result.code).toBe(1);
21
+ expect(result.fields.username).toContain('必填项');
22
+ });
23
+
24
+ test('验证失败 - 字段值为空', () => {
25
+ const data = { username: '' };
26
+ const rules = { username: { name: '用户名', type: 'string', min: 2, max: 20 } };
27
+ const required = ['username'];
28
+ const result = validator.validate(data, rules, required);
29
+ expect(result.code).toBe(1);
30
+ expect(result.fields.username).toBeDefined();
31
+ });
32
+ });
33
+
34
+ describe('Validator - 类型验证', () => {
35
+ test('string 类型', () => {
36
+ const data = { name: 'test' };
37
+ const rules = { name: { name: '名称', type: 'string', min: 2, max: 10 } };
38
+ const result = validator.validate(data, rules);
39
+ expect(result.code).toBe(0);
40
+ });
41
+
42
+ test('number 类型', () => {
43
+ const data = { age: 25 };
44
+ const rules = { age: { name: '年龄', type: 'number', min: 0, max: 150 } };
45
+ const result = validator.validate(data, rules);
46
+ expect(result.code).toBe(0);
47
+ });
48
+
49
+ test('email 验证', () => {
50
+ const data = { email: 'test@example.com' };
51
+ const rules = { email: { name: '邮箱', type: 'string', regexp: '@email' } };
52
+ const result = validator.validate(data, rules);
53
+ expect(result.code).toBe(0);
54
+ });
55
+
56
+ test('phone 验证', () => {
57
+ const data = { phone: '13800138000' };
58
+ const rules = { phone: { name: '手机号', type: 'string', regexp: '@phone' } };
59
+ const result = validator.validate(data, rules);
60
+ expect(result.code).toBe(0);
61
+ });
62
+ });
63
+
64
+ describe('Validator - 长度验证', () => {
65
+ test('string 最小长度', () => {
66
+ const data = { name: 'a' };
67
+ const rules = { name: { name: '名称', type: 'string', min: 2, max: 10 } };
68
+ const result = validator.validate(data, rules);
69
+ expect(result.code).toBe(1);
70
+ expect(result.fields.name).toBeDefined();
71
+ });
72
+
73
+ test('string 最大长度', () => {
74
+ const data = { name: 'a'.repeat(20) };
75
+ const rules = { name: { name: '名称', type: 'string', min: 2, max: 10 } };
76
+ const result = validator.validate(data, rules);
77
+ expect(result.code).toBe(1);
78
+ expect(result.fields.name).toBeDefined();
79
+ });
80
+
81
+ test('number 范围验证', () => {
82
+ const data = { age: 200 };
83
+ const rules = { age: { name: '年龄', type: 'number', min: 0, max: 150 } };
84
+ const result = validator.validate(data, rules);
85
+ expect(result.code).toBe(1);
86
+ expect(result.fields.age).toBeDefined();
87
+ });
88
+ });
89
+
90
+ describe('Validator - 正则验证', () => {
91
+ test('正则别名 @email - 有效邮箱', () => {
92
+ const data = { email: 'test@example.com' };
93
+ const rules = { email: { name: '邮箱', type: 'string', regexp: '@email' } };
94
+ const result = validator.validate(data, rules);
95
+ expect(result.code).toBe(0);
96
+ });
97
+
98
+ test('正则别名 @email - 无效邮箱', () => {
99
+ const data = { email: 'invalid-email' };
100
+ const rules = { email: { name: '邮箱', type: 'string', regexp: '@email' } };
101
+ const result = validator.validate(data, rules);
102
+ expect(result.code).toBe(1);
103
+ expect(result.fields.email).toBeDefined();
104
+ });
105
+
106
+ test('正则别名 @phone - 有效手机号', () => {
107
+ const data = { phone: '13800138000' };
108
+ const rules = { phone: { name: '手机号', type: 'string', regexp: '@phone' } };
109
+ const result = validator.validate(data, rules);
110
+ expect(result.code).toBe(0);
111
+ });
112
+
113
+ test('正则别名 @phone - 无效手机号', () => {
114
+ const data = { phone: '12345' };
115
+ const rules = { phone: { name: '手机号', type: 'string', regexp: '@phone' } };
116
+ const result = validator.validate(data, rules);
117
+ expect(result.code).toBe(1);
118
+ expect(result.fields.phone).toBeDefined();
119
+ });
120
+
121
+ test('自定义正则 - 纯数字', () => {
122
+ const data = { code: '12a' };
123
+ const rules = { code: { name: '验证码', type: 'string', regexp: '^\\d+$' } };
124
+ const result = validator.validate(data, rules);
125
+ expect(result.code).toBe(1);
126
+ expect(result.fields.code).toBeDefined();
127
+ });
128
+ });
129
+
130
+ describe('Validator - 参数检查', () => {
131
+ test('data 不是对象', () => {
132
+ const result = validator.validate(null as any, {});
133
+ expect(result.code).toBe(1);
134
+ expect(result.fields.error).toContain('对象格式');
135
+ });
136
+
137
+ test('rules 不是对象', () => {
138
+ const result = validator.validate({}, null as any);
139
+ expect(result.code).toBe(1);
140
+ expect(result.fields.error).toContain('对象格式');
141
+ });
142
+
143
+ test('required 不是数组', () => {
144
+ const result = validator.validate({}, {}, 'invalid' as any);
145
+ expect(result.code).toBe(1);
146
+ expect(result.fields.error).toContain('数组格式');
147
+ });
148
+ });
@@ -0,0 +1,101 @@
1
+ import { describe, test, expect } from 'bun:test';
2
+ import { Xml } from '../lib/xml';
3
+
4
+ describe('Xml - 基本解析', () => {
5
+ test('解析简单元素', () => {
6
+ const xml = new Xml();
7
+ const result = xml.parse('<root>Hello</root>');
8
+ expect(result).toBe('Hello');
9
+ });
10
+
11
+ test('解析嵌套元素', () => {
12
+ const xml = new Xml();
13
+ const result = xml.parse('<root><child>Value</child></root>');
14
+ expect(result).toEqual({ child: 'Value' });
15
+ });
16
+
17
+ test('解析多个同名元素', () => {
18
+ const xml = new Xml();
19
+ const result = xml.parse('<root><item>A</item><item>B</item></root>');
20
+ expect(result).toEqual({ item: ['A', 'B'] });
21
+ });
22
+
23
+ test('解析空元素', () => {
24
+ const xml = new Xml();
25
+ const result = xml.parse('<root></root>');
26
+ expect(result).toBe('');
27
+ });
28
+
29
+ test('解析自闭合标签', () => {
30
+ const xml = new Xml();
31
+ const result = xml.parse('<root><item/></root>');
32
+ expect(result).toEqual({ item: '' });
33
+ });
34
+ });
35
+
36
+ describe('Xml - 属性解析', () => {
37
+ test('解析元素属性', () => {
38
+ const xml = new Xml();
39
+ const result = xml.parse('<root id="1">Value</root>') as any;
40
+ expect(result['@id']).toBe('1');
41
+ expect(result['#text']).toBe('Value');
42
+ });
43
+
44
+ test('解析多个属性', () => {
45
+ const xml = new Xml();
46
+ const result = xml.parse('<user id="123" name="John">Content</user>') as any;
47
+ expect(result['@id']).toBe('123');
48
+ expect(result['@name']).toBe('John');
49
+ expect(result['#text']).toBe('Content');
50
+ });
51
+
52
+ test('忽略属性', () => {
53
+ const xml = new Xml({ ignoreAttributes: true });
54
+ const result = xml.parse('<root id="1">Value</root>');
55
+ expect(result).toBe('Value');
56
+ });
57
+ });
58
+
59
+ describe('Xml - 数值解析', () => {
60
+ test('自动解析数字', () => {
61
+ const xml = new Xml();
62
+ const result = xml.parse('<root>123</root>');
63
+ expect(result).toBe(123);
64
+ });
65
+
66
+ test('自动解析布尔值', () => {
67
+ const xml = new Xml();
68
+ const result1 = xml.parse('<root>true</root>');
69
+ const result2 = xml.parse('<root>false</root>');
70
+ expect(result1).toBe(true);
71
+ expect(result2).toBe(false);
72
+ });
73
+
74
+ test('禁用数字解析', () => {
75
+ const xml = new Xml({ parseNumbers: false });
76
+ const result = xml.parse('<root>123</root>');
77
+ expect(result).toBe('123');
78
+ });
79
+ });
80
+
81
+ describe('Xml - 错误处理', () => {
82
+ test('拒绝非字符串输入', () => {
83
+ const xml = new Xml();
84
+ expect(() => xml.parse(123 as any)).toThrow('无效的 XML 数据');
85
+ });
86
+
87
+ test('拒绝空字符串', () => {
88
+ const xml = new Xml();
89
+ expect(() => xml.parse('')).toThrow('非空字符串');
90
+ });
91
+
92
+ test('拒绝只有空格的字符串', () => {
93
+ const xml = new Xml();
94
+ expect(() => xml.parse(' ')).toThrow('非空字符串');
95
+ });
96
+
97
+ test('检测未闭合标签', () => {
98
+ const xml = new Xml();
99
+ expect(() => xml.parse('<root><item>')).toThrow('未找到结束标签');
100
+ });
101
+ });