mirror of
https://git.proxmox.com/git/proxmox-widget-toolkit
synced 2025-06-25 22:12:56 +00:00
network edit: shorten and improve bridge vlan ID validator
This does a few things, but all affecting the same validator and there would not be much value with separate commits, so just use one. Namely: - reduce code by a lot, mostly by having a explicit ordered if-else chain that avoids the need for some extra checks as further branches can assume that former did not evaluate to true, thus we cans safe the closure that checked invalidity for a range-atom. While this allows entering "useless" ranges like "2-2" it's not clear why that should be a disallowed range, it's perfectly clear about what it represents. - use Number.isNaN to avoid oddities from global isNaN that MDN warns against [0] - give explicit error messages for different failure cases like out-of-range or not-a-number - place error messages under gettext, our translators frequently ask to avoid untranslatable literals. - support the same lists as the backend does, i.e. allow multiple whitespace and comma and semicolon as separators. [0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#description Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
b1a3eb7195
commit
ffe41ad5e3
@ -74,39 +74,22 @@ Ext.define('Proxmox.node.NetworkEdit', {
|
||||
return true;
|
||||
}
|
||||
|
||||
let vid_list = value.split(' ');
|
||||
|
||||
let invalidVid = function(tag) {
|
||||
if (!isNaN(tag) && (tag < 2 || tag > 4094)) {
|
||||
return `not a valid VLAN ID '${tag}'`;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
for (const vid of vid_list) {
|
||||
for (const vid of value.split(/\s+[,;]?/)) {
|
||||
if (!vid) {
|
||||
continue;
|
||||
}
|
||||
let res = vid.match(/^(\d+)(?:-(\d+))?$/);
|
||||
if (!res) {
|
||||
return `not a valid VLAN configuration '${vid}'`;
|
||||
return Ext.String.format(gettext("not a valid bridge VLAN ID entry: {0}"), vid);
|
||||
}
|
||||
let start = Number(res[1]);
|
||||
let end = Number(res[2]);
|
||||
let start = Number(res[1]), end = Number(res[2] ?? res[1]); // end=start for single IDs
|
||||
|
||||
res = invalidVid(start);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res = invalidVid(end);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if (start >= end) {
|
||||
return `VID range must go from lower to higher tag: '${vid}'`;
|
||||
if (Number.isNaN(start) || Number.isNaN(end)) {
|
||||
return Ext.String.format(gettext('VID range includes not-a-number: {0}'), vid);
|
||||
} else if (start > end) {
|
||||
return Ext.String.format(gettext('VID range must go from lower to higher tag: {0}'), vid);
|
||||
} else if (start < 2 || end > 4094) { // check just one each, we already ensured start < end
|
||||
return Ext.String.format(gettext('VID range outside of allowed 2 and 4094 limit: {0}'), vid);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user