sentryPropTypeValidators.tsx 9.0 KB

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