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