sentryPropTypeValidators.tsx 8.9 KB

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