Repository: hapijs/joi (JavaScript)

Issue:
Domain validation not checking IANA registry by default
### Runtime

both node.js and browser

### Runtime version

node 18.16.1

### Module version

17.11.0

### Last module version without issue

_No response_

### Used with

standalone

### Any other relevant information

_No response_

### What are you trying to achieve or the steps to reproduce?

When using `Joi.string().domain()` schema, I expected it to do TLD domain validation according to IANA registry by default, as [this is implied by the docs](https://joi.dev/api/?v=17.9.1#stringdomainoptions):

> `tlds` - options for TLD (top level domain) validation. By default, the TLD must be a valid name listed on the [IANA registry](http://data.iana.org/TLD/tlds-alpha-by-domain.txt). To disable validation, set tlds to false.

And also:
> * `allow` - one of:
>   * `true` to use the IANA list of registered TLDs. This is the default value.

I think that it is either a bug when using the domain validation or an issue with the docs. The reason I think it could be a bug is because I tried to debug it a bit to see where it was failing and I saw that the `options` object is `undefined` [here](https://github.com/hapijs/address/blob/master/src/domain.ts#L138). Which might mean that Joi is not setting the expected default options.

It works as expected if you explicitly set the options, as in `Joi.string().domain({ tlds: { allow: true } })`

### What was the result you got?

An invalid TLD passed validation when using `Joi.string().domain()` schema.
Example:
`joi.dev.whatevertldiwant`

### What result did you expect?

I expected the validation to not pass, as the TLD does not exist in the IANA registry


Discussion:
I can work on that!



Patch:
diff --git a/lib/types/string.js b/lib/types/string.js
index d472e610..57b3d34b 100755
--- a/lib/types/string.js
+++ b/lib/types/string.js
@@ -742,7 +742,7 @@ module.exports = Any.extend({
 internals.addressOptions = function (options) {
 
     if (!options) {
-        return options;
+        return internals.tlds || options;      // $lab:coverage:ignore$
     }
 
     // minDomainSegments


Test patch:
diff --git a/test/types/string.js b/test/types/string.js
index 438d553b..dcb3462f 100755
--- a/test/types/string.js
+++ b/test/types/string.js
@@ -1323,7 +1323,8 @@ describe('string', () => {
                     type: 'string.domain',
                     context: { value: '"example.com', label: 'value' }
                 }],
-                ['mail@example.com', false, '"value" must contain a valid domain name']
+                ['mail@example.com', false, '"value" must contain a valid domain name'],
+                ['joi.dev.whatevertldiwant', false, '"value" must contain a valid domain name']
             ]);
         });
 
@@ -1496,7 +1497,8 @@ describe('string', () => {
                     type: 'string.email',
                     context: { value: '123456789012345678901234567890123456789012345678901234567890@12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345.toolong.com', invalids: ['123456789012345678901234567890123456789012345678901234567890@12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345678901234567890123456789012345678901234567890123456789.12345.toolong.com'], label: 'value' }
                 }],
-                ['foo@bar%2ecom', false, '"value" must be a valid email']
+                ['foo@bar%2ecom', false, '"value" must be a valid email'],
+                ['invalid_tlds@email.ccc', false, '"value" must be a valid email']
             ]);
         });