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

url validation is passed for values without protocol at the beginning but with www. #334

Open
komarovalexander opened this issue Sep 28, 2023 · 2 comments

Comments

@komarovalexander
Copy link

komarovalexander commented Sep 28, 2023

Hello could you explain why "www.ka-table.com" is a valid url? - it does not have protocol https:// - should it be invalid?

const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`;

expected:
"www.ka-table.com" - invalid
"https://www.ka-table.com" - valid

thanks

@komarovalexander
Copy link
Author

komarovalexander commented Sep 28, 2023

should it not be just:

 const regex = `${protocol}${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`;

?

@sfreytag
Copy link

sfreytag commented Mar 25, 2024

Based on an idea in #303 I added a custom validator using the browser's URL interface. However this allows URLs of the form http:www.example.com. A good compromise seemed to be to use both:

const descriptor = {
    url: [
        // Test 1
        {
            type: 'url',
            required: true,
        },
        // Test 2
        {
            validator: (_, value) => {
                try {
                    new URL(value)
                    return true
                }
                catch {
                    return false
                }
            }
        }
    ]
}
const validator = new Schema(descriptor);
validator.validate({ url: 'www.example.com' }) // Test1: pass; Test2: fail; Overall: fail
validator.validate({ url: 'http:www.example.com' }) // Test1: fail; Test2: pass; Overall: fail
validator.validate({ url: 'http://www.example.com' }) // Test1: pass; Test2: pass; Overall: pass

URL.canParse would be more concise but it looks a bit new, unless you have a polyfill: https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static

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