validator.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 at the start
  40. * @param value
  41. * @return {Boolean}
  42. */
  43. isValidDir: function(value){
  44. var floatRegex = /^\/[0-9a-z]*/;
  45. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  46. for(var i = 0; i < dirs.length; i++){
  47. if(!floatRegex.test(dirs[i])){
  48. return false;
  49. }
  50. }
  51. return true;
  52. },
  53. /**
  54. * validate directory doesn't start "home" or "homes"
  55. * @param value
  56. * @returns {boolean}
  57. */
  58. isAllowedDir: function(value) {
  59. var dirs = value.replace(/,/g,' ').trim().split(new RegExp("\\s+", "g"));
  60. for(var i = 0; i < dirs.length; i++){
  61. if(dirs[i].startsWith('/home') || dirs[i].startsWith('/homes')) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. },
  67. /**
  68. * validate ip address with port
  69. * @param value
  70. * @return {Boolean}
  71. */
  72. isIpAddress: function(value) {
  73. 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})$/;
  74. return ipRegex.test(value);
  75. },
  76. /**
  77. * validate hostname
  78. * @param value
  79. * @return {Boolean}
  80. */
  81. isHostname: function(value) {
  82. 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})$)/;
  83. return regex.test(value);
  84. },
  85. hasSpaces: function(value) {
  86. var regex = /(\s+)/;
  87. return regex.test(value);
  88. },
  89. isNotTrimmed: function(value) {
  90. var regex = /(^\s+|\s+$)/;
  91. return regex.test(value);
  92. },
  93. /**
  94. * validate domain name with port
  95. * @param value
  96. * @return {Boolean}
  97. */
  98. isDomainName: function(value) {
  99. var domainRegex = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  100. return domainRegex.test(value);
  101. },
  102. /**
  103. * validate username
  104. * @param value
  105. * @return {Boolean}
  106. */
  107. isValidUserName: function(value) {
  108. var usernameRegex = /^[a-z]([-a-z0-9]{0,30})$/;
  109. return usernameRegex.test(value);
  110. },
  111. /**
  112. * validate key of configurations
  113. * @param value
  114. * @return {Boolean}
  115. */
  116. isValidConfigKey: function(value) {
  117. var configKeyRegex = /^[0-9a-z_\-\.\*]+$/i;
  118. return configKeyRegex.test(value);
  119. },
  120. empty:function (e) {
  121. switch (e) {
  122. case "":
  123. case 0:
  124. case "0":
  125. case null:
  126. case false:
  127. case undefined:
  128. case typeof this == "undefined":
  129. return true;
  130. default :
  131. return false;
  132. }
  133. }
  134. };