validate.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { ErrorCode, WhereFilterOpList, OrderDirectionList, FieldType } from './constant';
  2. import { Util } from './util';
  3. import { ERRORS } from './const/code';
  4. import { E } from './utils/utils';
  5. import { getType } from './utils/type';
  6. import { SYMBOL_GEO_POINT } from './helper/symbol';
  7. const validOptionsKeys = ['limit', 'offset', 'projection', 'order', 'multiple', 'timeout'];
  8. export class Validate {
  9. static isGeopoint(point, degree) {
  10. if (Util.whichType(degree) !== FieldType.Number) {
  11. throw new Error('Geo Point must be number type');
  12. }
  13. const degreeAbs = Math.abs(degree);
  14. if (point === 'latitude' && degreeAbs > 90) {
  15. throw new Error('latitude should be a number ranges from -90 to 90');
  16. }
  17. else if (point === 'longitude' && degreeAbs > 180) {
  18. throw new Error('longitude should be a number ranges from -180 to 180');
  19. }
  20. return true;
  21. }
  22. static isInteger(param, num) {
  23. if (!Number.isInteger(num)) {
  24. throw new Error(param + ErrorCode.IntergerError);
  25. }
  26. return true;
  27. }
  28. static mustBeBoolean(param, bool) {
  29. if (typeof bool !== 'boolean') {
  30. throw new Error(param + ErrorCode.BooleanError);
  31. }
  32. return true;
  33. }
  34. static isProjection(param, value) {
  35. if (getType(value) !== 'object') {
  36. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `${param} projection must be an object` }));
  37. }
  38. for (const key in value) {
  39. const subValue = value[key];
  40. if (getType(subValue) === 'number') {
  41. if (subValue !== 0 && subValue !== 1) {
  42. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `if the value in projection is of number, it must be 0 or 1` }));
  43. }
  44. }
  45. else if (getType(subValue) === 'object') {
  46. }
  47. else {
  48. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: 'invalid projection' }));
  49. }
  50. }
  51. return true;
  52. }
  53. static isOrder(param, value) {
  54. if (getType(value) !== 'object') {
  55. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `${param} order must be an object` }));
  56. }
  57. for (let key in value) {
  58. const subValue = value[key];
  59. if (subValue !== 1 && subValue !== -1) {
  60. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `order value must be 1 or -1` }));
  61. }
  62. }
  63. return true;
  64. }
  65. static isFieldOrder(direction) {
  66. if (OrderDirectionList.indexOf(direction) === -1) {
  67. throw new Error(ErrorCode.DirectionError);
  68. }
  69. return true;
  70. }
  71. static isFieldPath(path) {
  72. if (!/^[a-zA-Z0-9-_\.]/.test(path)) {
  73. throw new Error();
  74. }
  75. return true;
  76. }
  77. static isOperator(op) {
  78. if (WhereFilterOpList.indexOf(op) === -1) {
  79. throw new Error(ErrorCode.OpStrError);
  80. }
  81. return true;
  82. }
  83. static isCollName(name) {
  84. if (!/^[a-zA-Z0-9]([a-zA-Z0-9-_]){1,32}$/.test(name)) {
  85. throw new Error(ErrorCode.CollNameError);
  86. }
  87. return true;
  88. }
  89. static isDocID(docId) {
  90. if (!/^([a-fA-F0-9]){24}$/.test(docId)) {
  91. throw new Error(ErrorCode.DocIDError);
  92. }
  93. return true;
  94. }
  95. static isValidOptions(options = {}) {
  96. if (getType(options) !== 'object') {
  97. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `options must be an object` }));
  98. }
  99. const keys = Object.keys(options);
  100. for (const index in keys) {
  101. if (validOptionsKeys.indexOf(keys[index]) < 0) {
  102. throw E(Object.assign(Object.assign({}, ERRORS.INVALID_PARAM), { message: `${keys[index]} is invalid options key` }));
  103. }
  104. }
  105. const { limit, offset, projection, order } = options;
  106. const { multiple } = options;
  107. if (limit !== undefined) {
  108. Validate.isInteger('limit', limit);
  109. }
  110. if (offset !== undefined) {
  111. Validate.isInteger('offset', offset);
  112. }
  113. if (projection !== undefined) {
  114. Validate.isProjection('projection', projection);
  115. }
  116. if (order !== undefined) {
  117. Validate.isOrder('order', order);
  118. }
  119. if (multiple !== undefined) {
  120. Validate.mustBeBoolean('multiple', multiple);
  121. }
  122. if (options.timeout !== undefined) {
  123. Validate.isInteger('timeout', options.timeout);
  124. }
  125. return true;
  126. }
  127. static isCentersPhere(param) {
  128. if (Array.isArray(param) && param.length === 2) {
  129. if (getType(param[0]) === 'object' &&
  130. param[0]._internalType === SYMBOL_GEO_POINT &&
  131. typeof param[1] === 'number') {
  132. return true;
  133. }
  134. if (Array.isArray(param[0]) && param[0].length === 2) {
  135. const longitude = param[0][0];
  136. const latitude = param[0][1];
  137. Validate.isGeopoint('longitude', longitude);
  138. Validate.isGeopoint('latitude', latitude);
  139. if (typeof param[1] === 'number') {
  140. return true;
  141. }
  142. }
  143. }
  144. throw new Error(`${ErrorCode.CentersPhereError}`);
  145. }
  146. }