Hey, I’m using `EnvironmentKey.uri().of().required...
# http4k
d
Hey, I’m using
EnvironmentKey.uri().of().required()
to use the
Uri
later in
ClientFilters.SetHostFrom()
. When I make the client call I run into the situation that I receive the error that
schema
is not a valid
http(s)
schema. Is there an elegant way to ensure that the
Uri
contains a valid schema? One option which came into my mind was using
map
in the lens, which didn’t felt nicely. Do you have some other ideas / advices?
a
If you're using
values4k
, you can make a custom value type that requires the scheme to be present.
Copy code
class Url(value: Uri): AbstractValue<Uri>(value) {
    companion object: ValueFactory<Url, Uri>(
        coerceFn = ::Url,
        parseFn = Uri::of,
        validation = {it.scheme.isNotEmpty() }
    )
}

val key = EnvironmentKey.value(Url).required("my_url")
🙏 1
👍 1
d
No, I’m not. Thank you for that idea. I can even without
values4k
create my own
Uri
class which is doing all the validation. In addition I have then to create my own lenses.
a
The benefit of values4k is that http4k has built-in support for it. But you can definitely make your own value class with a custom lens adapater.
👍 1