Passed
Push — development ( 6972b1...c2ccf0 )
by André
02:23
created

Validate.json   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
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
Are you sure you want to assign to error here, or did you intend to make a comparison like error === new Error("The...ers are not been set.")?
Loading history...
Unused Code introduced by
The assignment to variable error seems to be never used. Consider removing it.
Loading history...
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
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
70
71
}
72
73
module.exports = new Validate
74