GithubHelp home page GithubHelp logo

miguelmota / is-valid-domain Goto Github PK

View Code? Open in Web Editor NEW
70.0 7.0 17.0 120 KB

Validate domain name in JavaScript

Home Page: https://lab.miguelmota.com/is-valid-domain/

License: MIT License

JavaScript 90.07% HTML 9.93%
javascript nodejs validation domain hostname

is-valid-domain's Introduction

is-valid-domain

Validate domain name in JavaScript

Demo

https://lab.miguelmota.com/is-valid-domain

Install

npm install is-valid-domain

Usage

const isValidDomain = require('is-valid-domain')

isValidDomain('example.com') // true
isValidDomain('foo.example.com') // true
isValidDomain('bar.foo.example.com') // true
isValidDomain('exa-mple.co.uk') // true
isValidDomain('xn--80ak6aa92e.com') // true
isValidDomain('_dnslink.ipfs.io') // true
isValidDomain('exa_mple.com') // false
isValidDomain('-example.co.uk') // false
isValidDomain('example') // false
isValidDomain('ex*mple.com') // false
isValidDomain('*.example.com') // false
isValidDomain('*.com') // false
isValidDomain(3434) // false

isValidDomain('foo.example.com', {subdomain: true}) // true
isValidDomain('foo.example.com', {subdomain: false}) // false
isValidDomain('*.example.com', {wildcard: false}) // false
isValidDomain('*.example.com', {wildcard: true}) // true
isValidDomain('*.example.com', {subdomain: false, wildcard: true}) // false
isValidDomain('はじめよう.みんな') // false
isValidDomain('はじめよう.みんな', {allowUnicode: true}) // true
isValidDomain('ai.') // false
isValidDomain('ai.', {topLevel: true}) // true

view more examples

Test

npm test

Contributing

Adding new domains:

  • Add second level domain to data/second_level_domains.csv
  • Run npm run generate to generate JSON map file
  • Run npm test

FAQ

  • Q: Why is trailing dot . in domain names verified as true?

    • A: Fully qualified domain names allow the trailing dot which represents the root zone. More info here and here.

License

MIT

is-valid-domain's People

Contributors

andrewimeson avatar bluelovers avatar claydiffrient avatar evgencode avatar markporamest avatar miguelmota avatar nahanil avatar pfrazee avatar timjrobinson avatar unggulzmalqp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

is-valid-domain's Issues

Support Typescript import

Hey,

Love this package and was using the 0.0.17 version in my typescript project.
But since 0.1.6, i'm not able to import it correctly (no matters the way, default or single).

Capture d’écran 2022-06-30 à 01 17 19

Do you plan to support it again ?
Thanks!

subdomain ending in dash

i thought a sub domain can't start or end in a -
but it seems to allow it when at the end of the subdomain

punnycode with hypen

Hello!

Thank you for this library.

I found a small bug - this should be a valid domain name:
привет-мир.рф -> xn----ctbjkdxqigq.xn--p1ai

> isValidDomain('xn----ctbjkdxqigq.xn--p1ai')
false

Subdomain that ends with a dot is valid

When a domain ends with a dot, and subdomain flag is set to false, it still returns as valid:

isValidDomain('example.com.', { subdomain: false } // returns true

Is this the correct behavior?

Wildcard option with TLDs

It looks like the wildcard option cannot be used together with the topLevel option.

const isValidDomain = require('is-valid-domain')

isValidDomain('ch', {topLevel: true, subdomain: true})  // -> true
isValidDomain('*.ch', {topLevel: true, wildcard: true, subdomain: true})  // -> false

// one from sldMap.json has the same problem:
isValidDomain('gov.sg', {topLevel: true, subdomain: true})  // -> true
isValidDomain('*.gov.sg', {topLevel: true, wildcard: true, subdomain: true})  // -> false

Is this a bug or something that can be supported in the future? Either with those options or a new specific one (e.g. secondLevelWildcard)?
Thank you already for your feedback!

Allow domain containing double hyphens

Hey! Thanks for creating this package.

We have an example of a valid ASCII domain containing 2 dashes but this package is considering it as invalid.

isValidDomain('abcd--def.example.com')

"Failed to compile."

With the latest version, my NextJS build (using Webpack 5) fails. Here's the error:

Failed to compile.

ModuleNotFoundError: Module not found: Error: Can't resolve 'punycode' in '/Users/msc/Code/@sitearcade/words/node_modules/is-valid-domain'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "punycode": require.resolve("punycode/") }'
	- install 'punycode'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "punycode": false }


> Build error occurred
Error: > Build failed because of webpack errors
    at /PROJECT/node_modules/next/dist/build/index.js:15:924
    at async Span.traceAsyncFn (/PROJECT/node_modules/next/dist/telemetry/trace/trace.js:6:584)

TLDs not supported

This library doesn't appear to detect a TLD as a valid domain, even though they are -- it would be great if this was supported, either by default or with a flag.

Currently:

const isValidDomain = require('is-valid-domain')

console.log(isValidDomain('test.ai.')) // true
console.log(isValidDomain('ai.')) // false

But http://ai./ is valid.

Validation without subdomain should validate .co.uk domains

When I try to validate a domain name without subdomain is work for most of domains but fail with composed TLD (co.uk ...)

For example:

isValidDomain('www.domain.com', {
  subdomain: false,
}); // false

isValidDomain('domain.co.uk', {
  subdomain: false,
}); // false

The second case should be true instead of false.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.