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

isWebsite does not support internal domains or ports #74

Open
mdryden opened this issue Jun 2, 2022 · 1 comment
Open

isWebsite does not support internal domains or ports #74

mdryden opened this issue Jun 2, 2022 · 1 comment

Comments

@mdryden
Copy link

mdryden commented Jun 2, 2022

Expected Behavior

Urls like "http://localhost:8000" or ""http://foo.com:8080" pass isWebsite() validator.

Actual Behavior

Any URL with a port or without a domain suffix will fail the regex matching.

Steps to Reproduce the Problem

  1. Create a string validator which uses isWebsite (eg: validate(MyCommand::myUrl).isNotEmpty().isWebsite()
  2. Pass it a value with a port or an internal domain name,

Example Code or Link to a Project

data class MyCommand(
    val url1: String,
    val url2: String,
) : BaseCommand() {
    init {
        validate(this) {
            validate(MyCommand::url1).isNotEmpty().isWebsite()
            validate(MyCommand::url2).isNotEmpty().isWebsite()
        }
    }
}

Json Payload

{
    "url1": "http://localhost/",
    "url2": "http://mydomain.com:8000"
}

Environment

  • Valiktor version: 0.12.0
  • JDK/JRE version: 17
  • Platform/OS: Windows
@mdryden
Copy link
Author

mdryden commented Jun 2, 2022

You might consider trying to use the URL class to do a bit more validation - here's the solution I came up with since I needed something right away:


fun String.isWebsite(): Boolean {
    return try {
        val url = URL(this)
        return url.host.isNotEmpty() && url.protocol.startsWith("http")
    } catch (e: Throwable) {
        false
    }
}

fun <E> Validator<E>.Property<String?>.isWebsite(): Validator<E>.Property<String?> =
    this.validate(Website) {
        it == null || it.isWebsite()
    }

The URL constructor throws an exception for a completely invalid URL, but it accepts some URLs I wouldn't consider valid (eg: http://), and many which we probably wouldn't consider a website (eg: ftp://foo.bar).

For my purposes, I'm considering a website anything which passes the URL parsing, has a host and starts with http.

Test cases:

http://google.com:8000 - pass
http://localhost - pass
http:// - fail
ftp://foo.bar - fail
http://localhost: - pass (should fail - this solution isn't perfect either).

As noted with test case 5 this solution isn't perfect either but it's close enough for my purposes. I'm not sure if it's appropriate for this library, but maybe it's a helpful starting place if you decide to move forward with this report.

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

No branches or pull requests

1 participant