sentryPropTypeValidators.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import type {Avatar} from 'sentry/types/core';
  2. import type {Group} from 'sentry/types/group';
  3. import type {User, UserEmail} from 'sentry/types/user';
  4. /**
  5. * @deprecated
  6. */
  7. function isAvatarShape(avatar: unknown): null | Error {
  8. if (typeof avatar !== 'object' || avatar === null) {
  9. return new Error('avatar is not an object');
  10. }
  11. if (!('avatarType' in avatar) || typeof avatar.avatarType !== 'string') {
  12. return new Error(`avatarType must be string.`);
  13. }
  14. const maybeAvatarShape = avatar as Partial<Avatar>;
  15. if (
  16. maybeAvatarShape.avatarType !== 'letter_avatar' &&
  17. maybeAvatarShape.avatarType !== 'upload' &&
  18. maybeAvatarShape.avatarType !== 'gravatar'
  19. ) {
  20. return new Error(`avatarType must be one of 'letter_avatar', 'upload', 'gravatar'.`);
  21. }
  22. if (!('avatarUuid' in avatar) || typeof maybeAvatarShape.avatarUuid !== 'string') {
  23. return new Error(`avatarUuid must be string`);
  24. }
  25. return null;
  26. }
  27. /**
  28. * @deprecated
  29. */
  30. function isEmailShape(email: unknown): null | Error {
  31. if (typeof email !== 'object' || email === null) {
  32. return new Error('email is not of object type');
  33. }
  34. const maybeEmailShape = email as Partial<UserEmail>;
  35. if ('email' in maybeEmailShape && typeof maybeEmailShape.email !== 'string') {
  36. return new Error(`email must be string.`);
  37. }
  38. if ('id' in maybeEmailShape && typeof maybeEmailShape.id !== 'string') {
  39. return new Error(`id must be string.`);
  40. }
  41. if (
  42. 'is_verified' in maybeEmailShape &&
  43. typeof maybeEmailShape.is_verified !== 'boolean'
  44. ) {
  45. return new Error(`is_verified must be boolean.`);
  46. }
  47. return null;
  48. }
  49. /**
  50. * @deprecated
  51. */
  52. const USER_STRING_KEYS: (keyof User)[] = [
  53. 'avatarUrl',
  54. 'dateJoined',
  55. 'email',
  56. 'id',
  57. 'lastActive',
  58. 'lastLogin',
  59. 'username',
  60. ];
  61. const USER_BOOLEAN_KEYS: (keyof User)[] = [
  62. 'has2fa',
  63. 'hasPasswordAuth',
  64. 'isActive',
  65. 'isManaged',
  66. ];
  67. function isUserShape(user: unknown): null | Error {
  68. if (user === null) {
  69. return null;
  70. }
  71. if (typeof user !== 'object') {
  72. return new Error('user is not of object type');
  73. }
  74. const maybeUserShape = user as Partial<User>;
  75. if ('avatar' in maybeUserShape && isAvatarShape(maybeUserShape.avatar) !== null) {
  76. return new Error('user.avatar is not of type Avatar');
  77. }
  78. if (
  79. 'emails' in maybeUserShape &&
  80. Array.isArray(maybeUserShape.emails) &&
  81. !maybeUserShape.emails.every(e => isEmailShape(e) === null)
  82. ) {
  83. return null;
  84. }
  85. for (const key of USER_BOOLEAN_KEYS) {
  86. if (key in maybeUserShape && typeof maybeUserShape[key] !== 'boolean') {
  87. return new Error(`user.${key} is not of type string`);
  88. }
  89. }
  90. if ('identities' in maybeUserShape && !Array.isArray(maybeUserShape.identities)) {
  91. return new Error('user.id identities not of type array');
  92. }
  93. for (const key of USER_STRING_KEYS) {
  94. if (key in user && typeof user[key] !== 'string') {
  95. return new Error(`user.${key} is not of type string`);
  96. }
  97. }
  98. return null;
  99. }
  100. /**
  101. * @deprecated
  102. */
  103. function isPartialProjectShape(project: unknown): null | Error {
  104. if (typeof project !== 'object' || project === null) {
  105. return new Error('project is not of object type');
  106. }
  107. for (const key of ['name', 'slug']) {
  108. if (key in project && typeof project[key] !== 'string') {
  109. return new Error(`${key} must be string.`);
  110. }
  111. }
  112. return null;
  113. }
  114. const METADATA_STRING_KEYS = ['value', 'message', 'directive', 'type', 'title', 'uri'];
  115. /**
  116. * @deprecated
  117. */
  118. function isMetaDataShape(metaData: unknown): null | Error {
  119. if (typeof metaData !== 'object' || metaData === null) {
  120. return new Error('metaData is not of object type');
  121. }
  122. for (const key of METADATA_STRING_KEYS) {
  123. if (key in metaData && typeof metaData[key] !== 'string') {
  124. return new Error(`value must be string.`);
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * @deprecated
  131. */
  132. /**
  133. * @deprecated
  134. */
  135. const GROUP_NUMBER_KEYS: (keyof Group)[] = ['userCount', 'numComments'];
  136. const GROUP_BOOLEAN_KEYS: (keyof Group)[] = [
  137. 'hasSeen',
  138. 'isBookmarked',
  139. 'isPublic',
  140. 'isSubscribed',
  141. ];
  142. const GROUP_STRING_KEYS: (keyof Group)[] = [
  143. 'lastSeen',
  144. 'count',
  145. 'culprit',
  146. 'firstSeen',
  147. 'level',
  148. 'permalink',
  149. 'shareId',
  150. 'shortId',
  151. 'status',
  152. 'title',
  153. ];
  154. /**
  155. * @deprecated
  156. */
  157. function isGroup(
  158. props: unknown,
  159. propName: string,
  160. _componentName: unknown
  161. ): null | Error {
  162. if (typeof props !== 'object' || props === null) {
  163. return new Error('props is not an object');
  164. }
  165. if (!(propName in props) || typeof props[propName] !== 'object') {
  166. return null;
  167. }
  168. if (!props[propName]) {
  169. return null;
  170. }
  171. const group = props[propName];
  172. if (!('id' in group) || typeof group.id !== 'string') {
  173. return new Error(`id must be string.`);
  174. }
  175. for (const key of GROUP_NUMBER_KEYS) {
  176. if (key in group && typeof group[key] !== 'number') {
  177. return new Error(`${key} must be number.`);
  178. }
  179. }
  180. for (const key of GROUP_BOOLEAN_KEYS) {
  181. if (key in group && typeof group[key] !== 'boolean') {
  182. return new Error(`${key} must be boolean.`);
  183. }
  184. }
  185. if ('logger' in group) {
  186. if (typeof group.logger !== 'string' && group.logger !== null) {
  187. return new Error(`logger must be of string or null type.`);
  188. }
  189. }
  190. for (const key of GROUP_STRING_KEYS) {
  191. if (key in group && typeof group[key] !== 'string') {
  192. return new Error(`${key} must be string. got ${group[key]}`);
  193. }
  194. }
  195. if ('type' in group) {
  196. if (typeof group.type !== 'string') {
  197. return new Error(`type must be string.`);
  198. }
  199. if (
  200. group.type !== 'error' &&
  201. group.type !== 'csp' &&
  202. group.type !== 'hpkp' &&
  203. group.type !== 'expectct' &&
  204. group.type !== 'expectstaple' &&
  205. group.type !== 'default' &&
  206. group.type !== 'transaction'
  207. ) {
  208. return new Error(
  209. `type must be one of 'error', 'csp', 'hpkp', 'expectct', 'expectstaple', 'default', 'transaction'.`
  210. );
  211. }
  212. }
  213. if ('statusDetails' in group && typeof group.statusDetails !== 'object') {
  214. return new Error(`statusDetails must be object.`);
  215. }
  216. if ('annotations' in group && !Array.isArray(group.annotations)) {
  217. return new Error(`annotations must be of array type.`);
  218. }
  219. if ('assignedTo' in group && isUserShape(group.assignedTo) !== null) {
  220. return new Error(`assignedTo must be of type User.`);
  221. }
  222. if ('metadata' in group && isMetaDataShape(group.metadata) !== null) {
  223. return new Error(`metadata must be of type MetaData.`);
  224. }
  225. if ('project' in group && isPartialProjectShape(group.project) !== null) {
  226. return new Error(`project must be of type PartialProject.`);
  227. }
  228. return null;
  229. }
  230. /**
  231. * @deprecated
  232. */
  233. function isObject(
  234. props: unknown,
  235. propName: string,
  236. _componentName: unknown
  237. ): null | Error {
  238. if (typeof props !== 'object' || props === null) {
  239. return new Error('props does not contain organization property');
  240. }
  241. if (!(propName in props)) {
  242. return null;
  243. }
  244. if (!props[propName]) {
  245. return null;
  246. }
  247. if (typeof props[propName] !== 'object') {
  248. throw new Error(`props.${propName} is not of type object`);
  249. }
  250. return null;
  251. }
  252. /**
  253. * @deprecated
  254. */
  255. export const SentryPropTypeValidators = {
  256. isGroup,
  257. isObject,
  258. };