@truenewx/tnxvue3 3.4.1 → 3.4.3

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.
@@ -1,371 +0,0 @@
1
- // tnxvue-validator.js
2
- /**
3
- * 校验规则转换器,将服务端元数据中的校验规则转换为async-validator组件的规则。
4
- * async-validator组件详见:https://github.com/yiminghe/async-validator
5
- */
6
- import validator from '@truenewx/tnxcore/src/tnxcore-validator';
7
- import AsyncValidator from 'async-validator';
8
-
9
- /**
10
- * async-validator组件支持的类型清单
11
- */
12
- const ruleTypes = ['string', 'number', 'boolean', 'method', 'regexp', 'integer', 'float', 'array', 'object', 'enum',
13
- 'date', 'url', 'hex', 'email', 'any'];
14
-
15
- function getRuleType(metaType) {
16
- if (ruleTypes.contains(metaType)) {
17
- return metaType;
18
- }
19
- switch (metaType) {
20
- case 'decimal':
21
- return 'float';
22
- case 'regex':
23
- return 'regexp';
24
- case 'datetime':
25
- case 'time':
26
- return 'date';
27
- }
28
- return ruleTypes[0];
29
- }
30
-
31
- function getRule(validationName, validationValue, fieldMeta) {
32
- let rule = undefined;
33
- let fieldCaption = '';
34
- // 据目前观察,字段格式校验的错误消息均显示在字段旁,无需显示字段名称,未来如果出现不在字段旁显示的场景,再考虑扩展
35
- // if (fieldMeta && fieldMeta.caption) {
36
- // fieldCaption = fieldMeta.caption;
37
- // }
38
- switch (validationName) {
39
- case 'required':
40
- case 'notNull':
41
- case 'notEmpty':
42
- case 'notBlank':
43
- if (validationValue === true) {
44
- rule = {
45
- required: true,
46
- validator(r, fieldValue, callback, source, options) {
47
- if (validationValue) {
48
- let blank = fieldValue === undefined || fieldValue === null;
49
- if (!blank) {
50
- if (Array.isArray(fieldValue)) {
51
- blank = fieldValue.length === 0; // 数组长度为0视为空
52
- } else if (typeof fieldValue === 'string') {
53
- blank = fieldValue.trim().length === 0; // 字符串去掉两端空格后长度为0视为空
54
- } else if (typeof fieldValue === 'number') {
55
- blank = isNaN(fieldValue); // 非法的数字视为空
56
- } else if (typeof fieldValue === 'object') {
57
- // 对象非日期,且没有字段的视为空
58
- blank = !(fieldValue instanceof Date) && Object.keys(fieldValue).length === 0;
59
- if (!blank) {
60
- // 可永久的日期对象,非永久且日期值为空时视为空
61
- if (typeof fieldValue.permanent === 'boolean') {
62
- blank = !fieldValue.permanent && !fieldValue.value;
63
- }
64
- }
65
- }
66
- }
67
- if (blank) {
68
- let message = validator.getErrorMessage(validationName, fieldCaption);
69
- return callback(new Error(message));
70
- }
71
- }
72
- return callback();
73
- }
74
- }
75
- }
76
- break;
77
- case 'minLength':
78
- rule = {
79
- validator(r, fieldValue, callback, source, options) {
80
- if (typeof validationValue === 'number' && typeof fieldValue === 'string') {
81
- // 回车符计入长度
82
- let enterLength = fieldValue.indexOf('\n') < 0 ? 0 : fieldValue.match(/\n/g).length;
83
- let fieldLength = fieldValue.length + enterLength;
84
- if (fieldLength < validationValue) {
85
- let message = validator.getErrorMessage(validationName, fieldCaption,
86
- validationValue, validationValue - fieldLength);
87
- return callback(new Error(message));
88
- }
89
- }
90
- return callback();
91
- }
92
- };
93
- break;
94
- case 'maxLength':
95
- rule = {
96
- validator(r, fieldValue, callback, source, options) {
97
- if (typeof validationValue === 'number' && typeof fieldValue === 'string') {
98
- // 回车符计入长度
99
- let enterLength = fieldValue.indexOf('\n') < 0 ? 0 : fieldValue.match(/\n/g).length;
100
- let fieldLength = fieldValue.length + enterLength;
101
- if (fieldLength > validationValue) {
102
- let message = validator.getErrorMessage(validationName, fieldCaption,
103
- validationValue, fieldLength - validationValue);
104
- return callback(new Error(message));
105
- }
106
- }
107
- return callback();
108
- }
109
- };
110
- break;
111
- case 'number':
112
- if (validationValue === true) {
113
- rule = {
114
- validator(r, fieldValue, callback, source, options) {
115
- if (fieldValue && typeof fieldValue === 'string') {
116
- if (!/^[0-9]+$/.test(fieldValue)) {
117
- let message = validator.getErrorMessage(validationName, fieldCaption);
118
- return callback(new Error(message));
119
- }
120
- }
121
- return callback();
122
- }
123
- };
124
- }
125
- break;
126
- case 'notContainsHtmlChars':
127
- if (validationValue === true) {
128
- rule = {
129
- validator(r, fieldValue, callback, source, options) {
130
- if (typeof fieldValue === 'string') {
131
- let limitedValues = ['<', '>', '\'', '"', '\\'];
132
- for (let i = 0; i < limitedValues.length; i++) {
133
- if (fieldValue.indexOf(limitedValues[i]) >= 0) {
134
- let s = limitedValues.join(' ');
135
- let message = validator.getErrorMessage('notContains', fieldCaption, s);
136
- return callback(new Error(message));
137
- }
138
- }
139
- }
140
- return callback();
141
- }
142
- };
143
- }
144
- break;
145
- case 'notContainsIllegalFilenameChars':
146
- if (validationValue === true) {
147
- rule = {
148
- validator(r, fieldValue, callback, source, options) {
149
- if (typeof fieldValue === 'string') {
150
- let limitedValues = ['/', '\\', ':', '*', '?', '"', '<', '>', '|'];
151
- for (let i = 0; i < limitedValues.length; i++) {
152
- if (fieldValue.indexOf(limitedValues[i]) >= 0) {
153
- let s = limitedValues.join(' ');
154
- let message = validator.getErrorMessage('notContains', fieldCaption, s);
155
- return callback(new Error(message));
156
- }
157
- }
158
- }
159
- return callback();
160
- }
161
- };
162
- }
163
- break;
164
- case 'notContains':
165
- if (validationValue) {
166
- rule = {
167
- validator(r, fieldValue, callback, source, options) {
168
- if (typeof fieldValue === 'string') {
169
- let limitedValues = validationValue.split(' ');
170
- for (let i = 0; i < limitedValues.length; i++) {
171
- if (fieldValue.indexOf(limitedValues[i]) >= 0) {
172
- let message = validator.getErrorMessage('notContains', fieldCaption,
173
- validationValue);
174
- return callback(new Error(message));
175
- }
176
- }
177
- }
178
- return callback();
179
- }
180
- };
181
- }
182
- break;
183
- case 'rejectHtmlTags':
184
- if (validationValue === true) {
185
- rule = {
186
- validator(r, fieldValue, callback, source, options) {
187
- if (fieldValue) {
188
- if (/<[a-z]+[ ]*[/]?[ ]*>/gi.test(fieldValue)) {
189
- let message = validator.getErrorMessage(validationName, fieldCaption);
190
- return callback(new Error(message));
191
- }
192
- }
193
- return callback();
194
- }
195
- };
196
- }
197
- break;
198
- case 'allowedHtmlTags':
199
- if (validationValue) {
200
- rule = {
201
- validator(r, fieldValue, callback, source, options) {
202
- if (typeof fieldValue === 'string') {
203
- let tags = validationValue.toLowerCase().split(',');
204
- if (tags.length) {
205
- fieldValue = fieldValue.toLowerCase();
206
- let leftIndex = fieldValue.indexOf('<');
207
- let rightIndex = leftIndex >= 0 ? fieldValue.indexOf('>', leftIndex) : -1;
208
- while (leftIndex >= 0 && rightIndex >= 0) {
209
- let sub = fieldValue.substring(leftIndex + 1, rightIndex); // <>中间的部分
210
- let spaceIndex = sub.indexOf(' ');
211
- let tag = spaceIndex >= 0 ? sub.substring(0, spaceIndex) : sub;
212
- if (tag.startsWith('/')) { // 标签结束处
213
- tag = tag.substring(1);
214
- }
215
- if (!tags.contains(tag.toLowerCase())) {
216
- let message = validator.getErrorMessage(validationName, fieldCaption,
217
- tags.join(', '));
218
- return callback(new Error(message)); // 存在不允许的标签,则报错
219
- }
220
- leftIndex = fieldValue.indexOf('<', rightIndex);
221
- rightIndex = leftIndex >= 0 ? fieldValue.indexOf('>', leftIndex) : -1;
222
- }
223
- }
224
- }
225
- return callback();
226
- }
227
- };
228
- }
229
- break;
230
- case 'forbiddenHtmlTags':
231
- if (validationValue) {
232
- rule = {
233
- validator(r, fieldValue, callback, source, options) {
234
- if (fieldValue) {
235
- let tags = validationValue.toLowerCase().split(',');
236
- if (tags.length) {
237
- fieldValue = fieldValue.toLowerCase();
238
- for (let tag of tags) {
239
- if (fieldValue.contains('<' + tag + '>') || fieldValue.contains('<' + tag + ' ')) {
240
- let message = validator.getErrorMessage(validationName, fieldCaption,
241
- tags.join(', '));
242
- return callback(new Error(message));
243
- }
244
- }
245
- }
246
- }
247
- return callback();
248
- }
249
- };
250
- }
251
- break;
252
- case 'email':
253
- if (validationValue === true) {
254
- rule = {
255
- type: validationName,
256
- message: validator.getErrorMessage(validationName, fieldCaption),
257
- }
258
- }
259
- break;
260
- case 'cellphone':
261
- case 'idCardNo':
262
- case 'url':
263
- case 'opposableUrl':
264
- rule = {
265
- validator(r, fieldValue, callback, source, options) {
266
- if (validationValue) {
267
- let message = validator.validateRegExp(validationName, fieldValue, fieldCaption);
268
- if (message) {
269
- return callback(new Error(message));
270
- }
271
- }
272
- return callback();
273
- }
274
- };
275
- break;
276
- case 'regex':
277
- rule = {
278
- validator(r, fieldValue, callback, source, options) {
279
- if (fieldValue) {
280
- let pattern = validationValue.pattern;
281
- // 服务端正则表达式无需以^$作为首尾,客户端需确保以^$作为首尾
282
- if (!pattern.startsWith('^')) {
283
- pattern = '^' + pattern;
284
- }
285
- if (!pattern.endsWith('$')) {
286
- pattern += '$';
287
- }
288
- let regexp = new RegExp(pattern, 'gi');
289
- if (!regexp.test(fieldValue)) {
290
- let message = validationValue.message;
291
- if (message) {
292
- message = fieldCaption + message;
293
- } else {
294
- message = validator.getErrorMessage('regex', fieldCaption, '');
295
- }
296
- return callback(new Error(message));
297
- }
298
- }
299
- return callback();
300
- }
301
- }
302
- break;
303
- case 'custom':
304
- if (typeof validationValue === 'function') {
305
- rule = {
306
- validator(r, fieldValue, callback, source, options) {
307
- let message = validationValue(fieldValue);
308
- if (message) {
309
- return callback(new Error(message));
310
- }
311
- return callback();
312
- }
313
- }
314
- }
315
- break;
316
- }
317
- if (rule) {
318
- rule.name = validationName;
319
- let metaType = 'text';
320
- if (fieldMeta && fieldMeta.type) {
321
- metaType = fieldMeta.type.toLowerCase();
322
- }
323
- rule.type = rule.type || getRuleType(metaType);
324
- let optional = metaType === 'option' || metaType === 'datetime' || metaType === 'date' || metaType === 'time';
325
- rule.trigger = optional ? 'change' : 'blur';
326
- }
327
- return rule;
328
- }
329
-
330
- /**
331
- * 从服务端元数据中构建完整的规则集
332
- * @param meta
333
- * @returns {{}}
334
- */
335
- export function getRules(meta) {
336
- let rules = {};
337
- Object.keys(meta).forEach(fieldName => {
338
- let fieldMeta = meta[fieldName];
339
- if (fieldMeta.validation) {
340
- let fieldRules = [];
341
- Object.keys(fieldMeta.validation).forEach(validationName => {
342
- let validationValue = fieldMeta.validation[validationName];
343
- let rule = getRule(validationName, validationValue, fieldMeta);
344
- if (rule) {
345
- fieldRules.push(rule);
346
- }
347
- });
348
- // 将可能包含的引用字段路径中的.替换为__,以符合async-validator规则名称的规范
349
- let ruleName = fieldName.replace('.', '__');
350
- rules[ruleName] = fieldRules;
351
- }
352
- });
353
- return rules;
354
- }
355
-
356
- export default {
357
- getErrorMessage: validator.getErrorMessage,
358
- testRegExp: validator.testRegExp,
359
- validateRegExp: validator.validateRegExp,
360
- getRule,
361
- getRules,
362
- /**
363
- * 校验字段
364
- * @param rules 包含所有字段校验规则的对象,格式:{字段名: [规则数组]}
365
- * @param model 待校验的模型对象,包含待校验的字段
366
- * @returns {Promise} then({errors,fields}=>{})
367
- */
368
- validate: function (rules, model) {
369
- return new AsyncValidator(rules).validate(model);
370
- },
371
- }