ph-utils 0.2.13 → 0.2.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
  }
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
  */
@@ -79,10 +80,15 @@ class Validator {
79
80
  types[schema.key] = schema.type;
80
81
  }
81
82
  else {
82
- types[schema.key] = validTypes.has(schema.type || '') ? schema.type : 'string';
83
+ types[schema.key] = validTypes.has(schema.type || '')
84
+ ? schema.type
85
+ : 'string';
83
86
  }
84
87
  // 解析规则
85
88
  let rules = [];
89
+ if (schema.required === true) {
90
+ rules.push(this._parseStringRule('required'));
91
+ }
86
92
  let rule = schema.rules;
87
93
  if (typeof rule === 'string') {
88
94
  rules = rules.concat(this._parseStringRule(rule));
@@ -92,7 +98,8 @@ class Validator {
92
98
  if (typeof ruleItem === 'string') {
93
99
  rules = rules.concat(this._parseStringRule(ruleItem));
94
100
  }
95
- else if (ruleItem instanceof RegExp || typeof ruleItem === 'function') {
101
+ else if (ruleItem instanceof RegExp ||
102
+ typeof ruleItem === 'function') {
96
103
  rules.push({ rule: ruleItem, message: defaultMsg });
97
104
  }
98
105
  else {
@@ -100,7 +107,10 @@ class Validator {
100
107
  rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
101
108
  }
102
109
  else {
103
- rules.push({ rule: ruleItem.rule, message: ruleItem.message || defaultMsg });
110
+ rules.push({
111
+ rule: ruleItem.rule,
112
+ message: ruleItem.message || defaultMsg,
113
+ });
104
114
  }
105
115
  }
106
116
  }
@@ -173,21 +183,34 @@ class Validator {
173
183
  _validateRule(rules, value, data) {
174
184
  let errMsg = '';
175
185
  for (let rule of rules) {
176
- if (typeof rule.rule === 'function') {
177
- if (!rule.rule(value)) {
186
+ // 如果数据为空,则判断是否是必填
187
+ if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
188
+ if (rule.rule === 'required') {
178
189
  errMsg = rule.message;
179
190
  }
180
191
  }
181
- else if (rule.sameKey != null) {
182
- if (data != null) {
183
- if (!ruleFns.same(value, data[rule.sameKey])) {
192
+ else {
193
+ if (typeof rule.rule === 'function') {
194
+ if (!rule.rule(value)) {
184
195
  errMsg = rule.message;
185
196
  }
186
197
  }
187
- }
188
- else {
189
- if (!ruleFns.pattern(rule.rule, value)) {
190
- errMsg = rule.message;
198
+ else if (rule.sameKey != null) {
199
+ if (data != null) {
200
+ if (!ruleFns.same(value, data[rule.sameKey])) {
201
+ errMsg = rule.message;
202
+ }
203
+ }
204
+ }
205
+ else if (rule.rule === 'required') {
206
+ if (!ruleFns.pattern(ruleRegexs.required, value)) {
207
+ errMsg = rule.message;
208
+ }
209
+ }
210
+ else {
211
+ if (!ruleFns.pattern(rule.rule, value)) {
212
+ errMsg = rule.message;
213
+ }
191
214
  }
192
215
  }
193
216
  if (errMsg !== '') {
@@ -211,7 +234,11 @@ class Validator {
211
234
  message = defaultMsgs['same'];
212
235
  }
213
236
  }
214
- else if (ruleRegexs.hasOwnProperty(r)) {
237
+ else if (rule === 'required') {
238
+ rrule = 'required';
239
+ message = defaultMsgs.required;
240
+ }
241
+ else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
215
242
  rrule = ruleRegexs[r];
216
243
  message = defaultMsgs[r] || defaultMsg;
217
244
  }
@@ -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
  }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
  /**
2
3
  * 数据验证器
3
4
  */
@@ -74,10 +75,15 @@ class Validator {
74
75
  types[schema.key] = schema.type;
75
76
  }
76
77
  else {
77
- types[schema.key] = validTypes.has(schema.type || '') ? schema.type : 'string';
78
+ types[schema.key] = validTypes.has(schema.type || '')
79
+ ? schema.type
80
+ : 'string';
78
81
  }
79
82
  // 解析规则
80
83
  let rules = [];
84
+ if (schema.required === true) {
85
+ rules.push(this._parseStringRule('required'));
86
+ }
81
87
  let rule = schema.rules;
82
88
  if (typeof rule === 'string') {
83
89
  rules = rules.concat(this._parseStringRule(rule));
@@ -87,7 +93,8 @@ class Validator {
87
93
  if (typeof ruleItem === 'string') {
88
94
  rules = rules.concat(this._parseStringRule(ruleItem));
89
95
  }
90
- else if (ruleItem instanceof RegExp || typeof ruleItem === 'function') {
96
+ else if (ruleItem instanceof RegExp ||
97
+ typeof ruleItem === 'function') {
91
98
  rules.push({ rule: ruleItem, message: defaultMsg });
92
99
  }
93
100
  else {
@@ -95,7 +102,10 @@ class Validator {
95
102
  rules = rules.concat(this._parseStringRule(ruleItem.rule, ruleItem.message));
96
103
  }
97
104
  else {
98
- rules.push({ rule: ruleItem.rule, message: ruleItem.message || defaultMsg });
105
+ rules.push({
106
+ rule: ruleItem.rule,
107
+ message: ruleItem.message || defaultMsg,
108
+ });
99
109
  }
100
110
  }
101
111
  }
@@ -168,21 +178,34 @@ class Validator {
168
178
  _validateRule(rules, value, data) {
169
179
  let errMsg = '';
170
180
  for (let rule of rules) {
171
- if (typeof rule.rule === 'function') {
172
- if (!rule.rule(value)) {
181
+ // 如果数据为空,则判断是否是必填
182
+ if (value == null || !ruleFns.pattern(ruleRegexs.required, value)) {
183
+ if (rule.rule === 'required') {
173
184
  errMsg = rule.message;
174
185
  }
175
186
  }
176
- else if (rule.sameKey != null) {
177
- if (data != null) {
178
- if (!ruleFns.same(value, data[rule.sameKey])) {
187
+ else {
188
+ if (typeof rule.rule === 'function') {
189
+ if (!rule.rule(value)) {
179
190
  errMsg = rule.message;
180
191
  }
181
192
  }
182
- }
183
- else {
184
- if (!ruleFns.pattern(rule.rule, value)) {
185
- errMsg = rule.message;
193
+ else if (rule.sameKey != null) {
194
+ if (data != null) {
195
+ if (!ruleFns.same(value, data[rule.sameKey])) {
196
+ errMsg = rule.message;
197
+ }
198
+ }
199
+ }
200
+ else if (rule.rule === 'required') {
201
+ if (!ruleFns.pattern(ruleRegexs.required, value)) {
202
+ errMsg = rule.message;
203
+ }
204
+ }
205
+ else {
206
+ if (!ruleFns.pattern(rule.rule, value)) {
207
+ errMsg = rule.message;
208
+ }
186
209
  }
187
210
  }
188
211
  if (errMsg !== '') {
@@ -206,7 +229,11 @@ class Validator {
206
229
  message = defaultMsgs['same'];
207
230
  }
208
231
  }
209
- else if (ruleRegexs.hasOwnProperty(r)) {
232
+ else if (rule === 'required') {
233
+ rrule = 'required';
234
+ message = defaultMsgs.required;
235
+ }
236
+ else if (Object.prototype.hasOwnProperty.call(ruleRegexs, r)) {
210
237
  rrule = ruleRegexs[r];
211
238
  message = defaultMsgs[r] || defaultMsg;
212
239
  }
@@ -216,5 +243,5 @@ class Validator {
216
243
  return rules;
217
244
  }
218
245
  }
219
- // @ts-ignore
246
+ // @ts-ignore: Unreachable code error
220
247
  export default Validator;
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.13",
8
+ "version": "0.2.14",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https//gitee.com/towardly/ph.git",