hiveguard-backend
Version:
Backend for HiveGuard
77 lines (69 loc) • 1.88 kB
JavaScript
/*
* Copyright 2021-2022 Dimitrios-Georgios Akestoridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { URL } = require('url');
function isValidKey(potentialKey) {
return (
potentialKey
&& typeof potentialKey === 'string'
&& potentialKey.match(/^[0-9A-Fa-f]{32}$/)
);
}
function isValidWIDSSensorID(potentialID) {
return (
potentialID
&& typeof potentialID === 'string'
&& potentialID.match(/^[A-Za-z]{1}[0-9A-Za-z-._]{0,126}$/)
);
}
function isValidWIDSSensorAPI(potentialAPI) {
if (
!potentialAPI
|| typeof potentialAPI !== 'string'
|| potentialAPI.length < 1
|| potentialAPI.length > 127
|| potentialAPI.endsWith('/')
) {
return false;
}
try {
const urlObject = new URL(potentialAPI);
return (urlObject.protocol === 'http:');
} catch (err) {
return false;
}
}
function isValidHours(potentialHours) {
return (
potentialHours
&& typeof potentialHours === 'string'
&& !Number.isNaN(Number(potentialHours))
&& Number(potentialHours) > 0.0
);
}
function isValidAlertID(potentialAlertID) {
return (
potentialAlertID
&& typeof potentialAlertID === 'string'
&& potentialAlertID.match(/^HG[0-9]{10}(E|F)[0-9]+$/)
);
}
module.exports = {
isValidKey,
isValidWIDSSensorID,
isValidWIDSSensorAPI,
isValidHours,
isValidAlertID,
};