Migrate url tooling to twenty-shared (#10440)

Migrate and unify URL tooling in twenty-shared.

We now have:
- isValidHostname which follows our own business rules
- a zod schema that can be re-used in different context and leverages is
isValidHostname
- isValidUrl on top of the zod schema
- a getAbsoluteURl and getHostname on top of the zod schema

I have added a LOT of tests to cover all the cases I've found

Also fixes: https://github.com/twentyhq/twenty/issues/10147
This commit is contained in:
Charles Bochet
2025-02-24 18:01:51 +01:00
committed by GitHub
parent d4bdae562f
commit 9046a9ac16
28 changed files with 280 additions and 158 deletions

View File

@ -1,33 +0,0 @@
import { absoluteUrlSchema } from '~/utils/validation-schemas/absoluteUrlSchema';
describe('absoluteUrlSchema', () => {
it('validates an absolute url', () => {
expect(absoluteUrlSchema.parse('https://www.example.com')).toBe(
'https://www.example.com',
);
expect(absoluteUrlSchema.parse('http://subdomain.example.com')).toBe(
'http://subdomain.example.com',
);
expect(absoluteUrlSchema.parse('https://www.example.com/path')).toBe(
'https://www.example.com/path',
);
expect(absoluteUrlSchema.parse('https://www.example.com?query=123')).toBe(
'https://www.example.com?query=123',
);
expect(absoluteUrlSchema.parse('http://localhost:3000')).toBe(
'http://localhost:3000',
);
});
it('transforms a non-absolute URL to an absolute URL', () => {
expect(absoluteUrlSchema.parse('example.com')).toBe('https://example.com');
expect(absoluteUrlSchema.parse('www.subdomain.example.com')).toBe(
'https://www.subdomain.example.com',
);
});
it('fails for invalid urls', () => {
expect(absoluteUrlSchema.safeParse('?o').success).toBe(false);
expect(absoluteUrlSchema.safeParse('\\').success).toBe(false);
});
});

View File

@ -1,23 +0,0 @@
import { z } from 'zod';
export const absoluteUrlSchema = z
.string()
.url()
.or(
z
.string()
.transform((value) => {
try {
const url = `https://${value}`.trim();
return isNaN(Number(value.trim())) &&
new URL(url) &&
/\.[a-z]{2,}$/.test(url)
? url
: '';
} catch {
return '';
}
})
.pipe(z.string().url()),
)
.or(z.literal(''));