node-switchbot 1.3.0 → 1.4.0
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/CHANGELOG.md +32 -23
- package/LICENSE +1 -1
- package/README.md +322 -258
- package/lib/parameter-checker.js +271 -213
- package/lib/switchbot-advertising.js +238 -163
- package/lib/switchbot-device-wocontact.js +4 -7
- package/lib/switchbot-device-wocurtain.js +106 -91
- package/lib/switchbot-device-wohand.js +69 -65
- package/lib/switchbot-device-wohumi.js +69 -65
- package/lib/switchbot-device-woplugmini.js +81 -0
- package/lib/switchbot-device-wopresence.js +4 -7
- package/lib/switchbot-device-wosensorth.js +4 -7
- package/lib/switchbot-device.js +188 -149
- package/lib/switchbot.js +271 -233
- package/package.json +3 -3
package/lib/parameter-checker.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
class ParameterChecker {
|
|
4
4
|
constructor() {
|
|
@@ -12,55 +12,55 @@ class ParameterChecker {
|
|
|
12
12
|
// code: 'TYPE_INVALID',
|
|
13
13
|
// message: 'The `age` must be an integer.'
|
|
14
14
|
// name: 'age',
|
|
15
|
-
// }
|
|
15
|
+
// }
|
|
16
16
|
// ---------------------------------
|
|
17
17
|
return this._error;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
isSpecified(value) {
|
|
21
|
-
return
|
|
21
|
+
return value === void 0 ? false : true;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/* ------------------------------------------------------------------
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
25
|
+
* check(obj, rule, required)
|
|
26
|
+
* - Check if the specified object contains valid values
|
|
27
|
+
*
|
|
28
|
+
* [Arguments]
|
|
29
|
+
* - obj | Object | Required | Object including parameters you want to check
|
|
30
|
+
* - rules | Object | Required | Object including rules for the parameters
|
|
31
|
+
* - required | Boolean | Optional | Flag whther the `obj` is required or not.
|
|
32
|
+
* | | | The default is `false`
|
|
33
|
+
*
|
|
34
|
+
* [Return value]
|
|
35
|
+
* - If the value is valid, this method will return `true`.
|
|
36
|
+
* - If the value is invalid, this method will return `false` and
|
|
37
|
+
* an `Error` object will be set to `this._error`.
|
|
38
|
+
*
|
|
39
|
+
* [Usage]
|
|
40
|
+
* let valid = parameterChecker.check(params, {
|
|
41
|
+
* level: {
|
|
42
|
+
* required: false,
|
|
43
|
+
* type: 'integer',
|
|
44
|
+
* max: 100
|
|
45
|
+
* },
|
|
46
|
+
* greeting: {
|
|
47
|
+
* required: true, // But an empty string is allowed.
|
|
48
|
+
* type: 'string',
|
|
49
|
+
* max: 20 // the number of characters must be up to 20.
|
|
50
|
+
* }
|
|
51
|
+
* });
|
|
52
|
+
* if(!valid) {
|
|
53
|
+
* let e = parameterChecker.error.message;
|
|
54
|
+
* throw new Error(message);
|
|
55
|
+
* }
|
|
56
|
+
* ---------------------------------------------------------------- */
|
|
57
57
|
check(obj, rules, required = false) {
|
|
58
58
|
this._error = null;
|
|
59
59
|
if (required) {
|
|
60
60
|
if (!this.isSpecified(obj)) {
|
|
61
61
|
this._error = {
|
|
62
|
-
code:
|
|
63
|
-
message:
|
|
62
|
+
code: "MISSING_REQUIRED",
|
|
63
|
+
message: "The first argument is missing.",
|
|
64
64
|
};
|
|
65
65
|
return false;
|
|
66
66
|
}
|
|
@@ -72,8 +72,8 @@ class ParameterChecker {
|
|
|
72
72
|
|
|
73
73
|
if (!this.isObject(obj)) {
|
|
74
74
|
this._error = {
|
|
75
|
-
code:
|
|
76
|
-
message:
|
|
75
|
+
code: "MISSING_REQUIRED",
|
|
76
|
+
message: "The first argument is missing.",
|
|
77
77
|
};
|
|
78
78
|
return false;
|
|
79
79
|
}
|
|
@@ -93,8 +93,8 @@ class ParameterChecker {
|
|
|
93
93
|
if (rule.required) {
|
|
94
94
|
result = false;
|
|
95
95
|
this._error = {
|
|
96
|
-
code:
|
|
97
|
-
message:
|
|
96
|
+
code: "MISSING_REQUIRED",
|
|
97
|
+
message: "The `" + name + "` is required.",
|
|
98
98
|
};
|
|
99
99
|
break;
|
|
100
100
|
} else {
|
|
@@ -102,23 +102,27 @@ class ParameterChecker {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
if (rule.type ===
|
|
105
|
+
if (rule.type === "float") {
|
|
106
106
|
result = this.isFloat(v, rule, name);
|
|
107
|
-
} else if (rule.type ===
|
|
107
|
+
} else if (rule.type === "integer") {
|
|
108
108
|
result = this.isInteger(v, rule, name);
|
|
109
|
-
} else if (rule.type ===
|
|
109
|
+
} else if (rule.type === "boolean") {
|
|
110
110
|
result = this.isBoolean(v, rule, name);
|
|
111
|
-
} else if (rule.type ===
|
|
111
|
+
} else if (rule.type === "array") {
|
|
112
112
|
result = this.isArray(v, rule, name);
|
|
113
|
-
} else if (rule.type ===
|
|
113
|
+
} else if (rule.type === "object") {
|
|
114
114
|
result = this.isObject(v, rule, name);
|
|
115
|
-
} else if (rule.type ===
|
|
115
|
+
} else if (rule.type === "string") {
|
|
116
116
|
result = this.isString(v, rule, name);
|
|
117
117
|
} else {
|
|
118
118
|
result = false;
|
|
119
119
|
this._error = {
|
|
120
|
-
code:
|
|
121
|
-
message:
|
|
120
|
+
code: "TYPE_UNKNOWN",
|
|
121
|
+
message:
|
|
122
|
+
"The rule specified for the `" +
|
|
123
|
+
name +
|
|
124
|
+
"` includes an unknown type: " +
|
|
125
|
+
rule.type,
|
|
122
126
|
};
|
|
123
127
|
}
|
|
124
128
|
|
|
@@ -132,55 +136,65 @@ class ParameterChecker {
|
|
|
132
136
|
}
|
|
133
137
|
|
|
134
138
|
/* ------------------------------------------------------------------
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
isFloat(value, rule = {}, name =
|
|
139
|
+
* isFloat(value, rule, name)
|
|
140
|
+
* - Check if the value is a float
|
|
141
|
+
*
|
|
142
|
+
* [Arguments]
|
|
143
|
+
* - value | Any | Required | The value you want to check
|
|
144
|
+
* - rule | Object | Optional |
|
|
145
|
+
* - required | Boolean | Optional | Required or not. Default is `false`.
|
|
146
|
+
* - min | Float | Optional | Minimum number
|
|
147
|
+
* - max | Float | Optional | Maximum number
|
|
148
|
+
* - enum | Array | Optional | list of possible values
|
|
149
|
+
* - name | String | Optional | Parameter name
|
|
150
|
+
*
|
|
151
|
+
* If non-number value is specified to the `min` or `max`,
|
|
152
|
+
* they will be ignored.
|
|
153
|
+
*
|
|
154
|
+
* [Return value]
|
|
155
|
+
* - If the value is valid, this method will return `true`.
|
|
156
|
+
* - If the value is invalid, this method will return `false` and
|
|
157
|
+
* an `Error` object will be set to `this._error`.
|
|
158
|
+
* ---------------------------------------------------------------- */
|
|
159
|
+
isFloat(value, rule = {}, name = "value") {
|
|
156
160
|
this._error = null;
|
|
157
161
|
|
|
158
162
|
if (!rule.required && !this.isSpecified(value)) {
|
|
159
163
|
return true;
|
|
160
164
|
}
|
|
161
165
|
|
|
162
|
-
if (typeof
|
|
166
|
+
if (typeof value !== "number") {
|
|
163
167
|
this._error = {
|
|
164
|
-
code:
|
|
165
|
-
message:
|
|
168
|
+
code: "TYPE_INVALID",
|
|
169
|
+
message: "The `" + name + "` must be a number (integer or float).",
|
|
166
170
|
};
|
|
167
171
|
return false;
|
|
168
172
|
}
|
|
169
173
|
|
|
170
|
-
if (typeof
|
|
174
|
+
if (typeof rule.min === "number") {
|
|
171
175
|
if (value < rule.min) {
|
|
172
176
|
this._error = {
|
|
173
|
-
code:
|
|
174
|
-
message:
|
|
177
|
+
code: "VALUE_UNDERFLOW",
|
|
178
|
+
message:
|
|
179
|
+
"The `" +
|
|
180
|
+
name +
|
|
181
|
+
"` must be grater than or equal to " +
|
|
182
|
+
rule.min +
|
|
183
|
+
".",
|
|
175
184
|
};
|
|
176
185
|
return false;
|
|
177
186
|
}
|
|
178
187
|
}
|
|
179
|
-
if (typeof
|
|
188
|
+
if (typeof rule.max === "number") {
|
|
180
189
|
if (value > rule.max) {
|
|
181
190
|
this._error = {
|
|
182
|
-
code:
|
|
183
|
-
message:
|
|
191
|
+
code: "VALUE_OVERFLOW",
|
|
192
|
+
message:
|
|
193
|
+
"The `" +
|
|
194
|
+
name +
|
|
195
|
+
"` must be less than or equal to " +
|
|
196
|
+
rule.max +
|
|
197
|
+
".",
|
|
184
198
|
};
|
|
185
199
|
return false;
|
|
186
200
|
}
|
|
@@ -188,8 +202,13 @@ class ParameterChecker {
|
|
|
188
202
|
if (Array.isArray(rule.enum) && rule.enum.length > 0) {
|
|
189
203
|
if (rule.enum.indexOf(value) === -1) {
|
|
190
204
|
this._error = {
|
|
191
|
-
code:
|
|
192
|
-
message:
|
|
205
|
+
code: "ENUM_UNMATCH",
|
|
206
|
+
message:
|
|
207
|
+
"The `" +
|
|
208
|
+
name +
|
|
209
|
+
"` must be any one of " +
|
|
210
|
+
JSON.stringify(rule.enum) +
|
|
211
|
+
".",
|
|
193
212
|
};
|
|
194
213
|
return false;
|
|
195
214
|
}
|
|
@@ -199,27 +218,27 @@ class ParameterChecker {
|
|
|
199
218
|
}
|
|
200
219
|
|
|
201
220
|
/* ------------------------------------------------------------------
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
isInteger(value, rule = {}, name =
|
|
221
|
+
* isInteger(value, rule)
|
|
222
|
+
* - Check if the value is an integer
|
|
223
|
+
*
|
|
224
|
+
* [Arguments]
|
|
225
|
+
* - value | Any | Required | The value you want to check
|
|
226
|
+
* - rule | Object | Optional |
|
|
227
|
+
* - required | Boolean | Optional | Required or not. Default is `false`.|
|
|
228
|
+
* - min | Float | Optional | Minimum number
|
|
229
|
+
* - max | Float | Optional | Maximum number
|
|
230
|
+
* - enum | Array | Optional | list of possible values
|
|
231
|
+
* - name | String | Optional | Parameter name
|
|
232
|
+
*
|
|
233
|
+
* If non-number value is specified to the `min` or `max`,
|
|
234
|
+
* they will be ignored.
|
|
235
|
+
*
|
|
236
|
+
* [Return value]
|
|
237
|
+
* - If the value is valid, this method will return `true`.
|
|
238
|
+
* - If the value is invalid, this method will return `false` and
|
|
239
|
+
* an `Error` object will be set to `this._error`.
|
|
240
|
+
* ---------------------------------------------------------------- */
|
|
241
|
+
isInteger(value, rule = {}, name = "value") {
|
|
223
242
|
this._error = null;
|
|
224
243
|
|
|
225
244
|
if (!rule.required && !this.isSpecified(value)) {
|
|
@@ -231,8 +250,8 @@ class ParameterChecker {
|
|
|
231
250
|
return true;
|
|
232
251
|
} else {
|
|
233
252
|
this._error = {
|
|
234
|
-
code:
|
|
235
|
-
message:
|
|
253
|
+
code: "TYPE_INVALID",
|
|
254
|
+
message: "The `" + name + "` must be an integer.",
|
|
236
255
|
};
|
|
237
256
|
return false;
|
|
238
257
|
}
|
|
@@ -242,31 +261,31 @@ class ParameterChecker {
|
|
|
242
261
|
}
|
|
243
262
|
|
|
244
263
|
/* ------------------------------------------------------------------
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
isBoolean(value, rule = {}, name =
|
|
264
|
+
* isBoolean(value, rule, name)
|
|
265
|
+
* - Check if the value is a boolean.
|
|
266
|
+
*
|
|
267
|
+
* [Arguments]
|
|
268
|
+
* - value | Any | Required | The value you want to check
|
|
269
|
+
* - rule | Object | Optional |
|
|
270
|
+
* - required | Boolean | Optional | Required or not. Default is `false`.
|
|
271
|
+
* - name | String | Optional | Parameter name
|
|
272
|
+
*
|
|
273
|
+
* [Return value]
|
|
274
|
+
* - If the value is valid, this method will return `true`.
|
|
275
|
+
* - If the value is invalid, this method will return `false` and
|
|
276
|
+
* an `Error` object will be set to `this._error`.
|
|
277
|
+
* ---------------------------------------------------------------- */
|
|
278
|
+
isBoolean(value, rule = {}, name = "value") {
|
|
260
279
|
this._error = null;
|
|
261
280
|
|
|
262
281
|
if (!rule.required && !this.isSpecified(value)) {
|
|
263
282
|
return true;
|
|
264
283
|
}
|
|
265
284
|
|
|
266
|
-
if (typeof
|
|
285
|
+
if (typeof value !== "boolean") {
|
|
267
286
|
this._error = {
|
|
268
|
-
code:
|
|
269
|
-
message:
|
|
287
|
+
code: "TYPE_INVALID",
|
|
288
|
+
message: "The `" + name + "` must be boolean.",
|
|
270
289
|
};
|
|
271
290
|
return false;
|
|
272
291
|
}
|
|
@@ -274,30 +293,30 @@ class ParameterChecker {
|
|
|
274
293
|
}
|
|
275
294
|
|
|
276
295
|
/* ------------------------------------------------------------------
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
isObject(value, rule = {}, name =
|
|
296
|
+
* isObject(value)
|
|
297
|
+
* - Check if the value is an object
|
|
298
|
+
*
|
|
299
|
+
* [Arguments]
|
|
300
|
+
* - value | Any | Required | The value you want to check
|
|
301
|
+
* - rule | Object | Optional |
|
|
302
|
+
* - required | Boolean | Optional | Required or not. Default is `false`.
|
|
303
|
+
* - name | String | Optional | Parameter name
|
|
304
|
+
*
|
|
305
|
+
* [Return value]
|
|
306
|
+
* - If the value is valid, this method will return `true`.
|
|
307
|
+
* - If the value is invalid, this method will return `false` and
|
|
308
|
+
* an `Error` object will be set to `this._error`.
|
|
309
|
+
* ---------------------------------------------------------------- */
|
|
310
|
+
isObject(value, rule = {}, name = "value") {
|
|
292
311
|
this._error = null;
|
|
293
312
|
if (!rule.required && !this.isSpecified(value)) {
|
|
294
313
|
return true;
|
|
295
314
|
}
|
|
296
315
|
|
|
297
|
-
if (typeof
|
|
316
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
298
317
|
this._error = {
|
|
299
|
-
code:
|
|
300
|
-
message:
|
|
318
|
+
code: "TYPE_INVALID",
|
|
319
|
+
message: "The `" + name + "` must be an object.",
|
|
301
320
|
};
|
|
302
321
|
return false;
|
|
303
322
|
}
|
|
@@ -305,26 +324,26 @@ class ParameterChecker {
|
|
|
305
324
|
}
|
|
306
325
|
|
|
307
326
|
/* ------------------------------------------------------------------
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
isArray(value, rule = {}, name =
|
|
327
|
+
* isArray(value, rule, name)
|
|
328
|
+
* - Check if the value is an `Array` object
|
|
329
|
+
*
|
|
330
|
+
* [Arguments]
|
|
331
|
+
* - value | Any | Required | The value you want to check
|
|
332
|
+
* - rule | Object | Optional |
|
|
333
|
+
* - required | Boolean | Optional | Required or not. Default is `false`.
|
|
334
|
+
* - min | Integer | Optional | Minimum number of elements in the array
|
|
335
|
+
* - max | Integer | Optional | Maximum number of elements in the array
|
|
336
|
+
* - name | String | Optional | Parameter name
|
|
337
|
+
*
|
|
338
|
+
* If non-number value is specified to the `min` or `max`,
|
|
339
|
+
* they will be ignored.
|
|
340
|
+
*
|
|
341
|
+
* [Return value]
|
|
342
|
+
* - If the value is valid, this method will return `true`.
|
|
343
|
+
* - If the value is invalid, this method will return `false` and
|
|
344
|
+
* an `Error` object will be set to `this._error`.
|
|
345
|
+
* ---------------------------------------------------------------- */
|
|
346
|
+
isArray(value, rule = {}, name = "value") {
|
|
328
347
|
this._error = null;
|
|
329
348
|
|
|
330
349
|
if (!rule.required && !this.isSpecified(value)) {
|
|
@@ -333,26 +352,36 @@ class ParameterChecker {
|
|
|
333
352
|
|
|
334
353
|
if (!Array.isArray(value)) {
|
|
335
354
|
this._error = {
|
|
336
|
-
code:
|
|
337
|
-
message:
|
|
355
|
+
code: "TYPE_INVALID",
|
|
356
|
+
message: "The value must be an array.",
|
|
338
357
|
};
|
|
339
358
|
return false;
|
|
340
359
|
}
|
|
341
360
|
|
|
342
|
-
if (typeof
|
|
361
|
+
if (typeof rule.min === "number") {
|
|
343
362
|
if (value.length < rule.min) {
|
|
344
363
|
this._error = {
|
|
345
|
-
code:
|
|
346
|
-
message:
|
|
364
|
+
code: "LENGTH_UNDERFLOW",
|
|
365
|
+
message:
|
|
366
|
+
"The number of characters in the `" +
|
|
367
|
+
name +
|
|
368
|
+
"` must be grater than or equal to " +
|
|
369
|
+
rule.min +
|
|
370
|
+
".",
|
|
347
371
|
};
|
|
348
372
|
return false;
|
|
349
373
|
}
|
|
350
374
|
}
|
|
351
|
-
if (typeof
|
|
375
|
+
if (typeof rule.max === "number") {
|
|
352
376
|
if (value.length > rule.max) {
|
|
353
377
|
this._error = {
|
|
354
|
-
code:
|
|
355
|
-
message:
|
|
378
|
+
code: "LENGTH_OVERFLOW",
|
|
379
|
+
message:
|
|
380
|
+
"The number of characters in the `" +
|
|
381
|
+
name +
|
|
382
|
+
"` must be less than or equal to " +
|
|
383
|
+
rule.max +
|
|
384
|
+
".",
|
|
356
385
|
};
|
|
357
386
|
return false;
|
|
358
387
|
}
|
|
@@ -362,78 +391,102 @@ class ParameterChecker {
|
|
|
362
391
|
}
|
|
363
392
|
|
|
364
393
|
/* ------------------------------------------------------------------
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
isString(value, rule = {}, name =
|
|
394
|
+
* isString(value, rule, name)
|
|
395
|
+
* - Check if the value is an `Array` object
|
|
396
|
+
*
|
|
397
|
+
* [Arguments]
|
|
398
|
+
* - value | Any | Required | The value you want to check
|
|
399
|
+
* - rule | Object | Optional |
|
|
400
|
+
* - required | Boolean | Optional | Required or not. Default is `false`.
|
|
401
|
+
* - min | Integer | Optional | Minimum number of characters in the string
|
|
402
|
+
* - max | Integer | Optional | Maximum number of characters in the string
|
|
403
|
+
* - minBytes | Integer | Optional | Minimum bytes of the string (UTF-8)
|
|
404
|
+
* - maxBytes | Integer | Optional | Maximum bytes of the string (UTF-8)
|
|
405
|
+
* - pattern | RegExp | Optional | Pattern of the string
|
|
406
|
+
* - enum | Array | Optional | list of possible values
|
|
407
|
+
* - name | String | Optional | Parameter name
|
|
408
|
+
*
|
|
409
|
+
* If non-number value is specified to the `min` or `max`,
|
|
410
|
+
* they will be ignored.
|
|
411
|
+
*
|
|
412
|
+
* [Return value]
|
|
413
|
+
* - If the value is valid, this method will return `true`.
|
|
414
|
+
* - If the value is invalid, this method will return `false` and
|
|
415
|
+
* an `Error` object will be set to `this._error`.
|
|
416
|
+
* ---------------------------------------------------------------- */
|
|
417
|
+
isString(value, rule = {}, name = "value") {
|
|
389
418
|
this._error = null;
|
|
390
419
|
|
|
391
420
|
if (!rule.required && !this.isSpecified(value)) {
|
|
392
421
|
return true;
|
|
393
422
|
}
|
|
394
423
|
|
|
395
|
-
if (typeof
|
|
424
|
+
if (typeof value !== "string") {
|
|
396
425
|
this._error = {
|
|
397
|
-
code:
|
|
398
|
-
message:
|
|
426
|
+
code: "TYPE_INVALID",
|
|
427
|
+
message: "The value must be a string.",
|
|
399
428
|
};
|
|
400
429
|
return false;
|
|
401
430
|
}
|
|
402
431
|
|
|
403
|
-
if (typeof
|
|
432
|
+
if (typeof rule.min === "number") {
|
|
404
433
|
if (value.length < rule.min) {
|
|
405
434
|
this._error = {
|
|
406
|
-
code:
|
|
407
|
-
message:
|
|
435
|
+
code: "LENGTH_UNDERFLOW",
|
|
436
|
+
message:
|
|
437
|
+
"The number of characters in the `" +
|
|
438
|
+
name +
|
|
439
|
+
"` must be grater than or equal to " +
|
|
440
|
+
rule.min +
|
|
441
|
+
".",
|
|
408
442
|
};
|
|
409
443
|
return false;
|
|
410
444
|
}
|
|
411
445
|
}
|
|
412
|
-
if (typeof
|
|
446
|
+
if (typeof rule.max === "number") {
|
|
413
447
|
if (value.length > rule.max) {
|
|
414
448
|
this._error = {
|
|
415
|
-
code:
|
|
416
|
-
message:
|
|
449
|
+
code: "LENGTH_OVERFLOW",
|
|
450
|
+
message:
|
|
451
|
+
"The number of characters in the `" +
|
|
452
|
+
name +
|
|
453
|
+
"` must be less than or equal to " +
|
|
454
|
+
rule.max +
|
|
455
|
+
".",
|
|
417
456
|
};
|
|
418
457
|
return false;
|
|
419
458
|
}
|
|
420
459
|
}
|
|
421
|
-
if (typeof
|
|
422
|
-
let blen = Buffer.from(value,
|
|
460
|
+
if (typeof rule.minBytes === "number") {
|
|
461
|
+
let blen = Buffer.from(value, "utf8").length;
|
|
423
462
|
if (blen < rule.minBytes) {
|
|
424
463
|
this._error = {
|
|
425
|
-
code:
|
|
426
|
-
message:
|
|
464
|
+
code: "LENGTH_UNDERFLOW",
|
|
465
|
+
message:
|
|
466
|
+
"The byte length of the `" +
|
|
467
|
+
name +
|
|
468
|
+
"` (" +
|
|
469
|
+
blen +
|
|
470
|
+
" bytes) must be grater than or equal to " +
|
|
471
|
+
rule.minBytes +
|
|
472
|
+
" bytes.",
|
|
427
473
|
};
|
|
428
474
|
return false;
|
|
429
475
|
}
|
|
430
476
|
}
|
|
431
|
-
if (typeof
|
|
432
|
-
let blen = Buffer.from(value,
|
|
477
|
+
if (typeof rule.maxBytes === "number") {
|
|
478
|
+
let blen = Buffer.from(value, "utf8").length;
|
|
433
479
|
if (blen > rule.maxBytes) {
|
|
434
480
|
this._error = {
|
|
435
|
-
code:
|
|
436
|
-
message:
|
|
481
|
+
code: "LENGTH_OVERFLOW",
|
|
482
|
+
message:
|
|
483
|
+
"The byte length of the `" +
|
|
484
|
+
name +
|
|
485
|
+
"` (" +
|
|
486
|
+
blen +
|
|
487
|
+
" bytes) must be less than or equal to " +
|
|
488
|
+
rule.maxBytes +
|
|
489
|
+
" bytes.",
|
|
437
490
|
};
|
|
438
491
|
return false;
|
|
439
492
|
}
|
|
@@ -441,8 +494,8 @@ class ParameterChecker {
|
|
|
441
494
|
if (rule.pattern instanceof RegExp) {
|
|
442
495
|
if (!rule.pattern.test(v)) {
|
|
443
496
|
this._error = {
|
|
444
|
-
code:
|
|
445
|
-
message:
|
|
497
|
+
code: "PATTERN_UNMATCH",
|
|
498
|
+
message: "The `" + name + "` does not conform with the pattern.",
|
|
446
499
|
};
|
|
447
500
|
return false;
|
|
448
501
|
}
|
|
@@ -450,8 +503,13 @@ class ParameterChecker {
|
|
|
450
503
|
if (Array.isArray(rule.enum) && rule.enum.length > 0) {
|
|
451
504
|
if (rule.enum.indexOf(value) === -1) {
|
|
452
505
|
this._error = {
|
|
453
|
-
code:
|
|
454
|
-
message:
|
|
506
|
+
code: "ENUM_UNMATCH",
|
|
507
|
+
message:
|
|
508
|
+
"The `" +
|
|
509
|
+
name +
|
|
510
|
+
"` must be any one of " +
|
|
511
|
+
JSON.stringify(rule.enum) +
|
|
512
|
+
".",
|
|
455
513
|
};
|
|
456
514
|
return false;
|
|
457
515
|
}
|