Export new url validation functions

This commit is contained in:
Gabe Kangas 2022-12-14 11:52:11 -08:00
parent 3abc7a3ab8
commit 11c4d80e28
No known key found for this signature in database
GPG key ID: 4345B2060657F330
6 changed files with 71 additions and 9 deletions

View file

@ -20,7 +20,7 @@ import {
} from '../../utils/input-statuses';
import { TextField } from './TextField';
import { FormStatusIndicator } from './FormStatusIndicator';
import isValidUrl from '../../utils/urls';
import { isValidUrl } from '../../utils/urls';
import { ToggleSwitch } from './ToggleSwitch';
const { Panel } = Collapse;

View file

@ -1,7 +1,7 @@
import { CheckCircleOutlined } from '@ant-design/icons';
import { Alert, Input, Space, Spin, Collapse, Typography, Button } from 'antd';
import React, { FC, useState } from 'react';
import isValidURL from '../../../utils/urls';
import { isValidUrl } from '../../../utils/urls';
const { Panel } = Collapse;
const { Link } = Typography;
@ -42,7 +42,7 @@ export const IndieAuthModal: FC<IndieAuthModalProps> = ({
}
const validate = (url: string) => {
if (!isValidURL(url)) {
if (!isValidUrl(url)) {
setValid(false);
return;
}

View file

@ -11,7 +11,7 @@ import {
} from '../../utils/config-constants';
import { createInputStatus, STATUS_ERROR, STATUS_SUCCESS } from '../../utils/input-statuses';
import { ServerStatusContext } from '../../utils/server-status-context';
import isValidUrl, { DEFAULT_TEXTFIELD_URL_PATTERN } from '../../utils/urls';
import { isValidUrl, DEFAULT_TEXTFIELD_URL_PATTERN } from '../../utils/urls';
const { Title, Paragraph } = Typography;
let resetTimer = null;

View file

@ -13,7 +13,7 @@ import {
import { TEXTFIELD_PROPS_FEDERATION_INSTANCE_URL } from '../../utils/config-constants';
import { ServerStatusContext } from '../../utils/server-status-context';
import { UpdateArgs } from '../../types/config-section';
import isValidUrl from '../../utils/urls';
import { isValidUrl } from '../../utils/urls';
const { Title } = Typography;

View file

@ -15,7 +15,7 @@ import {
} from 'antd';
import React, { useEffect, useState } from 'react';
import { CREATE_WEBHOOK, DELETE_WEBHOOK, fetchData, WEBHOOKS } from '../../utils/apis';
import isValidUrl, { DEFAULT_TEXTFIELD_URL_PATTERN } from '../../utils/urls';
import { isValidUrl, DEFAULT_TEXTFIELD_URL_PATTERN } from '../../utils/urls';
const { Title, Paragraph } = Typography;

View file

@ -1,9 +1,14 @@
// to use with <input type="url"> fields, as the default pattern only checks for `:`,
export const DEFAULT_TEXTFIELD_URL_PATTERN = 'https?://.*';
export default function isValidUrl(url: string): boolean {
const validProtocols = ['http:', 'https:'];
/**
* Determines if a URL is valid
* @param {string} url - A URL to validate.
* @param {string[]} validProtocols - An array of valid protocols. Defaults to web.
* @returns {boolean} - True if the URI is valid, false otherwise.
*/
export function isValidUrl(url: string, validProtocols: string[] = ['http', 'https']): boolean {
console.log(url, validProtocols);
try {
const validationObject = new URL(url);
if (
@ -19,3 +24,60 @@ export default function isValidUrl(url: string): boolean {
return true;
}
/**
* Determines if an account is valid by simply checking for a protocol, username
* and server, delimited by a colon. For example: @username:example.com
* @param {string} account - An account to validate.
* @param {string} protocol - The protocol we expect the account to be using.
* @returns {boolean} - True if the account is valid, false otherwise.
*/
export function isValidAccount(account: string, protocol: string): boolean {
if (account.startsWith('@')) {
// eslint-disable-next-line no-param-reassign
account = account.slice(1);
}
const components = account.split(/:|@/);
const [service, user, host] = components;
console.log({ account, protocol, service, user, host });
if (service !== protocol) {
return false;
}
if (components.length !== 3 || !service || !user || !host) {
return false;
}
return true;
}
/**
* Determines if an account is valid by simply checking for a protocol, username
* and server, delimited by a colon. For example: @username:example.com
* @param {string} account - An account to validate.
* @returns {boolean} - True if the account is valid, false otherwise.
*/
export function isValidMatrixAccount(account: string): boolean {
if (account.startsWith('matrix:')) {
// eslint-disable-next-line no-param-reassign
account = account.slice(7);
} else {
return false;
}
if (account.startsWith('@')) {
// eslint-disable-next-line no-param-reassign
account = account.slice(1);
}
const components = account.split(':');
const [user, host] = components;
if (components.length !== 2 || !user || !host) {
return false;
}
return true;
}