ph-utils 0.2.14 → 0.2.18

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