Implement a more strict hostname validation (isValid) #100
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Attempt at addressing #59 and #73 (it should be trivial to allow
_
as well, but we should probably discuss of a possible API for that there => #99).This function implements a more strict validation of the hostnames. It checks:
As far as I know that should be it, but let me know if you see some corner cases that are not covered.
Regarding performance, this one times at:
This is about
x6
slower than the previous implementation (which did not check much). So I'd say it's still fast enough (compared tocleanHost
~360,000 ops/sec
orgetDomain
~120,000 ops/sec
). I also compared it with a more naive validation regex (which does not check every thing) which times at9,574,128 ops/sec
.[1][1]
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/
(does not check maximum size, maximum label size, forbid trailing dot).