constraint.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { MESSAGE_TYPE, MESSAGE_STATUS, CONTACT_TYPE } from "utils/constant";
  2. import { error } from "utils";
  3. import { isPlainObject } from "utils/validate";
  4. const constraintContactBasic = data =>
  5. constraintObject(data, {
  6. id: true,
  7. displayName: true,
  8. avatar: true,
  9. type: {
  10. required: true,
  11. has: CONTACT_TYPE
  12. }
  13. });
  14. const constraintMessageBasic = data =>
  15. constraintObject(data, {
  16. content: true,
  17. sendTime: true,
  18. type: {
  19. required: true,
  20. has: MESSAGE_TYPE
  21. }
  22. });
  23. // constraintContact({
  24. // id: "123",
  25. // displayName: "123asd",
  26. // avatar: "123",
  27. // type: "single",
  28. // message: {
  29. // unread: 0,
  30. // sendTime: 12312312,
  31. // content: "12312312",
  32. // type: "image"
  33. // }
  34. // });
  35. constraintContact({
  36. id: "123",
  37. displayName: "123asd",
  38. avatar: "123",
  39. type: "single",
  40. unread: 0,
  41. lastSendTime: "",
  42. subText: "12312312"
  43. // message: {
  44. // unread: 0,
  45. // sendTime: 12312312,
  46. // content: "12312312",
  47. // type: "image"
  48. // }
  49. });
  50. // constraintRecentContact({
  51. // fromContactId: 0,
  52. // unread: 0,
  53. // sendTime: 12312312,
  54. // content: "12312312"
  55. // });
  56. constraintMessage({
  57. id: "123",
  58. status: "succeed",
  59. type: "image",
  60. sendTime: 12312312312,
  61. content: "asdas",
  62. fromContactId: "123",
  63. fromUser: { id: "123", displayName: "123", avatar: "123", type: "single" }
  64. });
  65. export function constraintObject(data, options) {
  66. if (!data || !isPlainObject(data)) {
  67. error("argument must be an object");
  68. }
  69. Object.keys(options).forEach(k => {
  70. const option = options[k];
  71. const val = data[k];
  72. if ((option === true || option.required === true) && val === undefined) {
  73. error(`"${k}" cannot be "${val}" `);
  74. } else if (option.has && !option.has.includes(val)) {
  75. error(
  76. `"${k}" cannot be "${val}",can only be the following data "${
  77. option.has
  78. }"`
  79. );
  80. }
  81. });
  82. return true;
  83. }
  84. // export function constraintRecentContact(data) {
  85. // constraintContact(data);
  86. // constraintMessageBasic(data.message);
  87. // constraintObject(data, {
  88. // unread: true
  89. // });
  90. // }
  91. export function constraintContact(data) {
  92. constraintContactBasic(data);
  93. // constraintObject(data, {
  94. // unread: true,
  95. // lastSendTime: true,
  96. // lastContent: true
  97. // });
  98. }
  99. export function constraintMessage(data) {
  100. constraintObject(data, {
  101. status: {
  102. required: true,
  103. has: MESSAGE_STATUS
  104. },
  105. fromContactId: true
  106. });
  107. constraintMessageBasic(data);
  108. constraintContactBasic(data.fromUser);
  109. let options = {};
  110. switch (data.type) {
  111. case "file":
  112. options = {
  113. fileSize: true,
  114. fileName: true
  115. };
  116. break;
  117. case "text":
  118. options = {
  119. text: true
  120. };
  121. break;
  122. }
  123. constraintObject(data, options);
  124. }