Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.48",
"version": "0.0.49",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
formatNumber,
} from './helpers';

import { toURL, isSameHost } from './url';
import { toURL, isSameHost, isValidDomain } from './url';

import { getRecipients } from './email';

Expand Down Expand Up @@ -62,6 +62,7 @@ export {
splitName,
toURL,
isSameHost,
isValidDomain,
trimContent,
downloadFile,
getFileInfo,
Expand Down
15 changes: 15 additions & 0 deletions src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ export const isSameHost = (
return false;
}
};

/**
* Check if a string is a valid domain name.
* An empty string is allowed and considered valid.
*
* @param domain Domain to validate.
* @returns Whether the domain matches the rules.
*/
export const isValidDomain = (domain: string): boolean => {
if (domain === '') return true;

const domainRegex = /^(?!-)(?!.*--)[\p{L}0-9-]{1,63}(?<!-)(?:\.(?!-)(?!.*--)[\p{L}0-9-]{1,63}(?<!-))*\.[\p{L}]{2,63}$/u;

return domainRegex.test(domain) && domain.length <= 253;
};
45 changes: 44 additions & 1 deletion test/url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toURL, isSameHost } from '../src';
import { toURL, isSameHost, isValidDomain } from '../src';

describe('toURL', () => {
it('returns null for falsy inputs', () => {
Expand Down Expand Up @@ -80,3 +80,46 @@ describe('isSameHost', () => {
expect(isSameHost(url1, url3)).toBe(false);
});
});

describe('#isValidDomain', () => {
it('should return true for valid domains', () => {
expect(isValidDomain('')).toBe(true);
expect(isValidDomain('google.com')).toBe(true);
expect(isValidDomain('example.org')).toBe(true);
expect(isValidDomain('test.net')).toBe(true);
expect(isValidDomain('www.google.com')).toBe(true);
expect(isValidDomain('api.example.org')).toBe(true);
expect(isValidDomain('deep.nested.subdomain.com')).toBe(true);
expect(isValidDomain('my-site.com')).toBe(true);
expect(isValidDomain('test-domain.co.uk')).toBe(true);
expect(isValidDomain('123domain.org')).toBe(true);
expect(isValidDomain('测试.网络')).toBe(true);
expect(isValidDomain('пример.рф')).toBe(true);
expect(isValidDomain('example.co.uk')).toBe(true);
});

it('should return false for invalid domains', () => {
const longDomain = 'a'.repeat(250) + '.com';
expect(isValidDomain('localhost')).toBe(false);
expect(isValidDomain('example.com!')).toBe(false);
expect(isValidDomain('test@domain.com')).toBe(false);
expect(isValidDomain('domain with spaces.com')).toBe(false);
expect(isValidDomain('test_domain.com')).toBe(false);
expect(isValidDomain('example..com')).toBe(false);
expect(isValidDomain('..example.com')).toBe(false);
expect(isValidDomain('example.com..')).toBe(false);
expect(isValidDomain('-example-.com')).toBe(false);
expect(isValidDomain(longDomain)).toBe(false);
expect(isValidDomain('192.168.1.1')).toBe(false);
expect(isValidDomain('127.0.0.1')).toBe(false);
expect(isValidDomain('example.com/path/to/page')).toBe(false);
expect(isValidDomain('http://example.com')).toBe(false);
expect(isValidDomain('https://example.com')).toBe(false);
expect(isValidDomain((null as unknown) as string)).toBe(false);
expect(isValidDomain((undefined as unknown) as string)).toBe(false);
expect(isValidDomain((123 as unknown) as string)).toBe(false);
expect(isValidDomain(({} as unknown) as string)).toBe(false);
expect(isValidDomain(([] as unknown) as string)).toBe(false);
expect(isValidDomain(' ')).toBe(false);
});
});
Loading