As ICANN has introduced new generic Top Level Domains into the DNS root, problems have been experienced with end users receiving errors from applications which try to sense the validity of a domain name. Traditionally all top-level domains have either been two octets long (for country-code top level domains like .UK and .DE), or three octets long (for generic top level domains like .COM and .INT). However, with the introduction of new domains such as .INFO and .TRAVEL this premise no longer applies.
This project provides some small utilities to work with TLDs and show how that's best done now. You may also want to look at the newer ua-code-samples repo which contains a number of code samples related to IDNs, that is, to domains such as טעסט. (a test TLD using yiddish).
Finally, in certain applications - such as those without reliable connectivity to perform live DNS requests, or for those for performance reasons can not conduct such requests for each test, the possibility exists to verify against IANA'a list of TLDs. Many users have come to use Mozilla's Public Suffix List for similar purposes. Note that both lists change frequently, so you need to update your copy every month, and that the PSL contains domains such as .co.uk, not just .uk.