-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from unstoppabledomains/refat/add_domain_vali…
…dation Added validation for domain name and test
- Loading branch information
Showing
7 changed files
with
123 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {expectResolutionErrorCode} from './helpers'; | ||
import {prepareAndValidateDomain} from '../utils/prepareAndValidate'; | ||
import {ResolutionErrorCode} from '../errors/resolutionError'; | ||
|
||
it('should throw exception for invalid domains', async () => { | ||
await expectResolutionErrorCode( | ||
() => prepareAndValidateDomain('#[email protected]'), | ||
ResolutionErrorCode.InvalidDomainAddress, | ||
); | ||
await expectResolutionErrorCode( | ||
() => prepareAndValidateDomain('hello123#.blockchain'), | ||
ResolutionErrorCode.InvalidDomainAddress, | ||
); | ||
await expectResolutionErrorCode( | ||
() => prepareAndValidateDomain('hello#blockchain'), | ||
ResolutionErrorCode.InvalidDomainAddress, | ||
); | ||
await expectResolutionErrorCode( | ||
() => prepareAndValidateDomain('helloblockchain#'), | ||
ResolutionErrorCode.InvalidDomainAddress, | ||
); | ||
}); | ||
|
||
it('should convert domain name to lower case', async () => { | ||
expect(prepareAndValidateDomain(' HELLO.Blockchain ')).toEqual( | ||
'hello.blockchain', | ||
); | ||
expect(prepareAndValidateDomain(' HELLO123.Blockchain ')).toEqual( | ||
'hello123.blockchain', | ||
); | ||
expect(prepareAndValidateDomain(' HELLO1.Blockchain1 ')).toEqual( | ||
'hello1.blockchain1', | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import ResolutionError, {ResolutionErrorCode} from '../errors/resolutionError'; | ||
|
||
/** | ||
* Checks domain name for special symbols and returns address in lowercase without spaces | ||
* @throws Will throw an error if domain address contains special symbols | ||
* @param domain - a domain address | ||
*/ | ||
const reg = RegExp('^[.a-z0-9-]+$'); | ||
export function prepareAndValidateDomain(domain: string): string { | ||
const retVal: string = domain ? domain.trim().toLowerCase() : ''; | ||
if (!reg.test(retVal)) { | ||
throw new ResolutionError(ResolutionErrorCode.InvalidDomainAddress, { | ||
domain, | ||
}); | ||
} | ||
return retVal; | ||
} |