luci-base: validation.js: optionally support negative prefixes
authorJo-Philipp Wich <jo@mein.io>
Mon, 1 Mar 2021 14:16:14 +0000 (15:16 +0100)
committerJo-Philipp Wich <jo@mein.io>
Mon, 1 Mar 2021 14:19:21 +0000 (15:19 +0100)
Support negative prefix length in the `cidr`, `cidr4`, `cidr6`, `ipmask`,
`ipmask4` and `ipmask6` data types when an optional truish flag is passed
to the datatype name.

Ref: #4812
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit cd06e708d8d8037486b77dab3389ca91a88f2dd0)

modules/luci-base/htdocs/luci-static/resources/validation.js

index eea837d64ee5c77a53c711ac31035d132f7ab4da..a01c8002fc69a181c6e561dea654015f68951d71 100644 (file)
@@ -268,18 +268,21 @@ var ValidatorFactory = baseclass.extend({
                                _('valid IPv6 prefix value (0-128)'));
                },
 
-               cidr: function() {
-                       return this.assert(this.apply('cidr4') || this.apply('cidr6'), _('valid IPv4 or IPv6 CIDR'));
+               cidr: function(negative) {
+                       return this.assert(this.apply('cidr4', null, [negative]) || this.apply('cidr6', null, [negative]),
+                               _('valid IPv4 or IPv6 CIDR'));
                },
 
-               cidr4: function() {
-                       var m = this.value.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/);
-                       return this.assert(m && this.factory.parseIPv4(m[1]) && this.apply('ip4prefix', m[2]), _('valid IPv4 CIDR'));
+               cidr4: function(negative) {
+                       var m = this.value.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(-)?(\d{1,2})$/);
+                       return this.assert(m && this.factory.parseIPv4(m[1]) && (negative || !m[2]) && this.apply('ip4prefix', m[3]),
+                               _('valid IPv4 CIDR'));
                },
 
-               cidr6: function() {
-                       var m = this.value.match(/^([0-9a-fA-F:.]+)\/(\d{1,3})$/);
-                       return this.assert(m && this.factory.parseIPv6(m[1]) && this.apply('ip6prefix', m[2]), _('valid IPv6 CIDR'));
+               cidr6: function(negative) {
+                       var m = this.value.match(/^([0-9a-fA-F:.]+)\/(-)?(\d{1,3})$/);
+                       return this.assert(m && this.factory.parseIPv6(m[1]) && (negative || !m[2]) && this.apply('ip6prefix', m[3]),
+                               _('valid IPv6 CIDR'));
                },
 
                ipnet4: function() {
@@ -300,18 +303,18 @@ var ValidatorFactory = baseclass.extend({
                        return this.assert(!(!v6 || v6[0] || v6[1] || v6[2] || v6[3]), _('valid IPv6 host id'));
                },
 
-               ipmask: function() {
-                       return this.assert(this.apply('ipmask4') || this.apply('ipmask6'),
+               ipmask: function(negative) {
+                       return this.assert(this.apply('ipmask4', null, [negative]) || this.apply('ipmask6', null, [negative]),
                                _('valid network in address/netmask notation'));
                },
 
-               ipmask4: function() {
-                       return this.assert(this.apply('cidr4') || this.apply('ipnet4') || this.apply('ip4addr'),
+               ipmask4: function(negative) {
+                       return this.assert(this.apply('cidr4', null, [negative]) || this.apply('ipnet4') || this.apply('ip4addr'),
                                _('valid IPv4 network'));
                },
 
-               ipmask6: function() {
-                       return this.assert(this.apply('cidr6') || this.apply('ipnet6') || this.apply('ip6addr'),
+               ipmask6: function(negative) {
+                       return this.assert(this.apply('cidr6', null, [negative]) || this.apply('ipnet6') || this.apply('ip6addr'),
                                _('valid IPv6 network'));
                },