Skip to content

Commit

Permalink
fix: uri yup extension
Browse files Browse the repository at this point in the history
  • Loading branch information
chalabi2 committed Dec 5, 2024
1 parent 3727ec6 commit 805a6fa
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
8 changes: 5 additions & 3 deletions components/factory/components/DenomImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ProfileAvatar from '@/utils/identicon';
import Image from 'next/image';
import { useState, useEffect } from 'react';

const supportedDomains = [
export const supportedDomains = [
'imgur.com',
'i.imgur.com',
'cloudfront.net',
Expand All @@ -24,9 +24,10 @@ const supportedDomains = [
'upload.wikimedia.org',
'unsplash.com',
'istockphoto.com',
't4.ftcdn.net',
];

const supportedPatterns = [
export const supportedPatterns = [
/^https:\/\/.*\.s3\.amazonaws\.com/,
/^https:\/\/.*\.storage\.googleapis\.com/,
/^https:\/\/.*\.cloudinary\.com/,
Expand All @@ -49,9 +50,10 @@ const supportedPatterns = [
/^https:\/\/.*\.twimg\.com/,
/^https:\/\/.*\.pinimg\.com/,
/^https:\/\/.*\.giphy\.com/,
/^https:\/\/.*\.t4\.ftcdn\.net/,
];

const isUrlSupported = (url: string) => {
export const isUrlSupported = (url: string) => {
try {
const { hostname } = new URL(url);
return (
Expand Down
6 changes: 5 additions & 1 deletion components/factory/modals/updateDenomMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const TokenDetailsSchema = Yup.object().shape({
description: Yup.string()
.min(10, 'Description must be at least 10 characters long')
.noProfanity(),
uri: Yup.string().url('Must be a valid URL'),
uri: Yup.string()
.url('Must be a valid URL')
.matches(/^https:\/\//i, 'URL must use HTTPS protocol')
.matches(/\.(jpg|jpeg|png|gif)$/i, 'URL must point to an image file')
.supportedImageUrl(),
});

export function UpdateDenomMetadataModal({
Expand Down
5 changes: 5 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const nextConfig = {
'media.istockphoto.com',
'upload.wikimedia.org',
'istockphoto.com',
't4.ftcdn.net',
],
remotePatterns: [
{
Expand Down Expand Up @@ -76,6 +77,10 @@ const nextConfig = {
protocol: 'https',
hostname: '*.upload.wikimedia.org',
},
{
protocol: 'https',
hostname: '*.t4.ftcdn.net',
},
],
},
};
Expand Down
21 changes: 21 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ input:-webkit-autofill:active {
[data-toast-container='true'] {
z-index: 100000;
}

.tooltip {
position: relative;
cursor: help;
}

.tooltip[data-tooltip]:hover::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 0;
background: #333;
color: white;
padding: 8px;
border-radius: 4px;
font-size: 14px;
white-space: pre-wrap;
width: max-content;
max-width: 300px;
z-index: 1000;
}
30 changes: 30 additions & 0 deletions utils/yupExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import * as Yup from 'yup';
import { containsProfanity } from '@/utils/profanityFilter';
import { bech32 } from '@scure/base'; // Updated import
import { supportedDomains, supportedPatterns } from '@/components/factory/components/DenomImage';

declare module 'yup' {
interface StringSchema {
noProfanity(message?: string): this;
manifestAddress(message?: string): this;
simulateDenomCreation(simulateFn: () => Promise<boolean>, message?: string): this;
simulateDenomMetadata(simulateFn: () => Promise<boolean>, message?: string): this;
supportedImageUrl(message?: string): this;
}
}

Expand Down Expand Up @@ -115,4 +117,32 @@ Yup.addMethod<Yup.StringSchema>(Yup.string, 'manifestAddress', function (message
});
});

Yup.addMethod<Yup.StringSchema>(Yup.string, 'supportedImageUrl', function (message) {
return this.test('supported-image-url', message, function (value) {
const { path, createError } = this;
if (!value) return true;

try {
const { hostname } = new URL(value);
const isSupported =
supportedDomains.includes(hostname) ||
supportedPatterns.some(pattern => pattern.test(value));

return (
isSupported ||
createError({
path,
message:
message || `URL domain is not supported. Please use one of the supported domains`,
})
);
} catch {
return createError({
path,
message: message || 'Invalid URL format',

Check warning on line 142 in utils/yupExtensions.ts

View check run for this annotation

Codecov / codecov/patch

utils/yupExtensions.ts#L121-L142

Added lines #L121 - L142 were not covered by tests
});
}
});
});

export default Yup;

0 comments on commit 805a6fa

Please sign in to comment.