validate.js 6.0 KB

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