ph-utils 0.2.14 → 0.2.16

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): 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,24 +47,13 @@ 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
59
  if (schema.required === true) {
@@ -120,7 +90,6 @@ class Validator {
120
90
  }
121
91
  parsedRules[schema.key] = rules;
122
92
  }
123
- this.types = types;
124
93
  this.rules = parsedRules;
125
94
  }
126
95
  /**
@@ -132,14 +101,10 @@ class Validator {
132
101
  return new Promise((resolve, reject) => {
133
102
  let errMsg = '';
134
103
  let errKey = '';
135
- let resData = {};
136
104
  for (let key in this.rules) {
137
105
  if ({}.hasOwnProperty.call(this.rules, key)) {
138
106
  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 {
107
+ if (errMsg !== '') {
143
108
  errKey = key;
144
109
  errMsg = errMsg.replace('%s', key);
145
110
  break;
@@ -147,7 +112,7 @@ class Validator {
147
112
  }
148
113
  }
149
114
  if (errMsg === '') {
150
- resolve(resData);
115
+ resolve(true);
151
116
  }
152
117
  else {
153
118
  reject(new ValidateError(errKey, errMsg));
@@ -163,23 +128,15 @@ class Validator {
163
128
  return new Promise((resolve, reject) => {
164
129
  let keyRules = this.rules[key];
165
130
  let errMsg = this._validateRule(keyRules, value, null);
166
- if (errMsg === '') {
167
- resolve(this._conversionType(this.types[key], value));
168
- }
169
- else {
131
+ if (errMsg !== '') {
170
132
  errMsg = errMsg.replace('%s', key);
171
133
  reject(new ValidateError(key, errMsg));
172
134
  }
135
+ else {
136
+ resolve(true);
137
+ }
173
138
  });
174
139
  }
175
- _conversionType(type, v) {
176
- if (typeof type === 'function') {
177
- return type(v);
178
- }
179
- else {
180
- return typeFns[type](v);
181
- }
182
- }
183
140
  _validateRule(rules, value, data) {
184
141
  let errMsg = '';
185
142
  for (let rule of rules) {
@@ -203,12 +160,12 @@ class Validator {
203
160
  }
204
161
  }
205
162
  else if (rule.rule === 'required') {
206
- if (!ruleFns.pattern(ruleRegexs.required, value)) {
163
+ if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
207
164
  errMsg = rule.message;
208
165
  }
209
166
  }
210
167
  else {
211
- if (!ruleFns.pattern(rule.rule, value)) {
168
+ if (!ruleFns.pattern(rule.rule, String(value))) {
212
169
  errMsg = rule.message;
213
170
  }
214
171
  }
@@ -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): 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,17 +49,7 @@ 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
55
  if (schema.required === true) {
@@ -115,7 +86,6 @@ class Validator {
115
86
  }
116
87
  parsedRules[schema.key] = rules;
117
88
  }
118
- this.types = types;
119
89
  this.rules = parsedRules;
120
90
  }
121
91
  /**
@@ -127,14 +97,10 @@ class Validator {
127
97
  return new Promise((resolve, reject) => {
128
98
  let errMsg = '';
129
99
  let errKey = '';
130
- let resData = {};
131
100
  for (let key in this.rules) {
132
101
  if ({}.hasOwnProperty.call(this.rules, key)) {
133
102
  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 {
103
+ if (errMsg !== '') {
138
104
  errKey = key;
139
105
  errMsg = errMsg.replace('%s', key);
140
106
  break;
@@ -142,7 +108,7 @@ class Validator {
142
108
  }
143
109
  }
144
110
  if (errMsg === '') {
145
- resolve(resData);
111
+ resolve(true);
146
112
  }
147
113
  else {
148
114
  reject(new ValidateError(errKey, errMsg));
@@ -158,23 +124,15 @@ class Validator {
158
124
  return new Promise((resolve, reject) => {
159
125
  let keyRules = this.rules[key];
160
126
  let errMsg = this._validateRule(keyRules, value, null);
161
- if (errMsg === '') {
162
- resolve(this._conversionType(this.types[key], value));
163
- }
164
- else {
127
+ if (errMsg !== '') {
165
128
  errMsg = errMsg.replace('%s', key);
166
129
  reject(new ValidateError(key, errMsg));
167
130
  }
131
+ else {
132
+ resolve(true);
133
+ }
168
134
  });
169
135
  }
170
- _conversionType(type, v) {
171
- if (typeof type === 'function') {
172
- return type(v);
173
- }
174
- else {
175
- return typeFns[type](v);
176
- }
177
- }
178
136
  _validateRule(rules, value, data) {
179
137
  let errMsg = '';
180
138
  for (let rule of rules) {
@@ -198,12 +156,12 @@ class Validator {
198
156
  }
199
157
  }
200
158
  else if (rule.rule === 'required') {
201
- if (!ruleFns.pattern(ruleRegexs.required, value)) {
159
+ if (!ruleFns.pattern(ruleRegexs.required, String(value))) {
202
160
  errMsg = rule.message;
203
161
  }
204
162
  }
205
163
  else {
206
- if (!ruleFns.pattern(rule.rule, value)) {
164
+ if (!ruleFns.pattern(rule.rule, String(value))) {
207
165
  errMsg = rule.message;
208
166
  }
209
167
  }
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.16",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",