You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by yu...@apache.org on 2012/09/18 01:05:42 UTC

svn commit: r1386901 - in /incubator/ambari/branches/AMBARI-666/ambari-web: app/utils/validator.js test/utils/ test/utils/validator_test.js

Author: yusaku
Date: Mon Sep 17 23:05:42 2012
New Revision: 1386901

URL: http://svn.apache.org/viewvc?rev=1386901&view=rev
Log:
AMBARI-746. Added Validator object and its unit test - those were missed in the last commit (yusaku)

Added:
    incubator/ambari/branches/AMBARI-666/ambari-web/app/utils/validator.js
    incubator/ambari/branches/AMBARI-666/ambari-web/test/utils/
    incubator/ambari/branches/AMBARI-666/ambari-web/test/utils/validator_test.js

Added: incubator/ambari/branches/AMBARI-666/ambari-web/app/utils/validator.js
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-web/app/utils/validator.js?rev=1386901&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-web/app/utils/validator.js (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-web/app/utils/validator.js Mon Sep 17 23:05:42 2012
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+module.exports = {
+
+  isValidEmail: function(value) {
+    var emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
+    return emailRegex.test(value);
+  },
+
+  isValidInt: function(value) {
+    var intRegex = /^-?\d+$/;
+    return intRegex.test(value);
+  },
+
+  isValidFloat: function(value) {
+    if (typeof value === 'string' && value.trim() === '') {
+      return false;
+    }
+    var floatRegex = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
+    return floatRegex.test(value);
+  }
+
+};
\ No newline at end of file

Added: incubator/ambari/branches/AMBARI-666/ambari-web/test/utils/validator_test.js
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-web/test/utils/validator_test.js?rev=1386901&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-web/test/utils/validator_test.js (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-web/test/utils/validator_test.js Mon Sep 17 23:05:42 2012
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var validator = require('utils/validator');
+
+describe('validator', function () {
+
+  describe('#isValidEmail(value)', function () {
+    it('should return false if value is null', function () {
+      expect(validator.isValidEmail(null)).to.equal(false);
+    })
+    it('should return false if value is ""', function () {
+      expect(validator.isValidEmail('')).to.equal(false);
+    })
+    it('should return false if value is "a.com"', function () {
+      expect(validator.isValidEmail('a.com')).to.equal(false);
+    })
+    it('should return false if value is "@a.com"', function () {
+      expect(validator.isValidEmail('@a.com')).to.equal(false);
+    })
+    it('should return false if value is "a@.com"', function () {
+      expect(validator.isValidEmail('a@.com')).to.equal(false);
+    })
+    it('should return true if value is "a@a.com"', function () {
+      expect(validator.isValidEmail('a@a.com')).to.equal(true);
+    })
+    it('should return true if value is "user@a.b.com"', function () {
+      expect(validator.isValidEmail('user@a.b.com')).to.equal(true);
+    })
+  })
+
+  describe('#isValidInt(value)', function () {
+    it('should return false if value is null', function () {
+      expect(validator.isValidInt(null)).to.equal(false);
+    })
+    it('should return false if value is ""', function () {
+      expect(validator.isValidInt('')).to.equal(false);
+    })
+    it('should return false if value is "abc"', function () {
+      expect(validator.isValidInt('abc')).to.equal(false);
+    })
+    it('should return false if value is "0xff"', function () {
+      expect(validator.isValidInt('0xff')).to.equal(false);
+    })
+    it('should return false if value is " 1""', function () {
+      expect(validator.isValidInt(' 1')).to.equal(false);
+    })
+    it('should return false if value is "1 "', function () {
+      expect(validator.isValidInt('1 ')).to.equal(false);
+    })
+    it('should return true if value is "10"', function () {
+      expect(validator.isValidInt('10')).to.equal(true);
+    })
+    it('should return true if value is "-123"', function () {
+      expect(validator.isValidInt('-123')).to.equal(true);
+    })
+    it('should return true if value is "0"', function () {
+      expect(validator.isValidInt('0')).to.equal(true);
+    })
+    it('should return true if value is 10', function () {
+      expect(validator.isValidInt(10)).to.equal(true);
+    })
+    it('should return true if value is -123', function () {
+      expect(validator.isValidInt(10)).to.equal(true);
+    })
+    it('should return true if value is 0', function () {
+      expect(validator.isValidInt(10)).to.equal(true);
+    })
+  })
+
+  describe('#isValidFloat(value)', function () {
+    it('should return false if value is null', function () {
+      expect(validator.isValidFloat(null)).to.equal(false);
+    })
+    it('should return false if value is ""', function () {
+      expect(validator.isValidFloat('')).to.equal(false);
+    })
+    it('should return false if value is "abc"', function () {
+      expect(validator.isValidFloat('abc')).to.equal(false);
+    })
+    it('should return false if value is "0xff"', function () {
+      expect(validator.isValidFloat('0xff')).to.equal(false);
+    })
+    it('should return false if value is " 1""', function () {
+      expect(validator.isValidFloat(' 1')).to.equal(false);
+    })
+    it('should return false if value is "1 "', function () {
+      expect(validator.isValidFloat('1 ')).to.equal(false);
+    })
+    it('should return true if value is "10"', function () {
+      expect(validator.isValidFloat('10')).to.equal(true);
+    })
+    it('should return true if value is "-123"', function () {
+      expect(validator.isValidFloat('-123')).to.equal(true);
+    })
+    it('should return true if value is "0"', function () {
+      expect(validator.isValidFloat('0')).to.equal(true);
+    })
+    it('should return true if value is 10', function () {
+      expect(validator.isValidFloat(10)).to.equal(true);
+    })
+    it('should return true if value is -123', function () {
+      expect(validator.isValidFloat(10)).to.equal(true);
+    })
+    it('should return true if value is 0', function () {
+      expect(validator.isValidFloat(10)).to.equal(true);
+    })
+    it('should return true if value is "0.0"', function () {
+      expect(validator.isValidFloat("0.0")).to.equal(true);
+    })
+    it('should return true if value is "10.123"', function () {
+      expect(validator.isValidFloat("10.123")).to.equal(true);
+    })
+    it('should return true if value is "-10.123"', function () {
+      expect(validator.isValidFloat("-10.123")).to.equal(true);
+    })
+    it('should return true if value is 10.123', function () {
+      expect(validator.isValidFloat(10.123)).to.equal(true);
+    })
+    it('should return true if value is -10.123', function () {
+      expect(validator.isValidFloat(-10.123)).to.equal(true);
+    })
+
+  })
+
+})
\ No newline at end of file