validator.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.split(',');
  48. if (dirs.some(function(i) { return i.startsWith(' '); })) {
  49. return false;
  50. }
  51. for(var i = 0; i < dirs.length; i++){
  52. if(!floatRegex.test(dirs[i]) && !winRegex.test(dirs[i]) && !winUrlRegex.test(dirs[i])){
  53. return false;
  54. }
  55. }
  56. return true;
  57. },
  58. /**
  59. * defines if config value looks like link to other config
  60. * @param value
  61. * @returns {boolean}
  62. */
  63. isConfigValueLink: function(value) {
  64. return /^\${.+}$/.test(value);
  65. },
  66. /**
  67. * validate directory with slash at the start
  68. * @param value
  69. * @returns {boolean}
  70. */
  71. isValidDataNodeDir: function(value) {
  72. var dirRegex = /^(\[[0-9a-zA-Z]+\])?(file:\/\/)?(\/[0-9a-z]*)/;
  73. var winRegex = /^(\[[0-9a-zA-Z]+\])?[a-zA-Z]:\\[0-9a-zA-Z]*/;
  74. var winUrlRegex = /^(\[[0-9a-zA-Z]+\])?file:\/\/\/[a-zA-Z]:\/[0-9a-zA-Z]*/;
  75. var dirs = value.split(',');
  76. if (dirs.some(function (i) {return i.startsWith(' '); })) {
  77. return false;
  78. }
  79. for(var i = 0; i < dirs.length; i++){
  80. if(!dirRegex.test(dirs[i]) && !winRegex.test(dirs[i]) && !winUrlRegex.test(dirs[i])){
  81. return false;
  82. }
  83. }
  84. return true;
  85. },
  86. /**
  87. * validate directory doesn't start "home" or "homes"
  88. * @param value
  89. * @returns {boolean}
  90. */
  91. isAllowedDir: function(value) {
  92. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  93. for(var i = 0; i < dirs.length; i++){
  94. if(dirs[i].startsWith('/home') || dirs[i].startsWith('/homes')) {
  95. return false;
  96. }
  97. }
  98. return true;
  99. },
  100. /**
  101. * validate ip address with port
  102. * @param value
  103. * @return {Boolean}
  104. */
  105. isIpAddress: function(value) {
  106. 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})$/;
  107. return ipRegex.test(value);
  108. },
  109. /**
  110. * validate hostname
  111. * @param value
  112. * @return {Boolean}
  113. */
  114. isHostname: function(value) {
  115. 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})$)/;
  116. return value === 'localhost' || regex.test(value);
  117. },
  118. hasSpaces: function(value) {
  119. var regex = /(\s+)/;
  120. return regex.test(value);
  121. },
  122. isNotTrimmed: function(value) {
  123. var regex = /(^\s+|\s+$)/;
  124. return regex.test(value);
  125. },
  126. /**
  127. * Check if string ends with spaces.
  128. * For multiline content only last line will be checked.
  129. *
  130. * @method isNotTrimmedLeft
  131. * @param {String} value
  132. * @returns {Boolean} - <code>true</code> if ends with spaces
  133. */
  134. isNotTrimmedRight: function(value) {
  135. return /\s+$/.test(("" + value).split(/\n/).slice(-1)[0]);
  136. },
  137. /**
  138. * validate domain name with port
  139. * @param value
  140. * @return {Boolean}
  141. */
  142. isDomainName: function(value) {
  143. var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  144. return domainRegex.test(value);
  145. },
  146. /**
  147. * validate username
  148. * @param value
  149. * @return {Boolean}
  150. */
  151. isValidUserName: function(value) {
  152. var usernameRegex = /^[a-z]([-a-z0-9]{0,30})$/;
  153. return usernameRegex.test(value);
  154. },
  155. /**
  156. * validate db name
  157. * @param value
  158. * @returns {boolean}
  159. */
  160. isValidDbName: function(value) {
  161. var dbPattern = /^\S+$/;
  162. return dbPattern.test(value);
  163. },
  164. /**
  165. * validate key of configurations
  166. * @param value
  167. * @return {Boolean}
  168. */
  169. isValidConfigKey: function(value) {
  170. var configKeyRegex = /^[0-9a-z_\-\.\*]+$/i;
  171. return configKeyRegex.test(value);
  172. },
  173. /**
  174. * validate configuration group name
  175. * @param value
  176. * @return {Boolean}
  177. */
  178. isValidConfigGroupName: function(value) {
  179. var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
  180. return configKeyRegex.test(value);
  181. },
  182. /**
  183. * validate alert group name
  184. * @param value
  185. * @return {Boolean}
  186. */
  187. isValidAlertGroupName: function(value) {
  188. var configKeyRegex = /^[\s0-9a-z_\-]+$/i;
  189. return configKeyRegex.test(value);
  190. },
  191. empty:function (e) {
  192. switch (e) {
  193. case "":
  194. case 0:
  195. case "0":
  196. case null:
  197. case false:
  198. case undefined:
  199. case typeof this == "undefined":
  200. return true;
  201. default :
  202. return false;
  203. }
  204. },
  205. /**
  206. * Validate string that will pass as parameter to .matches() url param.
  207. * Try to prevent invalid regexp.
  208. * For example: /api/v1/clusters/c1/hosts?Hosts/host_name.matches(.*localhost.)
  209. *
  210. * @param {String} value - string to validate
  211. * @return {Boolean}
  212. * @method isValidMatchesRegexp
  213. */
  214. isValidMatchesRegexp: function(value) {
  215. var checkPair = function(chars) {
  216. chars = chars.map(function(c) { return '\\' + c; });
  217. var charsReg = new RegExp(chars.join('|'), 'g');
  218. if (charsReg.test(value)) {
  219. var pairContentReg = new RegExp(chars.join('.*'), 'g');
  220. if (!pairContentReg.test(value)) return false;
  221. var pairCounts = chars.map(function(c) { return value.match(new RegExp(c, 'g')).length; });
  222. if (pairCounts[0] != pairCounts[1] ) return false;
  223. }
  224. return true;
  225. };
  226. if (/^[\?\|\*\!,]/.test(value)) return false;
  227. return /^((\.\*?)?([\w\s\[\]\/\?\-_,\|\*\!\{\}]*)?)+(\.\*?)?$/g.test(value) && (checkPair(['[',']'])) && (checkPair(['{','}']));
  228. },
  229. /**
  230. * Remove validation messages for components which are already installed
  231. */
  232. filterNotInstalledComponents: function(validationData) {
  233. var hostComponents = App.HostComponent.find();
  234. return validationData.resources[0].items.filter(function(item) {
  235. // true is there is no host with this component
  236. return hostComponents.filterProperty("componentName", item["component-name"]).filterProperty("hostName", item.host).length === 0;
  237. });
  238. },
  239. isValidRackId: function(path) {
  240. // See app/message.js:hostPopup.setRackId.invalid
  241. return /^\/[/.\w-]+$/.test(path);
  242. },
  243. /**
  244. * Validate url
  245. * @param value
  246. * @return {Boolean}
  247. */
  248. isValidURL: function(value) {
  249. var urlRegex = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([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])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
  250. return urlRegex.test(value);
  251. },
  252. /**
  253. * Validate base URL
  254. * @param {string} value
  255. * @returns {boolean}
  256. */
  257. isValidBaseUrl: function (value) {
  258. var remotePattern = /^(?:(?:https?|ftp):\/{2})(?:\S+(?::\S*)?@)?(?:(?:(?:[\w\-.]))*)(?::[0-9]+)?(?:\/\S*)?$/,
  259. localPattern = /^file:\/{2,3}([a-zA-Z][:|]\/){0,1}[\w~!*'();@&=\/\\\-+$,?%#.\[\]]+$/;
  260. return remotePattern.test(value) || localPattern.test(value);
  261. },
  262. /**
  263. * Validate if empty or has white spaces
  264. * @param value
  265. * @return {boolean}
  266. */
  267. isEmptyOrSpaces: function(value) {
  268. var regex = /(\s+)/;
  269. return (!value || regex.test(value));
  270. }
  271. };