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

Local Server URL Marked as Invalid #445

Open
GazEdge opened this issue Dec 9, 2021 · 2 comments
Open

Local Server URL Marked as Invalid #445

GazEdge opened this issue Dec 9, 2021 · 2 comments

Comments

@GazEdge
Copy link

GazEdge commented Dec 9, 2021

v3.22.1

Local server URLs are not seen as valid.

Example here:
https://codesandbox.io/s/npm-playground-forked-ne791

Code example here:

const Validator = require("validatorjs");

const validation = new Validator(
  {
    url1: "http://google.com", // valid
    url2: "http://localhost", // invalid
    url3: "http://localhost:8000", // invalid
    url4: "https://192.168.1.168", // invalid
    url5: "https://192.168.1.168:8000" // invalid
  },
  {
    url1: "url",
    url2: "url",
    url3: "url",
    url4: "url",
    url5: "url"
  }
);

if (validation.fails()) {
  let htmlStr = "";
  Object.values(validation.errors.all()).forEach((element) => {
    htmlStr = htmlStr + `<p>${element}</p>`;
  });

  document.getElementById("app").innerHTML = htmlStr;
}

@jershell
Copy link

jershell commented Jan 9, 2022

Also http://192.168.1.87:8080/debug marked as an invalid URL
Any ipv6 address will invalid like this

http://[::1]:3000/projects/5fd65de50df52c1a6781df40/hooks

I think we can replace it

  url: function (url) {
    return /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)/i.test(url);
  },

on simular this

const isValid = (str) => {try { new URL(str); return true; } catch(e) { return false }};
  url: function (url) {
    return isValidt(url);
  },

it will work on node & browsers.

@jershell
Copy link

jershell commented Jan 9, 2022

So, to fix this I used

 validator.register('url', isValidUrl, undefined);

The third argument is undefined for using the default error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants