validator.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. module.exports = {
  19. isValidEmail: function(value) {
  20. 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;
  21. return emailRegex.test(value);
  22. },
  23. isValidInt: function(value) {
  24. var intRegex = /^-?\d+$/;
  25. return intRegex.test(value);
  26. },
  27. isValidUNIXUser: function(value){
  28. var regex = /^[a-z_][a-z0-9_-]{0,31}$/;
  29. return regex.test(value);
  30. },
  31. isValidFloat: function(value) {
  32. if (typeof value === 'string' && value.trim() === '') {
  33. return false;
  34. }
  35. var floatRegex = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/;
  36. return floatRegex.test(value);
  37. },
  38. /**
  39. * validate directory with slash or drive at the start
  40. * @param value
  41. * @return {Boolean}
  42. */
  43. isValidDir: function(value){
  44. var floatRegex = /^\/[0-9a-z]*/;
  45. var winRegex = /^[a-z]:\\[0-9a-z]*/;
  46. var winUrlRegex = /^file:\/\/\/[a-z]:\/[0-9a-z]*/;
  47. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  48. for(var i = 0; i < dirs.length; i++){
  49. if(!floatRegex.test(dirs[i]) && !winRegex.test(dirs[i]) && !winUrlRegex.test(dirs[i])){
  50. return false;
  51. }
  52. }
  53. return true;
  54. },
  55. /**
  56. * validate directory with slash at the start
  57. * @param value
  58. * @returns {boolean}
  59. */
  60. isValidDataNodeDir: function(value) {
  61. var dirRegex = /^(\[[0-9a-zA-Z]+\])?(\/[0-9a-z]*)/;
  62. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  63. for(var i = 0; i < dirs.length; i++){
  64. if(!dirRegex.test(dirs[i])){
  65. return false;
  66. }
  67. }
  68. return true;
  69. },
  70. /**
  71. * validate directory doesn't start "home" or "homes"
  72. * @param value
  73. * @returns {boolean}
  74. */
  75. isAllowedDir: function(value) {
  76. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  77. for(var i = 0; i < dirs.length; i++){
  78. if(dirs[i].startsWith('/home') || dirs[i].startsWith('/homes')) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. },
  84. /**
  85. * validate ip address with port
  86. * @param value
  87. * @return {Boolean}
  88. */
  89. isIpAddress: function(value) {
  90. var ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)($|\:[0-9]{1,5})$/;
  91. return ipRegex.test(value);
  92. },
  93. /**
  94. * validate hostname
  95. * @param value
  96. * @return {Boolean}
  97. */
  98. isHostname: function(value) {
  99. var regex = /(?=^.{3,254}$)(^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*(\.[a-zA-Z]{1,62})$)/;
  100. return regex.test(value);
  101. },
  102. hasSpaces: function(value) {
  103. var regex = /(\s+)/;
  104. return regex.test(value);
  105. },
  106. isNotTrimmed: function(value) {
  107. var regex = /(^\s+|\s+$)/;
  108. return regex.test(value);
  109. },
  110. /**
  111. * validate domain name with port
  112. * @param value
  113. * @return {Boolean}
  114. */
  115. isDomainName: function(value) {
  116. var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  117. return domainRegex.test(value);
  118. },
  119. /**
  120. * validate username
  121. * @param value
  122. * @return {Boolean}
  123. */
  124. isValidUserName: function(value) {
  125. var usernameRegex = /^[a-z]([-a-z0-9]{0,30})$/;
  126. return usernameRegex.test(value);
  127. },
  128. /**
  129. * validate key of configurations
  130. * @param value
  131. * @return {Boolean}
  132. */
  133. isValidConfigKey: function(value) {
  134. var configKeyRegex = /^[0-9a-z_\-\.\*]+$/i;
  135. return configKeyRegex.test(value);
  136. },
  137. /**
  138. * validate configuration group name
  139. * @param value
  140. * @return {Boolean}
  141. */
  142. isValidConfigGroupName: function(value) {
  143. var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
  144. return configKeyRegex.test(value);
  145. },
  146. /**
  147. * validate alert group name
  148. * @param value
  149. * @return {Boolean}
  150. */
  151. isValidAlertGroupName: function(value) {
  152. var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
  153. return configKeyRegex.test(value);
  154. },
  155. empty:function (e) {
  156. switch (e) {
  157. case "":
  158. case 0:
  159. case "0":
  160. case null:
  161. case false:
  162. case undefined:
  163. case typeof this == "undefined":
  164. return true;
  165. default :
  166. return false;
  167. }
  168. },
  169. /**
  170. * Validate string that will pass as parameter to .matches() url param.
  171. * Try to prevent invalid regexp.
  172. * For example: /api/v1/clusters/c1/hosts?Hosts/host_name.matches(.*localhost.)
  173. *
  174. * @param {String} value - string to validate
  175. * @return {Boolean}
  176. * @method isValidMatchesRegexp
  177. */
  178. isValidMatchesRegexp: function(value) {
  179. var checkPair = function(chars) {
  180. chars = chars.map(function(c) { return '\\' + c; });
  181. var charsReg = new RegExp(chars.join('|'), 'g');
  182. if (charsReg.test(value)) {
  183. var pairContentReg = new RegExp(chars.join('.*'), 'g');
  184. if (!pairContentReg.test(value)) return false;
  185. var pairCounts = chars.map(function(c) { return value.match(new RegExp(c, 'g')).length; });
  186. if (pairCounts[0] != pairCounts[1] ) return false;
  187. }
  188. return true;
  189. };
  190. if (/^[\?\|\*\!,]/.test(value)) return false;
  191. return /^((\.\*?)?([\w\[\]\?\-_,\|\*\!\{\}]*)?)+(\.\*?)?$/g.test(value) && (checkPair(['[',']'])) && (checkPair(['{','}']));
  192. },
  193. /**
  194. * Remove validation messages for components which are already installed
  195. */
  196. filterNotInstalledComponents: function(validationData) {
  197. var hostComponents = App.HostComponent.find();
  198. return validationData.resources[0].items.filter(function(item) {
  199. // true is there is no host with this component
  200. return hostComponents.filterProperty("componentName", item["component-name"]).filterProperty("hostName", item.host).length === 0;
  201. });
  202. }
  203. };