83 lines
3.3 KiB
JavaScript
83 lines
3.3 KiB
JavaScript
|
|
|
|
const ipv4 = (params) => {
|
|
const ipv4WithMaskPattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(0|1[0-9]|2[0-9]|3[0-2]|[0-9])$/;
|
|
return (text) => {
|
|
return ipv4WithMaskPattern.test(text);
|
|
}
|
|
}
|
|
|
|
const ipv6 = (params) => {
|
|
const ipv6WithMaskPattern = /(^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(\/(\d{1,2}|1[0-1]\d|12[0-8]))(%.+)?\s*$)/;
|
|
|
|
|
|
return (text) => {
|
|
return ipv6WithMaskPattern.test(text);
|
|
}
|
|
}
|
|
|
|
const hostname = (params) => {
|
|
const maxHostnameLength = 253;
|
|
const maxLabelLength = 63;
|
|
const labelRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
|
|
|
|
return (text) => {
|
|
if (text.length > maxHostnameLength) {
|
|
return false;
|
|
}
|
|
const labels = text.split('.');
|
|
for (const label of labels) {
|
|
if (label.length < 1 || label.length > maxLabelLength) {
|
|
return false;
|
|
}
|
|
if (!labelRegex.test(label)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
const ipv4OrHost = (params) => {
|
|
const hostnameLocal = hostname(params);
|
|
const ipv4Local = ipv4(params);
|
|
return (text) => {
|
|
return ipv4Local(text) || hostnameLocal(text);
|
|
}
|
|
}
|
|
|
|
const ipv4or6 = (params) => {
|
|
const ipv4Local = ipv4(params);
|
|
const ipv6Local = ipv6(params);
|
|
return (text) => {
|
|
return ipv4Local(text) || ipv6Local(text);
|
|
}
|
|
}
|
|
|
|
const frequency = (params) => {
|
|
const factors = {
|
|
"mhz": 1e6,
|
|
"khz": 1e3,
|
|
"hz": 1e1,
|
|
"ghz": 1e9,
|
|
};
|
|
|
|
return (text) => {
|
|
const splited = text.split(/(\s+)/);
|
|
const numerator = parseFloat(splited[0]);
|
|
if (splited.length !== 3 || isNaN(numerator)) return false;
|
|
const factor = factors[splited[2].toLowerCase()];
|
|
if (!factor) return false;
|
|
const realFreq = factor * numerator;
|
|
return realFreq >= (params.min || 10*factors.mhz) && realFreq <= (params.max || 1*factors.ghz);
|
|
}
|
|
}
|
|
|
|
export const Validation = {
|
|
ipv4: ipv4,
|
|
ipv6: ipv6,
|
|
ipv4or6: ipv4or6,
|
|
hostname: hostname,
|
|
ipv4OrHost: ipv4OrHost,
|
|
frequency: frequency
|
|
}; |