1 | class Validate { |
||
2 | |||
3 | url (string) { |
||
4 | /* eslint no-useless-escape: 0 */ |
||
5 | let regex = /(mqtt|mqtts|tcp|tls|ws|wss):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/ |
||
6 | var pattern = new RegExp(regex) |
||
7 | return pattern.test(string) |
||
8 | } |
||
9 | |||
10 | json(json) { |
||
11 | if (typeof json !== 'object' ) { |
||
12 | return false |
||
13 | } |
||
14 | return true |
||
15 | } |
||
16 | |||
17 | sourceConfiguration (input, callback) { |
||
18 | let error = null |
||
19 | |||
20 | if (!input.source.url) { |
||
21 | if (!error) { |
||
22 | error = new Error('URL for MQTT broker has not been set.') |
||
23 | } |
||
24 | } |
||
25 | |||
26 | if (!this.url(input.source.url)) { |
||
27 | if (!error) { |
||
28 | error = new Error('Your defined URL is invalid. It should start with mqtt://') |
||
29 | } |
||
30 | } |
||
31 | |||
32 | let port = Number.parseInt(input.source.port) |
||
33 | if (Number.isNaN(port)) { |
||
34 | input.source.port = 1883 |
||
35 | } else { |
||
36 | input.source.port = port |
||
37 | } |
||
38 | |||
39 | callback(input, error) |
||
40 | } |
||
41 | |||
42 | paramConfiguration (input, callback) { |
||
43 | let error = null |
||
44 | |||
45 | if (!input.hasOwnProperty('params')) { |
||
46 | if (!error) { |
||
47 | return error = new Error('The parameters are not been set.') |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
![]() |
|||
48 | } |
||
49 | } else { |
||
50 | if (!input.params.topic) { |
||
51 | if (!error) { |
||
52 | error = new Error('The parameter topic has not been set.') |
||
53 | } |
||
54 | } |
||
55 | if (!input.params.payload) { |
||
56 | if (!error) { |
||
57 | error = new Error('The parameter payload has not been set.') |
||
58 | } |
||
59 | } |
||
60 | |||
61 | if (![0, 1, 2].includes(input.params.qos)) { |
||
62 | input.params.qos = 1 |
||
63 | } else { |
||
64 | input.params.qos.toString() |
||
65 | } |
||
66 | } |
||
67 | |||
68 | callback(input, error) |
||
69 | } |
||
0 ignored issues
–
show
|
|||
70 | |||
71 | } |
||
72 | |||
73 | module.exports = new Validate |
||
74 |