More socials

This commit is contained in:
Ahmed Al-Taiar
2024-09-21 15:29:14 -04:00
parent 43be1abf96
commit 430a2da835
8 changed files with 176 additions and 16 deletions

View File

@ -0,0 +1,10 @@
-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.
ALTER TYPE "Handle" ADD VALUE 'gitea';
ALTER TYPE "Handle" ADD VALUE 'leetcode';

View File

@ -0,0 +1,15 @@
-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.
ALTER TYPE "Handle" ADD VALUE 'steam';
ALTER TYPE "Handle" ADD VALUE 'discord';
ALTER TYPE "Handle" ADD VALUE 'twitch';
ALTER TYPE "Handle" ADD VALUE 'forgejo';
ALTER TYPE "Handle" ADD VALUE 'gitlab';
ALTER TYPE "Handle" ADD VALUE 'bitbucket';
ALTER TYPE "Handle" ADD VALUE 'phone';

View File

@ -21,9 +21,18 @@ enum Handle {
facebook
tiktok
youtube
steam
discord
twitch
linkedin
github
gitea
forgejo
gitlab
bitbucket
leetcode
email
phone
custom
}

View File

@ -13,9 +13,18 @@ export const schema = gql`
facebook
tiktok
youtube
steam
discord
twitch
linkedin
github
gitea
forgejo
gitlab
bitbucket
leetcode
email
phone
custom
}

View File

@ -3,6 +3,7 @@ import type {
MutationResolvers,
CreateSocialInput,
UpdateSocialInput,
Handle,
} from 'types/graphql'
import { ValidationError } from '@redwoodjs/graphql-server'
@ -12,6 +13,8 @@ import { db } from 'src/lib/db'
const urlRegex =
/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i
const phoneRegex = /^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/
const emailRegex =
/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-]+)(\.[a-zA-Z]{2,5}){1,2}$/
@ -50,8 +53,12 @@ const validateInput = (input: CreateSocialInput | UpdateSocialInput) => {
throw new ValidationError('Name is required')
if (!input.type) throw new ValidationError('Type is required')
if (input.type === 'custom' && !urlRegex.test(input.username))
const urlHandles: Handle[] = ['custom', 'gitea', 'forgejo']
if (urlHandles.includes(input.type) && !urlRegex.test(input.username))
throw new ValidationError('Invalid URL')
else if (input.type === 'phone' && !phoneRegex.test(input.username))
throw new ValidationError('Invalid Phone Number')
else if (input.type === 'email' && !emailRegex.test(input.username))
throw new ValidationError('Invalid Email')
else if (input.username.trim().length === 0)