validators.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // to use with <input type="url"> fields, as the default pattern only checks for `:`,
  2. export const DEFAULT_TEXTFIELD_URL_PATTERN = 'https?://.*';
  3. /**
  4. * Determines if a URL is valid
  5. * @param {string} url - A URL to validate.
  6. * @param {string[]} validProtocols - An array of valid protocols. Defaults to web.
  7. * @returns {boolean} - True if the URI is valid, false otherwise.
  8. */
  9. export function isValidUrl(url: string, validProtocols: string[] = ['http:', 'https:']): boolean {
  10. try {
  11. const validationObject = new URL(url);
  12. if (
  13. validationObject.protocol === '' ||
  14. validationObject.hostname === '' ||
  15. !validProtocols.includes(validationObject.protocol)
  16. ) {
  17. return false;
  18. }
  19. } catch (e) {
  20. return false;
  21. }
  22. return true;
  23. }
  24. /**
  25. * Determines if an account is valid by checking for a protocol, username
  26. * and server, delimited by a colon. For example: @username:example.com
  27. * @param {string} account - An account to validate.
  28. * @param {string} protocol - The protocol we expect the account to be using.
  29. * @returns {boolean} - True if the account is valid, false otherwise.
  30. */
  31. export function isValidAccount(account: string, protocol: string): boolean {
  32. if (account.startsWith('@')) {
  33. // eslint-disable-next-line no-param-reassign
  34. account = account.slice(1);
  35. }
  36. const components = account.split(/:|@/);
  37. const [service, user, host] = components;
  38. if (service !== protocol) {
  39. return false;
  40. }
  41. if (components.length !== 3 || !service || !user || !host) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. /**
  47. * Determines if an account is valid by simply checking for a protocol, username
  48. * and server, delimited by a colon. For example: @username:example.com
  49. * @param {string} account - An account to validate. Example: @me:matrix.org
  50. * @returns {boolean} - True if the account is valid, false otherwise.
  51. */
  52. export function isValidMatrixAccount(account: string): boolean {
  53. if (account.startsWith('matrix:')) {
  54. // eslint-disable-next-line no-param-reassign
  55. account = account.slice(7);
  56. }
  57. if (account.startsWith('@')) {
  58. // eslint-disable-next-line no-param-reassign
  59. account = account.slice(1);
  60. }
  61. const components = account.split(':');
  62. const [user, host] = components;
  63. if (components.length !== 2 || !user || !host) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. /**
  69. * Determines if a fediverse account is valid.
  70. * For example: @username@example.com
  71. * @param {string} account - An account to validate.
  72. * @returns {boolean} - True if the account is valid, false otherwise.
  73. */
  74. export function isValidFediverseAccount(account: string): boolean {
  75. const sanitized = account.replace(/^@+/, '');
  76. const regex =
  77. /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  78. return regex.test(String(sanitized).toLowerCase());
  79. }