summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2024-02-21 14:37:03 +0100
committerJo-Philipp Wich <jo@mein.io>2024-02-21 15:33:08 +0100
commitcf09f89df307f2e9d2b5dda152c571b3d0d1550d (patch)
treea4c0a32da385b8ccffdf86686551f0f5317d8275 /modules/luci-base
parent5ef203a46e06317506dab0c33cdea9ecca3f5fcc (diff)
luci-base: validation.js: add iprange, iprange4 and iprange6 validators
Add datatype validators for IP address ranges which are required for certain firewall inputs. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base')
-rw-r--r--modules/luci-base/htdocs/luci-static/resources/validation.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/validation.js b/modules/luci-base/htdocs/luci-static/resources/validation.js
index 9504e7f2c4..e4f8c7a201 100644
--- a/modules/luci-base/htdocs/luci-static/resources/validation.js
+++ b/modules/luci-base/htdocs/luci-static/resources/validation.js
@@ -5,6 +5,19 @@ function bytelen(x) {
return new Blob([x]).size;
}
+function arrayle(a, b) {
+ if (!Array.isArray(a) || !Array.isArray(b))
+ return false;
+
+ for (var i = 0; i < a.length; i++)
+ if (a[i] > b[i])
+ return false;
+ else if (a[i] < b[i])
+ return true;
+
+ return true;
+}
+
var Validator = baseclass.extend({
__name__: 'Validation',
@@ -333,6 +346,23 @@ var ValidatorFactory = baseclass.extend({
_('valid IPv6 network'));
},
+ iprange: function(negative) {
+ return this.assert(this.apply('iprange4', null, [negative]) || this.apply('iprange6', null, [negative]),
+ _('valid IP address range'));
+ },
+
+ iprange4: function(negative) {
+ var m = this.value.split('-');
+ return this.assert(m.length == 2 && arrayle(this.factory.parseIPv4(m[0]), this.factory.parseIPv4(m[1])),
+ _('valid IPv4 address range'));
+ },
+
+ iprange6: function(negative) {
+ var m = this.value.split('-');
+ return this.assert(m.length == 2 && arrayle(this.factory.parseIPv6(m[0]), this.factory.parseIPv6(m[1])),
+ _('valid IPv6 address range'));
+ },
+
port: function() {
var p = this.factory.parseInteger(this.value);
return this.assert(p >= 0 && p <= 65535, _('valid port value'));