fix validator override call for slice/slicePlus, add network_ports validation, allow...
authorScott Baker <smbaker@gmail.com>
Sat, 24 Jan 2015 00:37:51 +0000 (16:37 -0800)
committerScott Baker <smbaker@gmail.com>
Sat, 24 Jan 2015 00:37:51 +0000 (16:37 -0800)
planetstack/core/xoslib/static/js/xoslib/xos-backbone.js
planetstack/core/xoslib/static/js/xoslib/xos-util.js

index 1ca1307..3d2d7f7 100644 (file)
@@ -399,7 +399,11 @@ if (! window.XOSLIB_LOADED ) {
 //        }
 
         if ((typeof xosvalidators !== "undefined") && xosvalidators[modelName]) {
-            modelAttrs["validators"] = xosvalidators[modelName];
+            modelAttrs["validators"] = $.extend({}, xosvalidators[modelName], attrs["validators"] || {});
+        } else if (attrs["validators"]) {
+            modelAttrs["validators"] = attrs["validators"];
+            console.log(attrs);
+            console.log(modelAttrs);
         }
 
         lib[modelName] = XOSModel.extend(modelAttrs);
@@ -485,7 +489,7 @@ if (! window.XOSLIB_LOADED ) {
                            inputType: {"enabled": "checkbox"},
                            modelName: "slice",
                            xosValidate: function(attrs, options) {
-                               errors = XOSModel.prototype.xosValidate(this, attrs, options);
+                               errors = XOSModel.prototype.xosValidate.call(this, attrs, options);
                                // validate that slice.name starts with site.login_base
                                site = attrs.site || this.site;
                                if ((site!=undefined) && (attrs.name!=undefined)) {
@@ -665,8 +669,9 @@ if (! window.XOSLIB_LOADED ) {
                            modelName: "slicePlus",
                            collectionName: "slicesPlus",
                            defaults: extend_defaults("slice", {"network_ports": "", "site_allocation": []}),
+                           validators: {"network_ports": ["portspec"]},
                            xosValidate: function(attrs, options) {
-                               errors = XOSModel.prototype.xosValidate(this, attrs, options);
+                               errors = XOSModel.prototype.xosValidate.call(this, attrs, options);
                                // validate that slice.name starts with site.login_base
                                site = attrs.site || this.site;
                                if ((site!=undefined) && (attrs.name!=undefined)) {
index 2fa38a6..04bd041 100644 (file)
@@ -75,6 +75,12 @@ function validateField(validatorName, value, obj) {
                 return "must be a valid url";
             }
             break;
+
+        case "portspec":
+            if (! $.trim(value).match(portlist_regexp())) {
+                return "must be a valid portspec (example: 'tcp 123, udp 456-789')"
+            }
+            break;
     }
 
     return true;
@@ -195,3 +201,47 @@ function parse_portlist(ports) {
     }
     return nats
 }
+
+function portlist_regexp() {
+    /* this constructs the big complicated regexp that validates port
+       specifiers. Saved here in long form, in case we need to change it
+       in the future.
+    */
+
+    paren = function(x) { return "(?:" + x + ")"; }
+    whitespace = " *";
+    protocol = paren("tcp|udp");
+    protocolSlash = protocol + paren(whitespace + "|\/");
+    numbers = paren("[0-9]+");
+    range = paren(numbers + paren("-|:") + numbers);
+    numbersOrRange = paren(numbers + "|" + range);
+    protoPorts = paren(protocolSlash + numbersOrRange);
+    protoPortsCommas = paren(paren(protoPorts + "," + whitespace)+"+");
+    multiProtoPorts = paren(protoPortsCommas + protoPorts);
+    portSpec = "^" + paren(protoPorts + "|" + multiProtoPorts) + "$";
+    return RegExp(portSpec);
+}
+
+function portlist_selftest() {
+    r = portlist_regexp();
+    assert(! "tcp".match(r), 'should not have matched: "tcp"');
+    assert("tcp 1".match(r), 'should have matched: "tcp 1"');
+    assert("tcp 123".match(r), 'should have matched: "tcp 123"');
+    assert("tcp  123".match(r), 'should have matched: "tcp 123"');
+    assert("tcp 123-456".match(r), 'should have matched: "tcp 123-456"');
+    assert("tcp 123:456".match(r), 'should have matched: "tcp 123:456"');
+    assert(! "tcp 123-".match(r), 'should have matched: "tcp 123-"');
+    assert(! "tcp 123:".match(r), 'should have matched: "tcp 123:"');
+    assert(! "foo 123".match(r), 'should not have matched "foo 123"');
+    assert("udp 123".match(r), 'should have matched: "udp 123"');
+    assert("tcp 123,udp 456".match(r), 'should have matched: "tcp 123,udp 456"');
+    assert("tcp 123, udp 456".match(r), 'should have matched: "tcp 123, udp 456"');
+    assert("tcp 123,  udp 456".match(r), 'should have matched: "tcp 123,  udp 456"');
+    assert("tcp 123-45, udp 456".match(r), 'should have matched: "tcp 123-45, udp 456"');
+    assert("tcp 123-45, udp 456, tcp 11, tcp 22:45, udp 76, udp 47:49, udp 60-61".match(r), 'should have matched: "tcp 123-45, udp 456, tcp 11, tcp 22:45, udp 76, udp 47:49, udp 60-61"');
+    return "done";
+}
+
+//portlist_selftest();
+
+