-
Notifications
You must be signed in to change notification settings - Fork 8
Added isUrl() rule
#13
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
base: master
Are you sure you want to change the base?
Conversation
|
Sorry for late reply. I think it is ok to only support http/s scheme for now. But I test your regex at https://regexr.com/58lpv, it fails in some tests. Could you fix it? |
|
No problem! |
|
Does this work for |
|
@narwy I think this rule shouldn't allow schemeless URL. Or maybe we can add For example: isUrl() // valid with URL with http and https schemes
isUrl({schemes: ['http', 'https', 'ftp', '']}) // valid for "//foo.com", "https://foo.bar", "http://foo.com", "ftp://foo.com", etc |
|
but then that would defeat the purpose of the naming |
|
Hey @rachids , do you able to continue this PR? If you are able to continue this PR, I would like to add some tests in your code. |
|
Hello @emsifa, yes I will try to comply to all your regex rules 😉 Edit: I might require some help on achieving the regex rule |
|
@emsifa @rachids function isUrl(text: string, protocols: string[] = ["https:"]) {
const url = new URL(text);
if (!protocols.includes(url.protocol)) {
throw new Error(
`Only ${protocols.join(
", "
)} are supported. The url you provied has a protocol of: ${url.protocol}`
);
}
return url;
}
console.log(isUrl("https://url.com"));You can modify the above example to fit your use case. |
I wasn't able to convert the massive regexp rule from Laravel/Symfony into Validasaur, but this one will make any of the below urls valid :