hapijs / joi

The most powerful data validation library for JS
Other
20.92k stars 1.51k forks source link

Joi.string().email() gives Error: Built-in TLD list disabled #2390

Closed Bsd15 closed 4 years ago

Bsd15 commented 4 years ago

Support plan

Context

How can we help?

I referred the documentation and tried to use the email validation as follows

{
    validation: Joi.string().email()
}

But as soon as I load the page I get Error: Built-in TLD list disabled Screenshot from 2020-06-23 21-28-18 I tried with TLD 'allow' option set to true as well

{
    validation: Joi.string().email({tlds:{allow: true}})
}

But still I get the same error. When I set allow to false then it works properly. I've searched the docs and couldn't find anything.

AizenSousuke commented 4 years ago

I'm getting the same issue also.

DamodarSojka commented 4 years ago

Use .email({ tlds: { allow: false } }) if you don't have the IANA list of registered TLDs.

Per documentation: true to use the IANA list of registered TLDs. This is the default false to allow any TLD not listed in the deny list, if present. A Set or array of the allowed TLDs. Cannot be used together with deny.

Bsd15 commented 4 years ago

@DamodarSojka as I have stated in my comment I've done that only, But this is still an issue IMO as the examples in documentation show the usage as below without any error.

Joi.string().email()

So either the documentation should be updated or this is an actual issue.

hueniverse commented 4 years ago

Unfortunately, no community resources were available to help resolve this issue after two weeks, and it is being closed. We close unresolved community issues to keep the issue tracker organized and effective. Please check https://hapi.dev/support for other support options.

mrmadhat commented 4 years ago

I think this issue should be reopened the docs seem to be incorrect/confusing.

  • tlds - options for TLD (top level domain) validation. By default, the TLD must be a valid name listed on the IANA registry. To disable validation, set tlds to false. To customize how TLDs are validated, set one of these:
    • allow - one of:
      • true to use the IANA list of registered TLDs. This is the default value.

But when using something like Joi.attempt(email@example.com, Joi.string().email() the error @Bsd15 describes is shown.

NandoZee commented 1 year ago

Note that @hapi/joi has been deprecated: https://www.npmjs.com/package/@hapi/joi

For joi: https://www.npmjs.com/package/joi

Through testing, I can confirm that "@hapi/joi": "17.1.1" will throw errors for TLDs as you've reported, whereas "joi": "17.7.0", does not. So your answer is to npm uninstall @hapi/joi and npm i joi.

lunatupini commented 1 year ago

Note that @hapi/joi has been deprecated: https://www.npmjs.com/package/@hapi/joi

For joi: https://www.npmjs.com/package/joi

Through testing, I can confirm that "@hapi/joi": "17.1.1" will throw errors for TLDs as you've reported, whereas "joi": "17.7.0", does not. So your answer is to npm uninstall @hapi/joi and npm i joi.

That's not the case for me. I'm running "joi": "17.7.0" and the error persists

danielimmke commented 1 year ago

I am getting this error too, but it's intermittent. I am trying to narrow down the cause. It's possible a new bug was introduced recently that's triggering the same error.

danielimmke commented 1 year ago

It looks like the package that does the TLD validation is "@sideway/address" but I couldn't find a default list there. Maybe that's where the error is coming from?

I solved this by grabbing the IANA list, converting to an array and passing it in directly to the email options. This list is almost 1500 items. I was relieved to see in the code it's converting the array to a Set and using has for O(1) time complexity.

image
danielimmke commented 1 year ago

As an addendum to this, it actually did not fix the problem. It fixed the error I was getting.

When you pass an allow list, it seems to be case sensitive (!?) - the entire list I pulled originally was all caps.

I had to do .lowercase() and then also convert the entire TLD list to lowercase to get this working consistently. There's definitely some kind of bug here.

Attaching the list for anybody else who needs it. tlds.txt

tommywalkie commented 1 year ago

but I couldn't find a default list there

@danielimmke It used to have one, got deleted when the package was rewritten in TS.

racheldotey commented 1 year ago

Through testing, I can confirm that "@hapi/joi": "17.1.1" will throw errors for TLDs as you've reported, whereas "joi": "17.7.0", does not.

I just experienced this issue running "joi": "^17.7.1", but I think I've worked around it with the .email({ tlds: { allow: false } }) setting. Oddly I was using the exact same schema on the server side and did not get this error. The only difference was the joi running in React threw the TLD error.

Marsup commented 1 year ago

FWIW, we're now using @hapi/tlds internally. There's no guarantee that it's always up-to-date, but I should be able to apply updates to that list rapidly. It's intentionally not bundled to avoid adding weight for people not using this feature.

JuroOravec commented 1 year ago

Just came across this issue, using "joi": "^17.9.2" in browser. I can confirm the above solutions work.

Either:

  1. Disable TLDs check with
    Joi.string().email({ tlds: false });
  2. OR install and use @hapi/tlds
    npm i @hapi/tlds
    import { tlds } from '@hapi/tlds';
    Joi.string().email({ tlds: { allow: tlds } });

    NOTE: No need to lowercase it, see https://github.com/hapijs/tlds/blob/bf4415f67deaa3afe16106b2ff9a36f7fb9c73fc/src/index.ts#L1496