node-switchbot 1.1.3-beta.4 → 1.1.3-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/parameter-checker.js +319 -317
- package/lib/switchbot-advertising.js +192 -190
- package/lib/switchbot-device-wocontact.js +3 -2
- package/lib/switchbot-device-wocurtain.js +52 -51
- package/lib/switchbot-device-wohand.js +38 -37
- package/lib/switchbot-device-wohumi.js +46 -60
- package/lib/switchbot-device-wopresence.js +3 -2
- package/lib/switchbot-device-wosensorth.js +3 -2
- package/lib/switchbot-device.js +438 -437
- package/lib/switchbot.js +238 -237
- package/package.json +3 -10
package/lib/parameter-checker.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
class ParameterChecker {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
4
|
+
constructor() {
|
|
5
|
+
this._error = null;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get error() {
|
|
9
|
+
// ----------------------------------
|
|
10
|
+
// Error
|
|
11
|
+
// {
|
|
12
|
+
// code: 'TYPE_INVALID',
|
|
13
|
+
// message: 'The `age` must be an integer.'
|
|
14
|
+
// name: 'age',
|
|
15
|
+
// }
|
|
16
|
+
// ---------------------------------
|
|
17
|
+
return this._error;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
isSpecified(value) {
|
|
21
|
+
return (value === void 0) ? false : true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/* ------------------------------------------------------------------
|
|
23
25
|
* check(obj, rule, required)
|
|
24
26
|
* - Check if the specified object contains valid values
|
|
25
27
|
*
|
|
@@ -52,84 +54,84 @@ class ParameterChecker {
|
|
|
52
54
|
* throw new Error(message);
|
|
53
55
|
* }
|
|
54
56
|
* ---------------------------------------------------------------- */
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
57
|
+
check(obj, rules, required = false) {
|
|
58
|
+
this._error = null;
|
|
59
|
+
if (required) {
|
|
60
|
+
if (!this.isSpecified(obj)) {
|
|
61
|
+
this._error = {
|
|
62
|
+
code: 'MISSING_REQUIRED',
|
|
63
|
+
message: 'The first argument is missing.'
|
|
64
|
+
};
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
if (!obj) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!this.isObject(obj)) {
|
|
74
|
+
this._error = {
|
|
75
|
+
code: 'MISSING_REQUIRED',
|
|
76
|
+
message: 'The first argument is missing.'
|
|
77
|
+
};
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let result = true;
|
|
82
|
+
let name_list = Object.keys(rules);
|
|
83
|
+
|
|
84
|
+
for (let i = 0; i < name_list.length; i++) {
|
|
85
|
+
let name = name_list[i];
|
|
86
|
+
let v = obj[name];
|
|
87
|
+
let rule = rules[name];
|
|
88
|
+
|
|
89
|
+
if (!rule) {
|
|
90
|
+
rule = {};
|
|
91
|
+
}
|
|
92
|
+
if (!this.isSpecified(v)) {
|
|
93
|
+
if (rule.required) {
|
|
94
|
+
result = false;
|
|
95
|
+
this._error = {
|
|
96
|
+
code: 'MISSING_REQUIRED',
|
|
97
|
+
message: 'The `' + name + '` is required.'
|
|
98
|
+
};
|
|
99
|
+
break;
|
|
100
|
+
} else {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (rule.type === 'float') {
|
|
106
|
+
result = this.isFloat(v, rule, name);
|
|
107
|
+
} else if (rule.type === 'integer') {
|
|
108
|
+
result = this.isInteger(v, rule, name);
|
|
109
|
+
} else if (rule.type === 'boolean') {
|
|
110
|
+
result = this.isBoolean(v, rule, name);
|
|
111
|
+
} else if (rule.type === 'array') {
|
|
112
|
+
result = this.isArray(v, rule, name);
|
|
113
|
+
} else if (rule.type === 'object') {
|
|
114
|
+
result = this.isObject(v, rule, name);
|
|
115
|
+
} else if (rule.type === 'string') {
|
|
116
|
+
result = this.isString(v, rule, name);
|
|
117
|
+
} else {
|
|
118
|
+
result = false;
|
|
119
|
+
this._error = {
|
|
120
|
+
code: 'TYPE_UNKNOWN',
|
|
121
|
+
message: 'The rule specified for the `' + name + '` includes an unknown type: ' + rule.type,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (result === false) {
|
|
126
|
+
this._error.name = name;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* ------------------------------------------------------------------
|
|
133
135
|
* isFloat(value, rule, name)
|
|
134
136
|
* - Check if the value is a float
|
|
135
137
|
*
|
|
@@ -150,53 +152,53 @@ class ParameterChecker {
|
|
|
150
152
|
* - If the value is invalid, this method will return `false` and
|
|
151
153
|
* an `Error` object will be set to `this._error`.
|
|
152
154
|
* ---------------------------------------------------------------- */
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
155
|
+
isFloat(value, rule = {}, name = 'value') {
|
|
156
|
+
this._error = null;
|
|
157
|
+
|
|
158
|
+
if (!rule.required && !this.isSpecified(value)) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (typeof (value) !== 'number') {
|
|
163
|
+
this._error = {
|
|
164
|
+
code: 'TYPE_INVALID',
|
|
165
|
+
message: 'The `' + name + '` must be a number (integer or float).'
|
|
166
|
+
};
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (typeof (rule.min) === 'number') {
|
|
171
|
+
if (value < rule.min) {
|
|
172
|
+
this._error = {
|
|
173
|
+
code: 'VALUE_UNDERFLOW',
|
|
174
|
+
message: 'The `' + name + '` must be grater than or equal to ' + rule.min + '.'
|
|
175
|
+
};
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (typeof (rule.max) === 'number') {
|
|
180
|
+
if (value > rule.max) {
|
|
181
|
+
this._error = {
|
|
182
|
+
code: 'VALUE_OVERFLOW',
|
|
183
|
+
message: 'The `' + name + '` must be less than or equal to ' + rule.max + '.'
|
|
184
|
+
};
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (Array.isArray(rule.enum) && rule.enum.length > 0) {
|
|
189
|
+
if (rule.enum.indexOf(value) === -1) {
|
|
190
|
+
this._error = {
|
|
191
|
+
code: 'ENUM_UNMATCH',
|
|
192
|
+
message: 'The `' + name + '` must be any one of ' + JSON.stringify(rule.enum) + '.'
|
|
193
|
+
};
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/* ------------------------------------------------------------------
|
|
200
202
|
* isInteger(value, rule)
|
|
201
203
|
* - Check if the value is an integer
|
|
202
204
|
*
|
|
@@ -217,29 +219,29 @@ class ParameterChecker {
|
|
|
217
219
|
* - If the value is invalid, this method will return `false` and
|
|
218
220
|
* an `Error` object will be set to `this._error`.
|
|
219
221
|
* ---------------------------------------------------------------- */
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
222
|
+
isInteger(value, rule = {}, name = 'value') {
|
|
223
|
+
this._error = null;
|
|
224
|
+
|
|
225
|
+
if (!rule.required && !this.isSpecified(value)) {
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (this.isFloat(value, rule)) {
|
|
230
|
+
if (value % 1 === 0) {
|
|
231
|
+
return true;
|
|
232
|
+
} else {
|
|
233
|
+
this._error = {
|
|
234
|
+
code: 'TYPE_INVALID',
|
|
235
|
+
message: 'The `' + name + '` must be an integer.'
|
|
236
|
+
};
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/* ------------------------------------------------------------------
|
|
243
245
|
* isBoolean(value, rule, name)
|
|
244
246
|
* - Check if the value is a boolean.
|
|
245
247
|
*
|
|
@@ -254,24 +256,24 @@ class ParameterChecker {
|
|
|
254
256
|
* - If the value is invalid, this method will return `false` and
|
|
255
257
|
* an `Error` object will be set to `this._error`.
|
|
256
258
|
* ---------------------------------------------------------------- */
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
259
|
+
isBoolean(value, rule = {}, name = 'value') {
|
|
260
|
+
this._error = null;
|
|
261
|
+
|
|
262
|
+
if (!rule.required && !this.isSpecified(value)) {
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (typeof (value) !== 'boolean') {
|
|
267
|
+
this._error = {
|
|
268
|
+
code: 'TYPE_INVALID',
|
|
269
|
+
message: 'The `' + name + '` must be boolean.'
|
|
270
|
+
};
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/* ------------------------------------------------------------------
|
|
275
277
|
* isObject(value)
|
|
276
278
|
* - Check if the value is an object
|
|
277
279
|
*
|
|
@@ -286,23 +288,23 @@ class ParameterChecker {
|
|
|
286
288
|
* - If the value is invalid, this method will return `false` and
|
|
287
289
|
* an `Error` object will be set to `this._error`.
|
|
288
290
|
* ---------------------------------------------------------------- */
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
291
|
+
isObject(value, rule = {}, name = 'value') {
|
|
292
|
+
this._error = null;
|
|
293
|
+
if (!rule.required && !this.isSpecified(value)) {
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (typeof (value) !== 'object' || value === null || Array.isArray(value)) {
|
|
298
|
+
this._error = {
|
|
299
|
+
code: 'TYPE_INVALID',
|
|
300
|
+
message: 'The `' + name + '` must be an object.'
|
|
301
|
+
};
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/* ------------------------------------------------------------------
|
|
306
308
|
* isArray(value, rule, name)
|
|
307
309
|
* - Check if the value is an `Array` object
|
|
308
310
|
*
|
|
@@ -322,44 +324,44 @@ class ParameterChecker {
|
|
|
322
324
|
* - If the value is invalid, this method will return `false` and
|
|
323
325
|
* an `Error` object will be set to `this._error`.
|
|
324
326
|
* ---------------------------------------------------------------- */
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
327
|
+
isArray(value, rule = {}, name = 'value') {
|
|
328
|
+
this._error = null;
|
|
329
|
+
|
|
330
|
+
if (!rule.required && !this.isSpecified(value)) {
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (!Array.isArray(value)) {
|
|
335
|
+
this._error = {
|
|
336
|
+
code: 'TYPE_INVALID',
|
|
337
|
+
message: 'The value must be an array.'
|
|
338
|
+
};
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (typeof (rule.min) === 'number') {
|
|
343
|
+
if (value.length < rule.min) {
|
|
344
|
+
this._error = {
|
|
345
|
+
code: 'LENGTH_UNDERFLOW',
|
|
346
|
+
message: 'The number of characters in the `' + name + '` must be grater than or equal to ' + rule.min + '.'
|
|
347
|
+
};
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (typeof (rule.max) === 'number') {
|
|
352
|
+
if (value.length > rule.max) {
|
|
353
|
+
this._error = {
|
|
354
|
+
code: 'LENGTH_OVERFLOW',
|
|
355
|
+
message: 'The number of characters in the `' + name + '` must be less than or equal to ' + rule.max + '.'
|
|
356
|
+
};
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/* ------------------------------------------------------------------
|
|
363
365
|
* isString(value, rule, name)
|
|
364
366
|
* - Check if the value is an `Array` object
|
|
365
367
|
*
|
|
@@ -383,80 +385,80 @@ class ParameterChecker {
|
|
|
383
385
|
* - If the value is invalid, this method will return `false` and
|
|
384
386
|
* an `Error` object will be set to `this._error`.
|
|
385
387
|
* ---------------------------------------------------------------- */
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
388
|
+
isString(value, rule = {}, name = 'value') {
|
|
389
|
+
this._error = null;
|
|
390
|
+
|
|
391
|
+
if (!rule.required && !this.isSpecified(value)) {
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (typeof (value) !== 'string') {
|
|
396
|
+
this._error = {
|
|
397
|
+
code: 'TYPE_INVALID',
|
|
398
|
+
message: 'The value must be a string.'
|
|
399
|
+
};
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (typeof (rule.min) === 'number') {
|
|
404
|
+
if (value.length < rule.min) {
|
|
405
|
+
this._error = {
|
|
406
|
+
code: 'LENGTH_UNDERFLOW',
|
|
407
|
+
message: 'The number of characters in the `' + name + '` must be grater than or equal to ' + rule.min + '.'
|
|
408
|
+
};
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (typeof (rule.max) === 'number') {
|
|
413
|
+
if (value.length > rule.max) {
|
|
414
|
+
this._error = {
|
|
415
|
+
code: 'LENGTH_OVERFLOW',
|
|
416
|
+
message: 'The number of characters in the `' + name + '` must be less than or equal to ' + rule.max + '.'
|
|
417
|
+
};
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (typeof (rule.minBytes) === 'number') {
|
|
422
|
+
let blen = Buffer.from(value, 'utf8').length;
|
|
423
|
+
if (blen < rule.minBytes) {
|
|
424
|
+
this._error = {
|
|
425
|
+
code: 'LENGTH_UNDERFLOW',
|
|
426
|
+
message: 'The byte length of the `' + name + '` (' + blen + ' bytes) must be grater than or equal to ' + rule.minBytes + ' bytes.'
|
|
427
|
+
};
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (typeof (rule.maxBytes) === 'number') {
|
|
432
|
+
let blen = Buffer.from(value, 'utf8').length;
|
|
433
|
+
if (blen > rule.maxBytes) {
|
|
434
|
+
this._error = {
|
|
435
|
+
code: 'LENGTH_OVERFLOW',
|
|
436
|
+
message: 'The byte length of the `' + name + '` (' + blen + ' bytes) must be less than or equal to ' + rule.maxBytes + ' bytes.'
|
|
437
|
+
};
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (rule.pattern instanceof RegExp) {
|
|
442
|
+
if (!rule.pattern.test(v)) {
|
|
443
|
+
this._error = {
|
|
444
|
+
code: 'PATTERN_UNMATCH',
|
|
445
|
+
message: 'The `' + name + '` does not conform with the pattern.'
|
|
446
|
+
};
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (Array.isArray(rule.enum) && rule.enum.length > 0) {
|
|
451
|
+
if (rule.enum.indexOf(value) === -1) {
|
|
452
|
+
this._error = {
|
|
453
|
+
code: 'ENUM_UNMATCH',
|
|
454
|
+
message: 'The `' + name + '` must be any one of ' + JSON.stringify(rule.enum) + '.'
|
|
455
|
+
};
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
460
462
|
}
|
|
461
463
|
|
|
462
|
-
|
|
464
|
+
module.exports = new ParameterChecker();
|