-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.js
200 lines (185 loc) · 5.35 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
200
const Ajv = require('ajv');
const { omaObjects } = require('oma-json');
const { COLLECTIONS, METHODS } = require('./constants');
const ajv = new Ajv({ removeAdditional: true });
const deviceSchema = {
additionalProperties: false,
properties: {
accessPointUrl: { type: ['null', 'string'] },
apiKey: { type: 'string' },
clientKey: { type: ['null', 'string'] },
createdAt: { type: ['null', 'string'] },
description: { type: ['null', 'string'] },
devEui: { type: 'string' },
frameCounter: { type: 'number' },
icons: { type: 'array', items: { type: 'string' } },
id: { type: 'string' },
lastSignal: { type: ['null', 'string'] },
messageProtocol: { type: 'string' },
messageProtocolVersion: { type: ['null', 'string'] },
name: { type: 'string' },
ownerId: { type: 'string' },
status: { type: 'boolean' },
transportProtocol: { type: 'string' },
transportProtocolVersion: { type: ['null', 'string'] },
type: { type: 'string' },
},
required: [
'accessPointUrl',
'apiKey',
'devEui',
'frameCounter',
'icons',
'id',
'lastSignal',
'messageProtocol',
'name',
'ownerId',
'status',
'transportProtocol',
'type',
],
};
function validateOmaObject(schema, resources, parentSchema, currentPath, sensor) {
if (!resources) {
return true;
}
const { type } = sensor;
const omaObject = omaObjects.find(({ value }) => value === type);
if (!omaObject) {
return false;
}
for (let key in omaObject.resources) {
if (!resources.hasOwnProperty(key)) {
resources[key] = omaObject.resources[key];
}
}
return true;
}
ajv.addKeyword('isValidResources', {
validate: validateOmaObject,
errors: false,
});
const sensorSchema = {
additionalProperties: false,
properties: {
description: { type: ['null', 'string'] },
devEui: { type: 'string' },
deviceId: { type: 'string' },
frameCounter: { type: 'number' },
icons: { type: 'array' },
id: { type: 'string' },
inPrefix: { type: ['null', 'string'] },
lastSignal: { type: ['null', 'string', 'object'] },
messageProtocol: { type: 'string' },
messageProtocolVersion: { type: ['null', 'string'] },
method: { type: 'string' },
name: { type: 'string' },
nativeNodeId: { type: ['null', 'string'] },
nativeSensorId: { type: 'string' },
nativeResource: { type: ['number', 'string'] },
nativeType: { type: ['number', 'string'] },
outPrefix: { type: ['null', 'string'] },
ownerId: { type: 'string' },
resource: { type: 'number' },
transportProtocol: { type: 'string' },
transportProtocolVersion: { type: ['null', 'string'] },
type: { type: 'number' },
resources: {
type: ['null', 'object'],
isValidResources: {},
additionalProperties: true,
},
value: { type: ['null', 'string', 'number', 'object', 'array'] },
},
required: [
'devEui',
'deviceId',
'frameCounter',
'icons',
'id',
'lastSignal',
'messageProtocol',
'name',
'nativeNodeId',
'nativeResource',
'nativeSensorId',
'nativeType',
'ownerId',
'transportProtocol',
'type',
],
};
const isValidCollection = (collection) => Object.values(COLLECTIONS).includes(collection);
const isValidMethod = (method) => Object.values(METHODS).includes(method);
const isValidTopic = (topic) => {
const parts = topic.split('/');
const [, collection, method] = parts;
if (!collection || !method) {
return false;
}
if (!isValidCollection(collection) || !isValidMethod(method)) {
return false;
}
if (collection === COLLECTIONS.DEVICE && parts.length < 4) {
return false;
}
if (collection === COLLECTIONS.SENSOR && parts.length < 6) {
return false;
}
return true;
};
function matchTopic(ts, t) {
if (ts === '#') {
return true;
} else if (ts.startsWith('$share')) {
/* The following allows shared subscriptions (as in MQTT v5)
http://docs.oasis-open.org/mqtt/mqtt/v5.0/cs02/mqtt-v5.0-cs02.html#_Toc514345522
4.8.2 describes shares like:
$share/{ShareName}/{filter}
$share is a literal string that marks the Topic Filter as being a Shared Subscription Topic Filter.
{ShareName} is a character string that does not include "/", "+" or "#"
{filter} The remainder of the string has the same syntax and semantics as a Topic Filter in a non-shared subscription. Refer to section 4.7.
*/
ts = ts.replace(/^\$share\/[^#+/]+\/(.*)/g, '$1');
}
const re = new RegExp(
'^' +
ts
.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g, '\\$1')
.replace(/\+/g, '[^/]+')
.replace(/\/#$/, '(/.*)?') +
'$',
);
return re.test(t);
}
const validateDevice = (device) => {
const validate = ajv.compile(deviceSchema);
const isValid = validate(device);
if (!isValid) {
console.log(validate.errors);
}
return { isValid, device };
};
const validateSensor = (sensor) => {
const validate = ajv.compile(sensorSchema);
const isValid = validate(sensor);
if (!isValid) {
console.log(validate.errors);
}
return { isValid, sensor };
};
const validateInstance = {
device: (device) => validateDevice(device),
sensor: (sensor) => validateSensor(sensor),
measurement: (measurement) => ({ isValid: true, measurement }),
};
module.exports = {
isValidCollection,
isValidMethod,
isValidTopic,
matchTopic,
validateInstance,
validateDevice,
validateSensor,
};