ph-utils 0.2.13 → 0.2.17
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/validator.d.ts +4 -7
- package/lib/validator.js +46 -61
- package/lib/validator_m.d.ts +4 -7
- package/lib/validator_m.js +47 -61
- package/package.json +1 -1
package/lib/validator.d.ts
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
* 数据验证器
|
3
3
|
*/
|
4
4
|
interface RuleItem {
|
5
|
-
rule: RegExp | ((v: any) => boolean);
|
5
|
+
rule: RegExp | ((v: any) => boolean) | 'required';
|
6
6
|
message: string;
|
7
7
|
sameKey?: string;
|
8
8
|
}
|
@@ -12,6 +12,7 @@ export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp
|
|
12
12
|
});
|
13
13
|
export interface SchemaType {
|
14
14
|
key: string;
|
15
|
+
required?: boolean;
|
15
16
|
type?: string | ((v: any) => void);
|
16
17
|
rules: RuleType[];
|
17
18
|
}
|
@@ -22,9 +23,6 @@ declare class Validator {
|
|
22
23
|
rules: {
|
23
24
|
[index: string]: RuleItem[];
|
24
25
|
};
|
25
|
-
types: {
|
26
|
-
[index: string]: string | ((v: any) => void);
|
27
|
-
};
|
28
26
|
/**
|
29
27
|
* 构造数据验证转换器
|
30
28
|
* @param schemas 配置验证转换规则
|
@@ -35,14 +33,13 @@ declare class Validator {
|
|
35
33
|
* @param data 待验证的数据
|
36
34
|
* @returns
|
37
35
|
*/
|
38
|
-
validate
|
36
|
+
validate(data: any): Promise<boolean>;
|
39
37
|
/**
|
40
38
|
* 只验证指定 key 的数据格式
|
41
39
|
* @param key 指定待验证的 key
|
42
40
|
* @param value 待验证的数据
|
43
41
|
*/
|
44
|
-
validateKey(key: string, value: any): Promise<
|
45
|
-
private _conversionType;
|
42
|
+
validateKey(key: string, value: any): Promise<boolean>;
|
46
43
|
private _validateRule;
|
47
44
|
private _parseStringRule;
|
48
45
|
}
|
package/lib/validator.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
"use strict";
|
2
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2
3
|
/**
|
3
4
|
* 数据验证器
|
4
5
|
*/
|
@@ -9,8 +10,6 @@ const defaultMsgs = {
|
|
9
10
|
required: '%s为必填字段',
|
10
11
|
};
|
11
12
|
const defaultMsg = '请输入正确的数据';
|
12
|
-
// 允许的内置 type
|
13
|
-
const validTypes = new Set(['string', 'boolean', 'number', 'float', 'int']);
|
14
13
|
// 一些常用的验证正则
|
15
14
|
const ruleRegexs = {
|
16
15
|
/** 验证跟其余数据相等的正则,一般用于验证再次输入密码 */
|
@@ -34,23 +33,6 @@ const ruleFns = {
|
|
34
33
|
return regex.test(String(val));
|
35
34
|
},
|
36
35
|
};
|
37
|
-
const typeFns = {
|
38
|
-
string(v) {
|
39
|
-
return String(v);
|
40
|
-
},
|
41
|
-
boolean(v) {
|
42
|
-
return Boolean(v);
|
43
|
-
},
|
44
|
-
number(v) {
|
45
|
-
return Number(v);
|
46
|
-
},
|
47
|
-
int(v) {
|
48
|
-
return parseInt(v, 10);
|
49
|
-
},
|
50
|
-
floag(v) {
|
51
|
-
return parseFloat(v);
|
52
|
-
},
|
53
|
-
};
|
54
36
|
class ValidateError extends Error {
|
55
37
|
name;
|
56
38
|
key;
|
@@ -65,22 +47,13 @@ class ValidateError extends Error {
|
|
65
47
|
*/
|
66
48
|
class Validator {
|
67
49
|
rules;
|
68
|
-
types;
|
69
50
|
/**
|
70
51
|
* 构造数据验证转换器
|
71
52
|
* @param schemas 配置验证转换规则
|
72
53
|
*/
|
73
54
|
constructor(schemas) {
|
74
55
|
let parsedRules = {};
|
75
|
-
let types = {};
|
76
56
|
for (let schema of schemas) {
|
77
|
-
// 解析 types 用于进行数据类型转换
|
78
|
-
if (typeof schema.type === 'function') {
|
79
|
-
types[schema.key] = schema.type;
|
80
|
-
}
|
81
|
-
else {
|
82
|
-
types[schema.key] = validTypes.has(schema.type || '') ? schema.type : 'string';
|
83
|
-
}
|
84
57
|
// 解析规则
|
85
58
|
let rules = [];
|
86
59
|
let rule = schema.rules;
|
@@ -90,17 +63,21 @@ class Validator {
|
|
90
63
|
else if (rule instanceof Array) {
|
91
64
|
for (let ruleItem of rule) {
|
92
65
|
if (typeof ruleItem === 'string') {
|
93
|
-
rules
|
66
|
+
rules.push(...this._parseStringRule(ruleItem));
|
94
67
|
}
|
95
|
-
else if (ruleItem instanceof RegExp ||
|
68
|
+
else if (ruleItem instanceof RegExp ||
|
69
|
+
typeof ruleItem === 'function') {
|
96
70
|
rules.push({ rule: ruleItem, message: defaultMsg });
|
97
71
|
}
|
98
72
|
else {
|
99
73
|
if (typeof ruleItem.rule === 'string') {
|
100
|
-
rules
|
74
|
+
rules.push(...this._parseStringRule(ruleItem.rule, ruleItem.message));
|
101
75
|
}
|
102
76
|
else {
|
103
|
-
rules.push({
|
77
|
+
rules.push({
|
78
|
+
rule: ruleItem.rule,
|
79
|
+
message: ruleItem.message || defaultMsg,
|
80
|
+
});
|
104
81
|
}
|
105
82
|
}
|
106
83
|
}
|
@@ -108,9 +85,12 @@ class Validator {
|
|
108
85
|
else {
|
109
86
|
rules.push({ rule, message: defaultMsg });
|
110
87
|
}
|
88
|
+
if (schema.required === true &&
|
89
|
+
rules.findIndex((r) => r.rule === 'required') === -1) {
|
90
|
+
rules.push(this._parseStringRule('required'));
|
91
|
+
}
|
111
92
|
parsedRules[schema.key] = rules;
|
112
93
|
}
|
113
|
-
this.types = types;
|
114
94
|
this.rules = parsedRules;
|
115
95
|
}
|
116
96
|
/**
|
@@ -122,14 +102,10 @@ class Validator {
|
|
122
102
|
return new Promise((resolve, reject) => {
|
123
103
|
let errMsg = '';
|
124
104
|
let errKey = '';
|
125
|
-
let resData = {};
|
126
105
|
for (let key in this.rules) {
|
127
106
|
if ({}.hasOwnProperty.call(this.rules, key)) {
|
128
107
|
errMsg = this._validateRule(this.rules[key], data[key], data);
|
129
|
-
if (errMsg
|
130
|
-
resData[key] = this._conversionType(this.types[key], data[key]);
|
131
|
-
}
|
132
|
-
else {
|
108
|
+
if (errMsg !== '') {
|
133
109
|
errKey = key;
|
134
110
|
errMsg = errMsg.replace('%s', key);
|
135
111
|
break;
|
@@ -137,7 +113,7 @@ class Validator {
|
|
137
113
|
}
|
138
114
|
}
|
139
115
|
if (errMsg === '') {
|
140
|
-
resolve(
|
116
|
+
resolve(true);
|
141
117
|
}
|
142
118
|
else {
|
143
119
|
reject(new ValidateError(errKey, errMsg));
|
@@ -153,41 +129,46 @@ class Validator {
|
|
153
129
|
return new Promise((resolve, reject) => {
|
154
130
|
let keyRules = this.rules[key];
|
155
131
|
let errMsg = this._validateRule(keyRules, value, null);
|
156
|
-
if (errMsg
|
157
|
-
resolve(this._conversionType(this.types[key], value));
|
158
|
-
}
|
159
|
-
else {
|
132
|
+
if (errMsg !== '') {
|
160
133
|
errMsg = errMsg.replace('%s', key);
|
161
134
|
reject(new ValidateError(key, errMsg));
|
162
135
|
}
|
136
|
+
else {
|
137
|
+
resolve(true);
|
138
|
+
}
|
163
139
|
});
|
164
140
|
}
|
165
|
-
_conversionType(type, v) {
|
166
|
-
if (typeof type === 'function') {
|
167
|
-
return type(v);
|
168
|
-
}
|
169
|
-
else {
|
170
|
-
return typeFns[type](v);
|
171
|
-
}
|
172
|
-
}
|
173
141
|
_validateRule(rules, value, data) {
|
174
142
|
let errMsg = '';
|
175
143
|
for (let rule of rules) {
|
176
|
-
|
177
|
-
|
144
|
+
// 如果数据为空,则判断是否是必填
|
145
|
+
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
146
|
+
if (rule.rule === 'required') {
|
178
147
|
errMsg = rule.message;
|
179
148
|
}
|
180
149
|
}
|
181
|
-
else
|
182
|
-
if (
|
183
|
-
if (!
|
150
|
+
else {
|
151
|
+
if (typeof rule.rule === 'function') {
|
152
|
+
if (!rule.rule(value)) {
|
184
153
|
errMsg = rule.message;
|
185
154
|
}
|
186
155
|
}
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
156
|
+
else if (rule.sameKey != null) {
|
157
|
+
if (data != null) {
|
158
|
+
if (!ruleFns.same(value, data[rule.sameKey])) {
|
159
|
+
errMsg = rule.message;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
else if (rule.rule === 'required') {
|
164
|
+
if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
|
165
|
+
errMsg = rule.message;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
else {
|
169
|
+
if (!ruleFns.pattern(rule.rule, String(value))) {
|
170
|
+
errMsg = rule.message;
|
171
|
+
}
|
191
172
|
}
|
192
173
|
}
|
193
174
|
if (errMsg !== '') {
|
@@ -211,7 +192,11 @@ class Validator {
|
|
211
192
|
message = defaultMsgs['same'];
|
212
193
|
}
|
213
194
|
}
|
214
|
-
else if (
|
195
|
+
else if (rule === 'required') {
|
196
|
+
rrule = 'required';
|
197
|
+
message = ruleErrMsg || defaultMsgs.required;
|
198
|
+
}
|
199
|
+
else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
|
215
200
|
rrule = ruleRegexs[r];
|
216
201
|
message = defaultMsgs[r] || defaultMsg;
|
217
202
|
}
|
package/lib/validator_m.d.ts
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
* 数据验证器
|
3
3
|
*/
|
4
4
|
interface RuleItem {
|
5
|
-
rule: RegExp | ((v: any) => boolean);
|
5
|
+
rule: RegExp | ((v: any) => boolean) | 'required';
|
6
6
|
message: string;
|
7
7
|
sameKey?: string;
|
8
8
|
}
|
@@ -12,6 +12,7 @@ export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp
|
|
12
12
|
});
|
13
13
|
export interface SchemaType {
|
14
14
|
key: string;
|
15
|
+
required?: boolean;
|
15
16
|
type?: string | ((v: any) => void);
|
16
17
|
rules: RuleType[];
|
17
18
|
}
|
@@ -22,9 +23,6 @@ declare class Validator {
|
|
22
23
|
rules: {
|
23
24
|
[index: string]: RuleItem[];
|
24
25
|
};
|
25
|
-
types: {
|
26
|
-
[index: string]: string | ((v: any) => void);
|
27
|
-
};
|
28
26
|
/**
|
29
27
|
* 构造数据验证转换器
|
30
28
|
* @param schemas 配置验证转换规则
|
@@ -35,14 +33,13 @@ declare class Validator {
|
|
35
33
|
* @param data 待验证的数据
|
36
34
|
* @returns
|
37
35
|
*/
|
38
|
-
validate
|
36
|
+
validate(data: any): Promise<boolean>;
|
39
37
|
/**
|
40
38
|
* 只验证指定 key 的数据格式
|
41
39
|
* @param key 指定待验证的 key
|
42
40
|
* @param value 待验证的数据
|
43
41
|
*/
|
44
|
-
validateKey(key: string, value: any): Promise<
|
45
|
-
private _conversionType;
|
42
|
+
validateKey(key: string, value: any): Promise<boolean>;
|
46
43
|
private _validateRule;
|
47
44
|
private _parseStringRule;
|
48
45
|
}
|
package/lib/validator_m.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
1
2
|
/**
|
2
3
|
* 数据验证器
|
3
4
|
*/
|
@@ -8,8 +9,6 @@ const defaultMsgs = {
|
|
8
9
|
required: '%s为必填字段',
|
9
10
|
};
|
10
11
|
const defaultMsg = '请输入正确的数据';
|
11
|
-
// 允许的内置 type
|
12
|
-
const validTypes = new Set(['string', 'boolean', 'number', 'float', 'int']);
|
13
12
|
// 一些常用的验证正则
|
14
13
|
const ruleRegexs = {
|
15
14
|
/** 验证跟其余数据相等的正则,一般用于验证再次输入密码 */
|
@@ -33,23 +32,6 @@ const ruleFns = {
|
|
33
32
|
return regex.test(String(val));
|
34
33
|
},
|
35
34
|
};
|
36
|
-
const typeFns = {
|
37
|
-
string(v) {
|
38
|
-
return String(v);
|
39
|
-
},
|
40
|
-
boolean(v) {
|
41
|
-
return Boolean(v);
|
42
|
-
},
|
43
|
-
number(v) {
|
44
|
-
return Number(v);
|
45
|
-
},
|
46
|
-
int(v) {
|
47
|
-
return parseInt(v, 10);
|
48
|
-
},
|
49
|
-
floag(v) {
|
50
|
-
return parseFloat(v);
|
51
|
-
},
|
52
|
-
};
|
53
35
|
class ValidateError extends Error {
|
54
36
|
constructor(key, msg) {
|
55
37
|
super(msg);
|
@@ -67,15 +49,7 @@ class Validator {
|
|
67
49
|
*/
|
68
50
|
constructor(schemas) {
|
69
51
|
let parsedRules = {};
|
70
|
-
let types = {};
|
71
52
|
for (let schema of schemas) {
|
72
|
-
// 解析 types 用于进行数据类型转换
|
73
|
-
if (typeof schema.type === 'function') {
|
74
|
-
types[schema.key] = schema.type;
|
75
|
-
}
|
76
|
-
else {
|
77
|
-
types[schema.key] = validTypes.has(schema.type || '') ? schema.type : 'string';
|
78
|
-
}
|
79
53
|
// 解析规则
|
80
54
|
let rules = [];
|
81
55
|
let rule = schema.rules;
|
@@ -85,17 +59,21 @@ class Validator {
|
|
85
59
|
else if (rule instanceof Array) {
|
86
60
|
for (let ruleItem of rule) {
|
87
61
|
if (typeof ruleItem === 'string') {
|
88
|
-
rules
|
62
|
+
rules.push(...this._parseStringRule(ruleItem));
|
89
63
|
}
|
90
|
-
else if (ruleItem instanceof RegExp ||
|
64
|
+
else if (ruleItem instanceof RegExp ||
|
65
|
+
typeof ruleItem === 'function') {
|
91
66
|
rules.push({ rule: ruleItem, message: defaultMsg });
|
92
67
|
}
|
93
68
|
else {
|
94
69
|
if (typeof ruleItem.rule === 'string') {
|
95
|
-
rules
|
70
|
+
rules.push(...this._parseStringRule(ruleItem.rule, ruleItem.message));
|
96
71
|
}
|
97
72
|
else {
|
98
|
-
rules.push({
|
73
|
+
rules.push({
|
74
|
+
rule: ruleItem.rule,
|
75
|
+
message: ruleItem.message || defaultMsg,
|
76
|
+
});
|
99
77
|
}
|
100
78
|
}
|
101
79
|
}
|
@@ -103,9 +81,12 @@ class Validator {
|
|
103
81
|
else {
|
104
82
|
rules.push({ rule, message: defaultMsg });
|
105
83
|
}
|
84
|
+
if (schema.required === true &&
|
85
|
+
rules.findIndex((r) => r.rule === 'required') === -1) {
|
86
|
+
rules.push(this._parseStringRule('required'));
|
87
|
+
}
|
106
88
|
parsedRules[schema.key] = rules;
|
107
89
|
}
|
108
|
-
this.types = types;
|
109
90
|
this.rules = parsedRules;
|
110
91
|
}
|
111
92
|
/**
|
@@ -117,14 +98,10 @@ class Validator {
|
|
117
98
|
return new Promise((resolve, reject) => {
|
118
99
|
let errMsg = '';
|
119
100
|
let errKey = '';
|
120
|
-
let resData = {};
|
121
101
|
for (let key in this.rules) {
|
122
102
|
if ({}.hasOwnProperty.call(this.rules, key)) {
|
123
103
|
errMsg = this._validateRule(this.rules[key], data[key], data);
|
124
|
-
if (errMsg
|
125
|
-
resData[key] = this._conversionType(this.types[key], data[key]);
|
126
|
-
}
|
127
|
-
else {
|
104
|
+
if (errMsg !== '') {
|
128
105
|
errKey = key;
|
129
106
|
errMsg = errMsg.replace('%s', key);
|
130
107
|
break;
|
@@ -132,7 +109,7 @@ class Validator {
|
|
132
109
|
}
|
133
110
|
}
|
134
111
|
if (errMsg === '') {
|
135
|
-
resolve(
|
112
|
+
resolve(true);
|
136
113
|
}
|
137
114
|
else {
|
138
115
|
reject(new ValidateError(errKey, errMsg));
|
@@ -148,41 +125,46 @@ class Validator {
|
|
148
125
|
return new Promise((resolve, reject) => {
|
149
126
|
let keyRules = this.rules[key];
|
150
127
|
let errMsg = this._validateRule(keyRules, value, null);
|
151
|
-
if (errMsg
|
152
|
-
resolve(this._conversionType(this.types[key], value));
|
153
|
-
}
|
154
|
-
else {
|
128
|
+
if (errMsg !== '') {
|
155
129
|
errMsg = errMsg.replace('%s', key);
|
156
130
|
reject(new ValidateError(key, errMsg));
|
157
131
|
}
|
132
|
+
else {
|
133
|
+
resolve(true);
|
134
|
+
}
|
158
135
|
});
|
159
136
|
}
|
160
|
-
_conversionType(type, v) {
|
161
|
-
if (typeof type === 'function') {
|
162
|
-
return type(v);
|
163
|
-
}
|
164
|
-
else {
|
165
|
-
return typeFns[type](v);
|
166
|
-
}
|
167
|
-
}
|
168
137
|
_validateRule(rules, value, data) {
|
169
138
|
let errMsg = '';
|
170
139
|
for (let rule of rules) {
|
171
|
-
|
172
|
-
|
140
|
+
// 如果数据为空,则判断是否是必填
|
141
|
+
if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
|
142
|
+
if (rule.rule === 'required') {
|
173
143
|
errMsg = rule.message;
|
174
144
|
}
|
175
145
|
}
|
176
|
-
else
|
177
|
-
if (
|
178
|
-
if (!
|
146
|
+
else {
|
147
|
+
if (typeof rule.rule === 'function') {
|
148
|
+
if (!rule.rule(value)) {
|
179
149
|
errMsg = rule.message;
|
180
150
|
}
|
181
151
|
}
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
152
|
+
else if (rule.sameKey != null) {
|
153
|
+
if (data != null) {
|
154
|
+
if (!ruleFns.same(value, data[rule.sameKey])) {
|
155
|
+
errMsg = rule.message;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}
|
159
|
+
else if (rule.rule === 'required') {
|
160
|
+
if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
|
161
|
+
errMsg = rule.message;
|
162
|
+
}
|
163
|
+
}
|
164
|
+
else {
|
165
|
+
if (!ruleFns.pattern(rule.rule, String(value))) {
|
166
|
+
errMsg = rule.message;
|
167
|
+
}
|
186
168
|
}
|
187
169
|
}
|
188
170
|
if (errMsg !== '') {
|
@@ -206,7 +188,11 @@ class Validator {
|
|
206
188
|
message = defaultMsgs['same'];
|
207
189
|
}
|
208
190
|
}
|
209
|
-
else if (
|
191
|
+
else if (rule === 'required') {
|
192
|
+
rrule = 'required';
|
193
|
+
message = ruleErrMsg || defaultMsgs.required;
|
194
|
+
}
|
195
|
+
else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
|
210
196
|
rrule = ruleRegexs[r];
|
211
197
|
message = defaultMsgs[r] || defaultMsg;
|
212
198
|
}
|
@@ -216,5 +202,5 @@ class Validator {
|
|
216
202
|
return rules;
|
217
203
|
}
|
218
204
|
}
|
219
|
-
// @ts-ignore
|
205
|
+
// @ts-ignore: Unreachable code error
|
220
206
|
export default Validator;
|