validator.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-zA-Z]*/;
  46. var winUrlRegex = /^file:\/\/\/[a-zA-Z]:\/[0-9a-zA-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 winRegex = /^(\[[0-9a-zA-Z]+\])?[a-zA-Z]:\\[0-9a-zA-Z]*/;
  63. var winUrlRegex = /^(\[[0-9a-zA-Z]+\])?file:\/\/\/[a-zA-Z]:\/[0-9a-zA-Z]*/;
  64. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  65. for(var i = 0; i < dirs.length; i++){
  66. if(!dirRegex.test(dirs[i]) && !winRegex.test(dirs[i]) && !winUrlRegex.test(dirs[i])){
  67. return false;
  68. }
  69. }
  70. return true;
  71. },
  72. /**
  73. * validate directory doesn't start "home" or "homes"
  74. * @param value
  75. * @returns {boolean}
  76. */
  77. isAllowedDir: function(value) {
  78. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  79. for(var i = 0; i < dirs.length; i++){
  80. if(dirs[i].startsWith('/home') || dirs[i].startsWith('/homes')) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. },
  86. /**
  87. * validate ip address with port
  88. * @param value
  89. * @return {Boolean}
  90. */
  91. isIpAddress: function(value) {
  92. 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})$/;
  93. return ipRegex.test(value);
  94. },
  95. /**
  96. * validate hostname
  97. * @param value
  98. * @return {Boolean}
  99. */
  100. isHostname: function(value) {
  101. 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})$)/;
  102. return regex.test(value);
  103. },
  104. hasSpaces: function(value) {
  105. var regex = /(\s+)/;
  106. return regex.test(value);
  107. },
  108. isNotTrimmed: function(value) {
  109. var regex = /(^\s+|\s+$)/;
  110. return regex.test(value);
  111. },
  112. /**
  113. * validate domain name with port
  114. * @param value
  115. * @return {Boolean}
  116. */
  117. isDomainName: function(value) {
  118. var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  119. return domainRegex.test(value);
  120. },
  121. /**
  122. * validate username
  123. * @param value
  124. * @return {Boolean}
  125. */
  126. isValidUserName: function(value) {
  127. var usernameRegex = /^[a-z]([-a-z0-9]{0,30})$/;
  128. return usernameRegex.test(value);
  129. },
  130. /**
  131. * validate key of configurations
  132. * @param value
  133. * @return {Boolean}
  134. */
  135. isValidConfigKey: function(value) {
  136. var configKeyRegex = /^[0-9a-z_\-\.\*]+$/i;
  137. return configKeyRegex.test(value);
  138. },
  139. /**
  140. * validate configuration group name
  141. * @param value
  142. * @return {Boolean}
  143. */
  144. isValidConfigGroupName: function(value) {
  145. var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
  146. return configKeyRegex.test(value);
  147. },
  148. /**
  149. * validate alert group name
  150. * @param value
  151. * @return {Boolean}
  152. */
  153. isValidAlertGroupName: function(value) {
  154. var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
  155. return configKeyRegex.test(value);
  156. },
  157. empty:function (e) {
  158. switch (e) {
  159. case "":
  160. case 0:
  161. case "0":
  162. case null:
  163. case false:
  164. case undefined:
  165. case typeof this == "undefined":
  166. return true;
  167. default :
  168. return false;
  169. }
  170. },
  171. /**
  172. * Validate string that will pass as parameter to .matches() url param.
  173. * Try to prevent invalid regexp.
  174. * For example: /api/v1/clusters/c1/hosts?Hosts/host_name.matches(.*localhost.)
  175. *
  176. * @param {String} value - string to validate
  177. * @return {Boolean}
  178. * @method isValidMatchesRegexp
  179. */
  180. isValidMatchesRegexp: function(value) {
  181. var checkPair = function(chars) {
  182. chars = chars.map(function(c) { return '\\' + c; });
  183. var charsReg = new RegExp(chars.join('|'), 'g');
  184. if (charsReg.test(value)) {
  185. var pairContentReg = new RegExp(chars.join('.*'), 'g');
  186. if (!pairContentReg.test(value)) return false;
  187. var pairCounts = chars.map(function(c) { return value.match(new RegExp(c, 'g')).length; });
  188. if (pairCounts[0] != pairCounts[1] ) return false;
  189. }
  190. return true;
  191. };
  192. if (/^[\?\|\*\!,]/.test(value)) return false;
  193. return /^((\.\*?)?([\w\[\]\?\-_,\|\*\!\{\}]*)?)+(\.\*?)?$/g.test(value) && (checkPair(['[',']'])) && (checkPair(['{','}']));
  194. },
  195. /**
  196. * Remove validation messages for components which are already installed
  197. */
  198. filterNotInstalledComponents: function(validationData) {
  199. var hostComponents = App.HostComponent.find();
  200. return validationData.resources[0].items.filter(function(item) {
  201. // true is there is no host with this component
  202. return hostComponents.filterProperty("componentName", item["component-name"]).filterProperty("hostName", item.host).length === 0;
  203. });
  204. }
  205. };