/**************************************************************** ** Licensed Materials - Property of IBM ** ** IBM Cognos Products: Validator ** ** © Copyright IBM Corp. 2005, 2013 ** US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *****************************************************************/ // Copyright (C) 2008 IBM Cognos Incorporated. All Rights Reserved. // IBM Cognos and the IBM Cognos logo are trademarks of IBM Cognos Incorporated. /* * Public API */ /* * Returns 0 on success, a negative integer on fatal code failures, or * a positive integer on a verify failure. See CValidator.RES_* for detailed * failure codes. */ CValidator.prototype = { verify: function(name, value) { var res = CValidator.RES_VALID; var found = false; if (typeof this[name] == "function") { res = this[name](value); found = true; } for (i = 0; !found && typeof this["npFunc" + i] == "function"; ++i) { if (this["npFunc" + i](name)) { found = true; res = this["npvFunc" + i](value); } } if (!found && this["_mandatoryRules"]) { res = CValidator.RES_PARAMETER_NOT_FOUND; } return res } } CValidator.RES_VALID = 0; CValidator.RES_PARAMETER_NOT_FOUND = 1; CValidator.RES_TYPE_BOOLEAN = 100; CValidator.RES_TYPE_STRING_MIN_LEN = 200; CValidator.RES_TYPE_STRING_MAX_LEN = 201; CValidator.RES_NUMERIC_MIN = 300; CValidator.RES_NUMERIC_MAX = 301; CValidator.RES_NUMERIC_NAN = 302; CValidator.RES_INTEGRAL = 400; CValidator.RES_TYPE_INT = 500; CValidator.RES_TYPE_LONG = 600; CValidator.RES_TYPE_UNSIGNED_INT = 700; CValidator.RES_TYPE_FLOAT = 550; CValidator.RES_TYPE_DOUBLE = 650; CValidator.RES_ENUM_EMPTY = 800; CValidator.RES_ENUM_NOT_FOUND = 901; CValidator.RES_PATTERN = 1001; CValidator.RES_INVALID_URL = 2001; CValidator.REGEXP_URL = "^(\/|\\.\/|\\.\\.\/|#.+|[^.]([^:]+\/?))(([^\/]+|([^\/]+\/)+[^\/]+))$" + // relative path "|" + "^http(s)?:\/\/" + // protocol "([\\w-]+)([\\w.-]+)([\\w-]+)" + // domain "(:(\\d|[1-9]\\d|[1-9]\\d\\d|[1-9]" + // valid ports "\\d\\d\\d|[1-5]\\d\\d\\d\\d|6[0-4]\\d" + // 1-65535 "\\d\\d|65[0-4]\\d\\d|655[0-2]\\d|6553[0-5]))?" + "((\/?)|" + // optional if no path "((\/[0-9a-z_!~*'().;?<>:@&=+$,%#-]+)+\/?))$"; // path /* * Private Functions * * CValidator and methods prefixed by '_' are private and should not be called by components */ function CValidator(verifiers) { for (var v in verifiers) { this[v] = verifiers[v]; } } CValidator.prototype._enumeration = function(v, emptyValid, caseSensitive, items) { var strV = v + ""; var res = CValidator.RES_ENUM_NOT_FOUND; if (v.length == 0) { if (emptyValid) { res = CValidator.RES_VALID; } else { res = CValidator.RES_ENUM_EMPTY; } } else { if (!caseSensitive) { strV = strV.toLowerCase(); } for (i = 0; i < items.length; ++i) { if (strV == items[i]) { res = CValidator.RES_VALID; } } } return res; } CValidator.prototype._typeString = function(v, minLen, maxLen, urlType) { var strV = v + ""; var res = CValidator.RES_VALID; if (minLen != null && strV.length < minLen) { res = CValidator.RES_TYPE_STRING_MIN_LEN; } else if (maxLen != null && strV.length > maxLen) { res = CValidator.RES_TYPE_STRING_MAX_LEN; } if (urlType == "true" && res == CValidator.RES_VALID) { //Only perform these checks IFF the string is of type urlType. //Case insensitive regular expression. var r = new RegExp(CValidator.REGEXP_URL, "i"); if (!r.test(strV)) { res = CValidator.RES_INVALID_URL; } } return res; } CValidator.prototype._typeBoolean = function(v) { var strV = v + ""; return (strV == "true" || strV == "false" || strV == "0" || strV == "1")? CValidator.RES_VALID : CValidator.RES_TYPE_BOOLEAN; } CValidator._getNumber = function(v) { var n = v; switch ( typeof(v) ) { case "number": break; case "string": if (v == "") { n = NaN; } else if (v == "-0") { n = 0; } else { n = (+v); if (!isNaN(n) && ("a" + n) != ("a" + v)) { n = NaN; } } break; default: n = NaN; break; } return n; } CValidator.prototype._checkInteger = function(v, n) { var res = CValidator.RES_VALID; var f = Math.floor(n); if (v != "-0" && f + "" != v) { res = CValidator.RES_INTEGRAL; } return res; } CValidator.prototype._checkNumber = function(n, min, max) { var res = CValidator.RES_VALID; if (isNaN(n)) { res = CValidator.RES_NUMERIC_NAN; } else if (min != null && n < min) { res = CValidator.RES_NUMERIC_MIN; } else if (max != null && n > max) { res = CValidator.RES_NUMERIC_MAX; } return res; } CValidator.prototype._typeInt = function (v, min, max, emptyValid) { var res = CValidator.RES_VALID; var s = v + ""; if (s != "" || !emptyValid) { var n = CValidator._getNumber(v); res = this._checkNumber(n, min, max); if (res == CValidator.RES_VALID) { res = this._checkInteger(v, n); if (res == CValidator.RES_VALID) { if (n < -2147483648 || n > 2147483647) { res = CValidator.RES_TYPE_INT; } } } } return res; } CValidator.prototype._typeFloat = function (v, min, max, emptyValid) { var res = CValidator.RES_VALID; var s = v + ""; if (s != "" || !emptyValid) { var n = CValidator._getNumber(v); res = this._checkNumber(n, min, max); } return res; } // CValidator._CLong64 = // s as to be passed as a string function(s) { this.NaN = true; var p = /^[-]?[0-9]{1,19}$/; if (p.test(s)) { this.NaN = false; if (s == "-0") { s = "0"; } if (s.charAt(0) == "-") { this.negative = true; s = s.substring(1); } else { this.negative = false; } // check for Java long boundaries -9223372036854775808 and 9223372036854775807 if (s.length == 19) { p = /^([1-8])(9[0-1])|(92[0-1])|(922[0-2])|(9223[0-2])|(92233[0-6])|(922337[0-1])|(92233720[0-2])/; if (!p.test(s)) { p = /^(922337203[0-5])|(9223372036[0-7])|(92233720368[0-4])|(922337203685[0-3])|(9223372036854[0-6])/; if (!p.test(s)) { p = /^(92233720368547[0-6])|(922337203685477[0-4])|(9223372036854775[0-7])|(922337203685477580[0-7])/; if (!p.test(s)) { if (!(this.negative && s.charAt(18) == '8')) { this.NaN = true; } } } } } if (!this.NaN) { this.high = null; if (s.length > 10) { this.high = s.substring(0, 10); if (this.high == NaN) { this.NaN = true; } this.low = CValidator._getNumber(s.substring(10)); } else { this.low = CValidator._getNumber(s); } if (this.low == NaN) { this.NaN = true; } } } } CValidator._CLong64.prototype = { "smallerThan": function(min) { var res = false; var min64 = new CValidator._CLong64(min); if (min64 != null) { if (this.negative && !min64.negative) { res = true; } else if (!this.negative && min64.negative) { res = false; } else if (this.high == null && min64.high == null) { res = this.negative? this.low > min64.low : this.low < min64.low; } else if (this.high != null && min64.high == null) { res = this.negative; } else if (this.high == null && min64.high != null) { res = !this.negative; } else { // this.high != null && this.min64 != null if (this.high == min64.high) { res = this.negative? this.low > min64.low : this.low < min64.low; } else { res = this.negative? this.high > min64.high : this.high < min64.high; } } } return res; }, "greaterThan": function(max) { var res = false; var max64 = new CValidator._CLong64(max); if (max64 != null) { if (!this.negative && max64.negative) { res = true; } else if (this.negative && !max64.negative) { res = false; } else if (this.high == null && max64.high == null) { res = this.negative? this.low < max64.low : this.low > max64.low; } else if (this.high != null && max64.high == null) { res = !this.negative; } else if (this.high == null && max64.high != null) { res = this.negative; } else { // this.high != null && this.min64 != null if (this.high == max64.high) { res = this.negative? this.low < max64.low : this.low > max64.low; } else { res = this.negative? this.high < max64.low : this.high > max64.low; } } } return res; } } CValidator._CDouble64 = // s as to be passed as a string function (s) { this.NaN = true; var p = /^[-]?\d+.?\d*$/; if (p.test(s)) { this.NaN = false; if (s == "-0") { s = "0"; } if (s.charAt(0) == "-") { this.negative = true; s = s.substring(1); } else { this.negative = false; } var indexOfDecimal = s.indexOf("."); if (indexOfDecimal != -1) { this.fractional = CValidator._getNumber(s.substring(indexOfDecimal + 1)); if (this.fractional == NaN) { this.fractional = null; } s = s.substring(0, indexOfDecimal - 1); } else { this.fractional = null; } if (!this.NaN) { this.high = null; if (s.length > 10) { this.high = s.substring(0, 10); if (this.high == NaN) { this.NaN = true; } this.low = CValidator._getNumber(s.substring(10)); } else { this.low = CValidator._getNumber(s); } if (this.low == NaN) { this.NaN = true; } } } } CValidator._CDouble64.prototype = { "smallerThan": function (min) { var res = false; var min64 = new CValidator._CDouble64(min); if (min64 != null) { if (this.negative && !min64.negative) { res = true; } else if (!this.negative && min64.negative) { res = false; } else if (this.high == null && min64.high == null) { if (this.low == min64.low) { if (this.fractional != null && min64.fractional != null) { res = this.negative ? this.fractional > min64.fractional : this.fractional < min64.fractional; } else if (this.fractional == null && min64.fractional == null) { res = true; } else if (this.fractional == null && min64.fractional != null) { res = !this.negative; } else if (this.fractional != null && min64.fractional == null) { res = this.negative; } } else { res = this.negative ? this.low > min64.low : this.low < min64.low; } } else if (this.high != null && min64.high == null) { res = this.negative; } else if (this.high == null && min64.high != null) { res = !this.negative; } else { // this.high != null && this.min64 != null if (this.high == min64.high) { if (this.low == min64.low) { if (this.fractional != null && min64.fractional != null) { res = this.negative ? this.fractional > min64.fractional : this.fractional < min64.fractional; } else if (this.fractional == null && min64.fractional == null) { res = true; } else if (this.fractional == null && min64.fractional != null) { res = !this.negative; } else if (this.fractional != null && min64.fractional == null) { res = this.negative; } } else { res = this.negative ? this.low > min64.low : this.low < min64.low; } } else { res = this.negative ? this.high > min64.high : this.high < min64.high; } } } return res; }, "greaterThan": function (max) { var res = false; var max64 = new CValidator._CDouble64(max); if (max64 != null) { if (!this.negative && max64.negative) { res = true; } else if (this.negative && !max64.negative) { res = false; } else if (this.high == null && max64.high == null) { if (this.low == max64.low) { if (this.fractional != null && min64.fractional != null) { res = this.negative ? this.fractional < max64.fractional : this.fractional > max64.fractional; } else if (this.fractional == null && max64.fractional == null) { res = true; } else if (this.fractional == null && max64.fractional != null) { res = this.negative; } else if (this.fractional != null && max64.fractional == null) { res = !this.negative; } } else { res = this.negative ? this.low < max64.low : this.low > max64.low; } } else if (this.high != null && max64.high == null) { res = !this.negative; } else if (this.high == null && max64.high != null) { res = this.negative; } else { // this.high != null && this.min64 != null if (this.high == max64.high) { if (this.low == max64.low) { if (this.fractional != null && min64.fractional != null) { res = this.negative ? this.fractional < max64.fractional : this.fractional > max64.fractional; } else if (this.fractional == null && max64.fractional == null) { res = true; } else if (this.fractional == null && max64.fractional != null) { res = this.negative; } else if (this.fractional != null && max64.fractional == null) { res = !this.negative; } } else { res = this.negative ? this.low < max64.low : this.low > max64.low; } } else { res = this.negative ? this.high < max64.low : this.high > max64.low; } } } return res; } } CValidator.prototype._typeLong = function (v, min, max, emptyValid) { var res = CValidator.RES_VALID; var s = v + ""; if (s != "" || !emptyValid) { var s64 = new CValidator._CLong64(s); if (s64.NaN) { res = CValidator.RES_NUMERIC_NAN; } else { if (min != null) { if (s64.smallerThan(min)) { res = CValidator.RES_NUMERIC_MIN; } } if (res == CValidator.RES_VALID && max != null) { if (s64.greaterThan(max)) { res = CValidator.RES_NUMERIC_MAX; } } } } return res; } CValidator.prototype._typeDouble = function (v, min, max, emptyValid) { var res = CValidator.RES_VALID; var s = v + ""; if (s != "" || !emptyValid) { var s64 = new CValidator._CDouble64(s); if (s64.NaN) { res = CValidator.RES_NUMERIC_NAN; } else { if (min != null) { if (s64.smallerThan(min)) { res = CValidator.RES_NUMERIC_MIN; } } if (res == CValidator.RES_VALID && max != null) { if (s64.greaterThan(max)) { res = CValidator.RES_NUMERIC_MAX; } } } } return res; } CValidator.prototype._typeUnsignedInt = function(v, min, max, emptyValid) { var res = CValidator.RES_VALID; var s = v + ""; if (s != "" || !emptyValid) { var n = CValidator._getNumber(v); res = this._checkNumber(n, min, max); if (res == CValidator.RES_VALID) { res = this._checkInteger(v, n); if (res == CValidator.RES_VALID) { if (n < 0 || n > 4294967295) { res = CValidator.RES_TYPE_UNSIGNED_INT; } } } } return res; } CValidator.prototype._pattern = function(v, caseSensitive, not, findMode, regex) { var res = CValidator.RES_VALID; var a = ""; if (!caseSensitive) { a += "i"; } if (!findMode) { regex = "^" + regex + "$"; } var r = new RegExp(regex, a); if (r.test(v)) { if (not) { res = CValidator.RES_PATTERN; } } else { if (!not) { res = CValidator.RES_PATTERN; } } return res; } CValidator.prototype._checkGlobals = function(v) { var res = CValidator.RES_VALID; for (i = 0; res == CValidator.RES_VALID && typeof this["gFunc" + i] == "function"; ++i) { res = this["gFunc" + i](v) != CValidator.RES_VALID; } return res; }