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.
@@ -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;