Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate URI domain #1705

Closed
sm2017 opened this issue Jan 21, 2019 · 3 comments
Closed

Validate URI domain #1705

sm2017 opened this issue Jan 21, 2019 · 3 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@sm2017
Copy link

sm2017 commented Jan 21, 2019

I want to validate uri

const schema = Joi
    .string()
    .uri({
      scheme: [
        /https?/,
      ],
    });

I know that https://github is valid but in my use case I want to treat as invalid

@sholladay
Copy link

You could use invalid():

const schema = Joi
    .string()
    .uri({
      scheme: [
        /https?/,
      ],
    })
    .invalid('https://github.com');

@sm2017
Copy link
Author

sm2017 commented Jan 24, 2019

@sholladay I means https://github has no tld , I want force tld exists

@sholladay
Copy link

Oh, sorry, I misunderstood. :) Some regex could sort of do this, but I don't think there is a clean and proper way to do it at the moment.

const schema = Joi
    .string()
    .regex(/:\/\/[0-9a-z-.]+\.[a-z]+\//i)
    .uri({
      scheme: [
        /https?/,
      ],
    });

Caveats:

  • Not rigorous enough for security purposes. A specially crafted URL could fool this regex by putting an unencoded absolute URL in the path or query string. However, it should work correctly for benevolent users who want to embed URLs, since you would typically encode URLs before putting them in the path or query string.
  • This would prevent you from using https://localhost (with or without a port number), which may or may not matter. You could use .allow() to fix that specific case, I suppose. Or you could extend the regex even further.
  • I didn't account for numbers in TLDs, as there don't seem to be any TLDs that include numbers at the moment.

@hueniverse hueniverse self-assigned this May 30, 2019
@hueniverse hueniverse added the feature New functionality or improvement label May 30, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone May 30, 2019
@hueniverse hueniverse changed the title Question: Validate URI Validate URI domain May 30, 2019
@hueniverse hueniverse added the v16 label Aug 10, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants