Validation.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { ChildrenStatusType } from '@/components/status/Types';
  2. import { MetricType, METRIC_TYPES_VALUES } from '@/consts';
  3. export type ValidType =
  4. | 'email'
  5. | 'require'
  6. | 'confirm'
  7. | 'range'
  8. | 'password'
  9. | 'clusterName'
  10. | 'CIDRorIP'
  11. | 'integer'
  12. | 'positiveNumber'
  13. | 'collectionName'
  14. | 'dimension'
  15. | 'multiple'
  16. | 'partitionName'
  17. | 'firstCharacter'
  18. | 'specValueOrRange';
  19. export interface ICheckMapParam {
  20. value: string;
  21. extraParam?: IExtraParam;
  22. rule: ValidType;
  23. }
  24. export interface IExtraParam {
  25. // used for confirm or any compare type
  26. compareValue?: string | number;
  27. // used for length type
  28. min?: number;
  29. max?: number;
  30. type?: 'string' | 'number';
  31. // used for dimension
  32. metricType?: MetricType;
  33. multipleNumber?: number;
  34. // used for check start item
  35. invalidTypes?: TypeEnum[];
  36. }
  37. export type CheckMap = {
  38. [key in ValidType]: boolean;
  39. };
  40. export enum TypeEnum {
  41. 'number' = 'number',
  42. }
  43. export const checkEmptyValid = (value: string | number): boolean => {
  44. return String(value).trim() !== '';
  45. };
  46. export const checkEmail = (value: string): boolean => {
  47. const re = /\S+@\S+\.\S+/;
  48. return re.test(value);
  49. };
  50. /* password rules:
  51. * 1. at least one uppercase letter
  52. * 2. at least one lowercase letter
  53. * 3. at least one number
  54. * 4. at least one nonalphanumeric character: ! @ # $ % ^ & * ( ) _ + - = [ ] { } | '
  55. */
  56. export const checkPasswordStrength = (value: string): boolean => {
  57. const re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*([^A-Za-z0-9]))/;
  58. return re.test(value);
  59. };
  60. export const checkRange = (param: {
  61. value: string | number;
  62. min?: number;
  63. max?: number;
  64. type?: 'string' | 'number';
  65. }): boolean => {
  66. const { value, min = 0, max = 0, type } = param;
  67. const length = type === 'number' ? Number(value) : (value as string).length;
  68. let result = true;
  69. const conditionMap = {
  70. min: length >= min,
  71. max: length <= max,
  72. };
  73. if (min !== 0) {
  74. result = result && conditionMap.min;
  75. }
  76. if (max !== 0) {
  77. result = result && conditionMap.max;
  78. }
  79. return result;
  80. };
  81. export const checkClusterName = (value: string): boolean => {
  82. const re = new RegExp('^[A-Za-z0-9+-=._:@/ ]*$');
  83. return re.test(value);
  84. };
  85. export const checkIP = (value: string): boolean => {
  86. const re =
  87. /^(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]?)$/;
  88. return re.test(value);
  89. };
  90. export const checkCIDR = (value: string): boolean => {
  91. const re =
  92. /^(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/([0-9]|[1-2]\d|3[0-2])$/;
  93. return re.test(value);
  94. };
  95. export const checkIpOrCIDR = (value: string): boolean => {
  96. // const re = new RegExp('^([0-9]{1,3}.){3}[0-9]{1,3}($|/(16|24)$)');
  97. // return re.test(value);
  98. return checkIP(value) || checkCIDR(value);
  99. };
  100. // collection name can only be combined with number, letter or _
  101. export const checkCollectionName = (value: string): boolean => {
  102. const re = /^[0-9,a-z,A-Z_]+$/;
  103. return re.test(value);
  104. };
  105. /**
  106. * check data type
  107. * @param type data types, like string, number
  108. * @param value
  109. * @returns whether value's value is equal to param type
  110. */
  111. export const checkType = (type: TypeEnum, value: string): boolean => {
  112. switch (type) {
  113. case TypeEnum.number:
  114. return !isNaN(Number(value));
  115. default:
  116. return true;
  117. }
  118. };
  119. /**
  120. * check input first character
  121. * @param value
  122. * @param invalidTypes
  123. * @returns whether start letter type not belongs to invalid types
  124. */
  125. export const checkFirstCharacter = (param: {
  126. value: string;
  127. invalidTypes?: TypeEnum[];
  128. }): boolean => {
  129. const { value, invalidTypes } = param;
  130. const start = value[0];
  131. const types = invalidTypes || [];
  132. for (let type of types) {
  133. const result = checkType(type, start);
  134. if (result) {
  135. return false;
  136. }
  137. }
  138. return true;
  139. };
  140. export const checkPartitionName = (value: string): boolean => {
  141. return value !== '_default';
  142. };
  143. export const checkMultiple = (param: {
  144. value: string;
  145. multipleNumber?: number;
  146. }): boolean => {
  147. const { value, multipleNumber = 1 } = param;
  148. return Number(value) % multipleNumber === 0;
  149. };
  150. export const checkDimension = (param: {
  151. value: string;
  152. metricType?: MetricType;
  153. multipleNumber?: number;
  154. }): boolean => {
  155. const { value, metricType, multipleNumber } = param;
  156. if (
  157. metricType === METRIC_TYPES_VALUES.IP ||
  158. metricType === METRIC_TYPES_VALUES.L2
  159. ) {
  160. return true;
  161. }
  162. return checkMultiple({ value, multipleNumber });
  163. };
  164. /**
  165. * function to check whether value(type: number) is equal to specified value or in valid range
  166. * @param param specValue and params checkRange function needed
  167. * @returns whether input is valid
  168. */
  169. export const checkSpecValueOrRange = (param: {
  170. value: number;
  171. min: number;
  172. max: number;
  173. compareValue: number;
  174. }): boolean => {
  175. const { value, min, max, compareValue } = param;
  176. return (
  177. value === compareValue || checkRange({ min, max, value, type: 'number' })
  178. );
  179. };
  180. export const getCheckResult = (param: ICheckMapParam): boolean => {
  181. const { value, extraParam = {}, rule } = param;
  182. const numberValue = Number(value);
  183. const checkMap = {
  184. email: checkEmail(value),
  185. require: checkEmptyValid(value),
  186. confirm: value === extraParam?.compareValue,
  187. range: checkRange({
  188. value,
  189. min: extraParam?.min,
  190. max: extraParam?.max,
  191. type: extraParam?.type,
  192. }),
  193. password: checkPasswordStrength(value),
  194. clusterName: checkClusterName(value),
  195. CIDRorIP: checkIpOrCIDR(value),
  196. integer: !isNaN(numberValue) && Number.isInteger(numberValue),
  197. positiveNumber: !isNaN(numberValue) && numberValue > 0,
  198. collectionName: checkCollectionName(value),
  199. dimension: checkDimension({
  200. value,
  201. metricType: extraParam?.metricType,
  202. multipleNumber: extraParam?.multipleNumber,
  203. }),
  204. multiple: checkMultiple({
  205. value,
  206. multipleNumber: extraParam?.multipleNumber,
  207. }),
  208. partitionName: checkPartitionName(value),
  209. firstCharacter: checkFirstCharacter({
  210. value,
  211. invalidTypes: extraParam?.invalidTypes,
  212. }),
  213. specValueOrRange: checkSpecValueOrRange({
  214. value: Number(value),
  215. min: extraParam?.min || 0,
  216. max: extraParam?.max || 0,
  217. compareValue: Number(extraParam.compareValue) || 0,
  218. }),
  219. };
  220. return checkMap[rule];
  221. };
  222. /**
  223. * Check collection is loading or not
  224. */
  225. export const checkLoading = (v: any): boolean =>
  226. v._loadedPercentage !== '-1' && v._loadedPercentage !== '100';
  227. /**
  228. * Check collection is index building or not.
  229. * @param v
  230. * @returns boolean
  231. */
  232. export const checkIndexBuilding = (v: any): boolean =>
  233. v._indexState === ChildrenStatusType.CREATING;