ph-utils 0.2.22 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.md +11 -11
- package/lib/date.d.ts +55 -34
- package/lib/date.js +203 -126
- package/lib/dom.d.ts +92 -92
- package/lib/dom.js +190 -190
- package/lib/file.d.ts +31 -34
- package/lib/file.js +96 -99
- package/lib/index.d.ts +97 -61
- package/lib/index.js +171 -82
- package/lib/server.d.ts +32 -39
- package/lib/server.js +77 -93
- package/lib/validator.d.ts +47 -47
- package/lib/validator.js +212 -215
- package/lib/web.d.ts +13 -55
- package/lib/web.js +57 -141
- package/package.json +10 -9
- package/lib/date_m.d.ts +0 -34
- package/lib/date_m.js +0 -119
- package/lib/index_m.d.ts +0 -61
- package/lib/index_m.js +0 -82
- package/lib/validator_m.d.ts +0 -47
- package/lib/validator_m.js +0 -210
package/lib/validator_m.js
DELETED
@@ -1,210 +0,0 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2
|
-
/**
|
3
|
-
* 数据验证器
|
4
|
-
*/
|
5
|
-
// 默认的错误提示信息
|
6
|
-
const defaultMsgs = {
|
7
|
-
mobile: '请输入正确的手机号',
|
8
|
-
same: '两次输入不一致',
|
9
|
-
required: '%s为必填字段',
|
10
|
-
};
|
11
|
-
const defaultMsg = '请输入正确的数据';
|
12
|
-
// 一些常用的验证正则
|
13
|
-
const ruleRegexs = {
|
14
|
-
/** 验证跟其余数据相等的正则,一般用于验证再次输入密码 */
|
15
|
-
same: /^same:(.+)$/i,
|
16
|
-
/** 验证手机号的正则表达式 */
|
17
|
-
mobile: /^1[34578]\d{9}$/,
|
18
|
-
/** 非空验证的正则表达式 */
|
19
|
-
required: /^\S{1}.*/,
|
20
|
-
};
|
21
|
-
// 规则比对函数
|
22
|
-
const ruleFns = {
|
23
|
-
/** 验证相等 */
|
24
|
-
same(val1, val2) {
|
25
|
-
return val2 === val1;
|
26
|
-
},
|
27
|
-
/** 正则匹配 */
|
28
|
-
pattern(regex, val) {
|
29
|
-
if (val == null) {
|
30
|
-
return false;
|
31
|
-
}
|
32
|
-
return regex.test(String(val));
|
33
|
-
},
|
34
|
-
};
|
35
|
-
class ValidateError extends Error {
|
36
|
-
constructor(key, msg) {
|
37
|
-
super(msg);
|
38
|
-
this.name = 'ValidateError';
|
39
|
-
this.key = key;
|
40
|
-
}
|
41
|
-
}
|
42
|
-
/**
|
43
|
-
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
44
|
-
*/
|
45
|
-
class Validator {
|
46
|
-
/**
|
47
|
-
* 构造数据验证转换器
|
48
|
-
* @param schemas 配置验证转换规则
|
49
|
-
*/
|
50
|
-
constructor(schemas) {
|
51
|
-
let parsedRules = {};
|
52
|
-
for (let schema of schemas) {
|
53
|
-
// 解析规则
|
54
|
-
let rules = [];
|
55
|
-
let rule = schema.rules;
|
56
|
-
if (typeof rule === 'string') {
|
57
|
-
rules = rules.concat(this._parseStringRule(rule, schema.message));
|
58
|
-
}
|
59
|
-
else if (rule instanceof Array) {
|
60
|
-
for (let ruleItem of rule) {
|
61
|
-
if (typeof ruleItem === 'string') {
|
62
|
-
rules.push(...this._parseStringRule(ruleItem, schema.message));
|
63
|
-
}
|
64
|
-
else if (ruleItem instanceof RegExp ||
|
65
|
-
typeof ruleItem === 'function') {
|
66
|
-
rules.push({
|
67
|
-
rule: ruleItem,
|
68
|
-
message: schema.message || defaultMsg,
|
69
|
-
});
|
70
|
-
}
|
71
|
-
else {
|
72
|
-
if (typeof ruleItem.rule === 'string') {
|
73
|
-
rules.push(...this._parseStringRule(ruleItem.rule, ruleItem.message));
|
74
|
-
}
|
75
|
-
else {
|
76
|
-
rules.push({
|
77
|
-
rule: ruleItem.rule,
|
78
|
-
message: ruleItem.message || defaultMsg,
|
79
|
-
});
|
80
|
-
}
|
81
|
-
}
|
82
|
-
}
|
83
|
-
}
|
84
|
-
else {
|
85
|
-
rules.push({ rule, message: defaultMsg });
|
86
|
-
}
|
87
|
-
if (schema.required === true &&
|
88
|
-
rules.findIndex((r) => r.rule === 'required') === -1) {
|
89
|
-
rules.push(this._parseStringRule('required'));
|
90
|
-
}
|
91
|
-
parsedRules[schema.key] = rules;
|
92
|
-
}
|
93
|
-
this.rules = parsedRules;
|
94
|
-
}
|
95
|
-
/**
|
96
|
-
* 进行数据验证
|
97
|
-
* @param data 待验证的数据
|
98
|
-
* @returns
|
99
|
-
*/
|
100
|
-
validate(data) {
|
101
|
-
return new Promise((resolve, reject) => {
|
102
|
-
let errMsg = '';
|
103
|
-
let errKey = '';
|
104
|
-
for (let key in this.rules) {
|
105
|
-
// eslint-disable-next-line no-prototype-builtins
|
106
|
-
if (this.rules.hasOwnProperty(key)) {
|
107
|
-
errMsg = this._validateRule(this.rules[key], data[key], data);
|
108
|
-
if (errMsg !== '') {
|
109
|
-
errKey = key;
|
110
|
-
errMsg = errMsg.replace('%s', key);
|
111
|
-
break;
|
112
|
-
}
|
113
|
-
}
|
114
|
-
}
|
115
|
-
if (errMsg === '') {
|
116
|
-
resolve(true);
|
117
|
-
}
|
118
|
-
else {
|
119
|
-
reject(new ValidateError(errKey, errMsg));
|
120
|
-
}
|
121
|
-
});
|
122
|
-
}
|
123
|
-
/**
|
124
|
-
* 只验证指定 key 的数据格式
|
125
|
-
* @param key 指定待验证的 key
|
126
|
-
* @param value 待验证的数据
|
127
|
-
*/
|
128
|
-
validateKey(key, value, data) {
|
129
|
-
return new Promise((resolve, reject) => {
|
130
|
-
let keyRules = this.rules[key];
|
131
|
-
let errMsg = this._validateRule(keyRules, value, data);
|
132
|
-
if (errMsg !== '') {
|
133
|
-
errMsg = errMsg.replace('%s', key);
|
134
|
-
reject(new ValidateError(key, errMsg));
|
135
|
-
}
|
136
|
-
else {
|
137
|
-
resolve(true);
|
138
|
-
}
|
139
|
-
});
|
140
|
-
}
|
141
|
-
_validateRule(rules, value, data) {
|
142
|
-
let errMsg = '';
|
143
|
-
for (let rule of rules) {
|
144
|
-
// 如果数据为空,则判断是否是必填
|
145
|
-
if (rule.rule === 'required') {
|
146
|
-
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
147
|
-
errMsg = rule.message;
|
148
|
-
}
|
149
|
-
}
|
150
|
-
if (typeof rule.rule === 'function') {
|
151
|
-
if (!rule.rule(value)) {
|
152
|
-
errMsg = rule.message;
|
153
|
-
}
|
154
|
-
}
|
155
|
-
else if (rule.sameKey != null) {
|
156
|
-
if (data != null) {
|
157
|
-
if (!ruleFns.same(value, data[rule.sameKey])) {
|
158
|
-
errMsg = rule.message;
|
159
|
-
}
|
160
|
-
}
|
161
|
-
}
|
162
|
-
else if (rule.rule === 'required') {
|
163
|
-
if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
|
164
|
-
errMsg = rule.message;
|
165
|
-
}
|
166
|
-
}
|
167
|
-
else {
|
168
|
-
if (!ruleFns.pattern(rule.rule, String(value))) {
|
169
|
-
errMsg = rule.message;
|
170
|
-
}
|
171
|
-
}
|
172
|
-
if (errMsg !== '') {
|
173
|
-
break;
|
174
|
-
}
|
175
|
-
}
|
176
|
-
return errMsg;
|
177
|
-
}
|
178
|
-
_parseStringRule(rule, ruleErrMsg) {
|
179
|
-
let rules = [];
|
180
|
-
let trule = rule.split('|');
|
181
|
-
for (let r of trule) {
|
182
|
-
let message = ruleErrMsg;
|
183
|
-
let rrule = null;
|
184
|
-
let sameKey;
|
185
|
-
if (ruleRegexs.same.test(r)) {
|
186
|
-
let m = r.match(ruleRegexs.same);
|
187
|
-
if (m != null) {
|
188
|
-
rrule = ruleRegexs.same;
|
189
|
-
let m = r.match(ruleRegexs.same);
|
190
|
-
if (m != null) {
|
191
|
-
sameKey = m[1];
|
192
|
-
}
|
193
|
-
message = message || defaultMsgs['same'];
|
194
|
-
}
|
195
|
-
}
|
196
|
-
else if (rule === 'required') {
|
197
|
-
rrule = 'required';
|
198
|
-
message = message || ruleErrMsg || defaultMsgs.required;
|
199
|
-
// eslint-disable-next-line no-prototype-builtins
|
200
|
-
}
|
201
|
-
else if (ruleRegexs.hasOwnProperty(r)) {
|
202
|
-
rrule = ruleRegexs[r];
|
203
|
-
message = message || defaultMsgs[r];
|
204
|
-
}
|
205
|
-
rules.push({ rule: rrule, message: message, sameKey });
|
206
|
-
}
|
207
|
-
return rules;
|
208
|
-
}
|
209
|
-
}
|
210
|
-
export default Validator;
|