Skip to content

Add hostname matching to Joi.String#210

Closed
osslate wants to merge 10 commits intohapijs:masterfrom
osslate:master
Closed

Add hostname matching to Joi.String#210
osslate wants to merge 10 commits intohapijs:masterfrom
osslate:master

Conversation

@osslate
Copy link

@osslate osslate commented Mar 23, 2014

Match hostnames as defined in RFC1123 - note that this doesn't validate the length, which can be a maximum of 255 characters.

@hueniverse
Copy link
Contributor

Why not enforce length if it is part of the spec? Also, need more test cases like numbers.

@osslate
Copy link
Author

osslate commented Apr 2, 2014

Will add a check for the length and add some tests.

@skeggse
Copy link

skeggse commented Apr 8, 2014

Note that this does not include IPv6 hostname support.

@osslate
Copy link
Author

osslate commented Apr 8, 2014

It's not designed to match IP addresses in particular, it's designed to match hostnames (which does include any IPv4 address as they're technically valid hostnames).

@skeggse
Copy link

skeggse commented Apr 8, 2014

Yup, but IPv6 addresses are also technically valid hostnames.

@osslate
Copy link
Author

osslate commented Apr 8, 2014

Do you know of any regular expressions that can match hostnames (including IPv4 and IPv6)? I looked specifically through the RFCs for a reference when I implemented this, can't find much on IPv6 hostnames.

@skeggse
Copy link

skeggse commented Apr 8, 2014

Am I correct in assuming that joi is Node-only? If so, I'd recommend using the net.isIP function in conjunction with a regular expression. Regular expressions are powerful, but extremely idiomatic and not always the right tool for the job (especially when it comes to email addresses but that's a different story).

In a URL/URI, IPv6 hostnames are special, and must be wrapped with square brackets, but they stand alone as just a hostname (so you should just be able to pass the provided hostname straight to the isIP function, and if it fails pass it to a regex test method).

> url.parse('https://[fe80::d63d:7eff:feda:d3a7]:3000/resource')
{ protocol: 'https:',
  slashes: true,
  auth: null,
  host: '[fe80::d63d:7eff:feda:d3a7]:3000',
  port: '3000',
  hostname: 'fe80::d63d:7eff:feda:d3a7',
  hash: null,
  search: null,
  query: null,
  pathname: '/resource',
  path: '/resource',
  href: 'https://[fe80::d63d:7eff:feda:d3a7]:3000/resource' }
> net.isIP('fe80::d63d:7eff:feda:d3a7')
6

@osslate
Copy link
Author

osslate commented Apr 8, 2014

Completely forgot about the url/net module. Will push a fix later on.

@osslate
Copy link
Author

osslate commented Apr 8, 2014

I used regex.match() due to it being used everywhere else throughout Joi rather than .test().

@skeggse
Copy link

skeggse commented Apr 8, 2014

Ah, fair enough. Consistency over correctness.

@hueniverse
Copy link
Contributor

We just got joi compatible with browserify. Will this break it?

@skeggse
Copy link

skeggse commented Apr 9, 2014

Yep. Darn, that would've been nice.

Edit: Actually, I'm not sure. Does browserify's net module provide an isIP implementation?

@osslate
Copy link
Author

osslate commented Apr 9, 2014

Apparently it doesn't provide anything: https://github.com/substack/node-browserify/blob/master/lib/builtins.js#L16

It'd be easy to whip out the isIPv6 function from Node, but I'm not sure if it's worthwhile.

@hueniverse hueniverse self-assigned this Apr 9, 2014
@hueniverse
Copy link
Contributor

Can you update this to the master branch?

@hueniverse hueniverse added this to the 4.0.0 milestone Apr 17, 2014
@hueniverse hueniverse modified the milestone: 4.0.0 Apr 22, 2014
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 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

Successfully merging this pull request may close these issues.

3 participants