sentryPropTypeValidators.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import type {
  2. Avatar,
  3. Group,
  4. Organization,
  5. Project,
  6. Team,
  7. User,
  8. UserEmail,
  9. } from 'sentry/types';
  10. /**
  11. * @deprecated
  12. */
  13. function isAvatarShape(avatar: unknown): null | Error {
  14. if (typeof avatar !== 'object' || avatar === null) {
  15. return new Error('avatar is not an object');
  16. }
  17. if (!('avatarType' in avatar) || typeof avatar.avatarType !== 'string') {
  18. return new Error(`avatarType must be string.`);
  19. }
  20. const maybeAvatarShape = avatar as Partial<Avatar>;
  21. if (
  22. maybeAvatarShape.avatarType !== 'letter_avatar' &&
  23. maybeAvatarShape.avatarType !== 'upload' &&
  24. maybeAvatarShape.avatarType !== 'gravatar'
  25. ) {
  26. return new Error(`avatarType must be one of 'letter_avatar', 'upload', 'gravatar'.`);
  27. }
  28. if (!('avatarUuid' in avatar) || typeof maybeAvatarShape.avatarUuid !== 'string') {
  29. return new Error(`avatarUuid must be string`);
  30. }
  31. return null;
  32. }
  33. /**
  34. * @deprecated
  35. */
  36. function isEmailShape(email: unknown): null | Error {
  37. if (typeof email !== 'object' || email === null) {
  38. return new Error('email is not of object type');
  39. }
  40. const maybeEmailShape = email as Partial<UserEmail>;
  41. if ('email' in maybeEmailShape && typeof maybeEmailShape.email !== 'string') {
  42. return new Error(`email must be string.`);
  43. }
  44. if ('id' in maybeEmailShape && typeof maybeEmailShape.id !== 'string') {
  45. return new Error(`id must be string.`);
  46. }
  47. if (
  48. 'is_verified' in maybeEmailShape &&
  49. typeof maybeEmailShape.is_verified !== 'boolean'
  50. ) {
  51. return new Error(`is_verified must be boolean.`);
  52. }
  53. return null;
  54. }
  55. /**
  56. * @deprecated
  57. */
  58. const USER_STRING_KEYS: (keyof User)[] = [
  59. 'avatarUrl',
  60. 'dateJoined',
  61. 'email',
  62. 'id',
  63. 'lastActive',
  64. 'lastLogin',
  65. 'username',
  66. ];
  67. const USER_BOOLEAN_KEYS: (keyof User)[] = [
  68. 'has2fa',
  69. 'hasPasswordAuth',
  70. 'isActive',
  71. 'isManaged',
  72. ];
  73. function isUserShape(user: unknown): null | Error {
  74. if (user === null) {
  75. return null;
  76. }
  77. if (typeof user !== 'object') {
  78. return new Error('user is not of object type');
  79. }
  80. const maybeUserShape = user as Partial<User>;
  81. if ('avatar' in maybeUserShape && isAvatarShape(maybeUserShape.avatar) !== null) {
  82. return new Error('user.avatar is not of type Avatar');
  83. }
  84. if (
  85. 'emails' in maybeUserShape &&
  86. Array.isArray(maybeUserShape.emails) &&
  87. !maybeUserShape.emails.every(e => isEmailShape(e) === null)
  88. ) {
  89. return null;
  90. }
  91. for (const key of USER_BOOLEAN_KEYS) {
  92. if (key in maybeUserShape && typeof maybeUserShape[key] !== 'boolean') {
  93. return new Error(`user.${key} is not of type string`);
  94. }
  95. }
  96. if ('identities' in maybeUserShape && !Array.isArray(maybeUserShape.identities)) {
  97. return new Error('user.id identities not of type array');
  98. }
  99. for (const key of USER_STRING_KEYS) {
  100. if (key in user && typeof user[key] !== 'string') {
  101. return new Error(`user.${key} is not of type string`);
  102. }
  103. }
  104. return null;
  105. }
  106. /**
  107. * @deprecated
  108. */
  109. function isPartialProjectShape(project: unknown): null | Error {
  110. if (typeof project !== 'object' || project === null) {
  111. return new Error('project is not of object type');
  112. }
  113. for (const key of ['name', 'slug']) {
  114. if (key in project && typeof project[key] !== 'string') {
  115. return new Error(`${key} must be string.`);
  116. }
  117. }
  118. return null;
  119. }
  120. const METADATA_STRING_KEYS = ['value', 'message', 'directive', 'type', 'title', 'uri'];
  121. /**
  122. * @deprecated
  123. */
  124. function isMetaDataShape(metaData: unknown): null | Error {
  125. if (typeof metaData !== 'object' || metaData === null) {
  126. return new Error('metaData is not of object type');
  127. }
  128. for (const key of METADATA_STRING_KEYS) {
  129. if (key in metaData && typeof metaData[key] !== 'string') {
  130. return new Error(`value must be string.`);
  131. }
  132. }
  133. return null;
  134. }
  135. /**
  136. * @deprecated
  137. */
  138. /**
  139. * @deprecated
  140. */
  141. const GROUP_NUMBER_KEYS: (keyof Group)[] = ['userCount', 'numComments'];
  142. const GROUP_BOOLEAN_KEYS: (keyof Group)[] = [
  143. 'hasSeen',
  144. 'isBookmarked',
  145. 'isPublic',
  146. 'isSubscribed',
  147. ];
  148. const GROUP_STRING_KEYS: (keyof Group)[] = [
  149. 'lastSeen',
  150. 'count',
  151. 'culprit',
  152. 'firstSeen',
  153. 'level',
  154. 'permalink',
  155. 'shareId',
  156. 'shortId',
  157. 'status',
  158. 'title',
  159. ];
  160. /**
  161. * @deprecated
  162. */
  163. function isGroup(
  164. props: unknown,
  165. propName: string,
  166. _componentName: unknown
  167. ): null | Error {
  168. if (typeof props !== 'object' || props === null) {
  169. return new Error('props is not an object');
  170. }
  171. if (!(propName in props) || typeof props[propName] !== 'object') {
  172. return null;
  173. }
  174. if (!props[propName]) {
  175. return null;
  176. }
  177. const group = props[propName];
  178. if (!('id' in group) || typeof group.id !== 'string') {
  179. return new Error(`id must be string.`);
  180. }
  181. for (const key of GROUP_NUMBER_KEYS) {
  182. if (key in group && typeof group[key] !== 'number') {
  183. return new Error(`${key} must be number.`);
  184. }
  185. }
  186. for (const key of GROUP_BOOLEAN_KEYS) {
  187. if (key in group && typeof group[key] !== 'boolean') {
  188. return new Error(`${key} must be boolean.`);
  189. }
  190. }
  191. if ('logger' in group) {
  192. if (typeof group.logger !== 'string' && group.logger !== null) {
  193. return new Error(`logger must be of string or null type.`);
  194. }
  195. }
  196. for (const key of GROUP_STRING_KEYS) {
  197. if (key in group && typeof group[key] !== 'string') {
  198. return new Error(`${key} must be string. got ${group[key]}`);
  199. }
  200. }
  201. if ('type' in group) {
  202. if (typeof group.type !== 'string') {
  203. return new Error(`type must be string.`);
  204. }
  205. if (
  206. group.type !== 'error' &&
  207. group.type !== 'csp' &&
  208. group.type !== 'hpkp' &&
  209. group.type !== 'expectct' &&
  210. group.type !== 'expectstaple' &&
  211. group.type !== 'default' &&
  212. group.type !== 'transaction'
  213. ) {
  214. return new Error(
  215. `type must be one of 'error', 'csp', 'hpkp', 'expectct', 'expectstaple', 'default', 'transaction'.`
  216. );
  217. }
  218. }
  219. if ('statusDetails' in group && typeof group.statusDetails !== 'object') {
  220. return new Error(`statusDetails must be object.`);
  221. }
  222. if ('annotations' in group && !Array.isArray(group.annotations)) {
  223. return new Error(`annotations must be of array type.`);
  224. }
  225. if ('assignedTo' in group && isUserShape(group.assignedTo) !== null) {
  226. return new Error(`assignedTo must be of type User.`);
  227. }
  228. if ('metadata' in group && isMetaDataShape(group.metadata) !== null) {
  229. return new Error(`metadata must be of type MetaData.`);
  230. }
  231. if ('project' in group && isPartialProjectShape(group.project) !== null) {
  232. return new Error(`project must be of type PartialProject.`);
  233. }
  234. return null;
  235. }
  236. /**
  237. * @deprecated
  238. */
  239. function isTeamShape(team: unknown) {
  240. if (typeof team !== 'object' || team === null) {
  241. return new Error('Team has to be of object type');
  242. }
  243. const maybeTeamShape = team as Partial<Team>;
  244. if (!('id' in maybeTeamShape) || typeof maybeTeamShape.id !== 'string') {
  245. return new Error(`id must be string.`);
  246. }
  247. if (!('slug' in maybeTeamShape) || typeof maybeTeamShape.slug !== 'string') {
  248. return new Error(`slug must be string.`);
  249. }
  250. return null;
  251. }
  252. /**
  253. * @deprecated
  254. */
  255. function isProject(
  256. props: unknown,
  257. propName: string,
  258. _componentName: unknown
  259. ): null | Error {
  260. if (typeof props !== 'object' || props === null) {
  261. return new Error('props is not an object');
  262. }
  263. if (!(propName in props) || typeof props[propName] !== 'object') {
  264. return null;
  265. }
  266. if (!props[propName]) {
  267. return null;
  268. }
  269. const project = props[propName] as Partial<Project>;
  270. if (
  271. !('id' in project) ||
  272. ('id' in project && typeof project.id !== 'string' && typeof project.id !== 'number')
  273. ) {
  274. return new Error(`id must be string or number.`);
  275. }
  276. if ('slug' in project && typeof project.slug !== 'string') {
  277. return new Error('slug must be string');
  278. }
  279. if ('isBookmarked' in project && typeof project.isBookmarked !== 'boolean') {
  280. return new Error('isBookmarked must be boolean');
  281. }
  282. if ('status' in project && typeof project.status !== 'string') {
  283. return new Error('status must be string');
  284. }
  285. if ('teams' in project) {
  286. if (!Array.isArray(project.teams)) {
  287. return new Error('teams must be array');
  288. }
  289. if (!project.teams.every(t => isTeamShape(t) === null)) {
  290. return new Error(`teams must be array of Team types.`);
  291. }
  292. }
  293. if ('name' in project && typeof project.name !== 'string') {
  294. return new Error('name must be string');
  295. }
  296. return null;
  297. }
  298. /**
  299. * @deprecated
  300. */
  301. function isOrganization(
  302. props: unknown,
  303. propName: string,
  304. _componentName: unknown
  305. ): null | Error {
  306. if (typeof props !== 'object' || props === null) {
  307. return new Error('props does not contain organization property');
  308. }
  309. if (!(propName in props)) {
  310. return null;
  311. }
  312. if (!props[propName]) {
  313. return null;
  314. }
  315. const organization = props[propName] as Partial<Organization>;
  316. if (
  317. !('id' in organization) ||
  318. ('id' in organization && typeof organization.id !== 'string')
  319. ) {
  320. return new Error(`id must be string.`);
  321. }
  322. return null;
  323. }
  324. /**
  325. * @deprecated
  326. */
  327. function isObject(
  328. props: unknown,
  329. propName: string,
  330. _componentName: unknown
  331. ): null | Error {
  332. if (typeof props !== 'object' || props === null) {
  333. return new Error('props does not contain organization property');
  334. }
  335. if (!(propName in props)) {
  336. return null;
  337. }
  338. if (!props[propName]) {
  339. return null;
  340. }
  341. if (typeof props[propName] !== 'object') {
  342. throw new Error(`props.${propName} is not of type object`);
  343. }
  344. return null;
  345. }
  346. /**
  347. * @deprecated
  348. */
  349. export const SentryPropTypeValidators = {
  350. isGroup,
  351. isProject,
  352. isOrganization,
  353. isObject,
  354. };