arrow-refined-types / arrow.refinement.network / PrivateNetwork

PrivateNetwork

class PrivateNetwork

PrivateNetwork constrains String to match any of the types Rfc1918PrivateNetwork, Rfc5737TestnetNetwork, Rfc3927LocalLinkNetwork and Rfc2544BenchmarkNetwork

Safe nullable construction

import arrow.refinement.network.PrivateNetwork

PrivateNetwork.orNull("192.168.1.2")
// 192.168.1.2
PrivateNetwork.orNull("80.34.200.60")

Resolved constraints

PrivateNetwork.constraints("192.168.1.2")
// [(true, Expected 192.168.1.2 to be an IPv4 address), (true, Expected 192.168.1.2 to start with 192.168.)]
PrivateNetwork.constraints("80.34.200.60")
// [(true, Expected 80.34.200.60 to be an IPv4 address), (false, Expected 80.34.200.60 to start with 10.), (false, Expected 80.34.200.60 to match ^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\..+), (false, Expected 80.34.200.60 to start with 192.168.), (false, Expected 80.34.200.60 to start with 192.0.2.), (false, Expected 80.34.200.60 to start with 198.51.100.), (false, Expected 80.34.200.60 to start with 203.0.113.), (false, Expected 80.34.200.60 to start with 169.254.), (false, Expected 80.34.200.60 to start with 198.18.), (false, Expected 80.34.200.60 to start with 198.19.)]
PrivateNetwork.isValid("192.168.1.2")
// true
PrivateNetwork.isValid("80.34.200.60")
// false

Folding validation

PrivateNetwork.fold("192.168.1.2", { "failed: $it" }, { "success: $it" })
// success: PrivateNetwork(value=192.168.1.2)
PrivateNetwork.fold("80.34.200.60", { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected 80.34.200.60 to start with 10.), (false, Expected 80.34.200.60 to match ^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\..+), (false, Expected 80.34.200.60 to start with 192.168.), (false, Expected 80.34.200.60 to start with 192.0.2.), (false, Expected 80.34.200.60 to start with 198.51.100.), (false, Expected 80.34.200.60 to start with 203.0.113.), (false, Expected 80.34.200.60 to start with 169.254.), (false, Expected 80.34.200.60 to start with 198.18.), (false, Expected 80.34.200.60 to start with 198.19.)]

Unsafe require

try { PrivateNetwork.require("80.34.200.60") } catch (e: IllegalArgumentException) { e.message }
// Expected 80.34.200.60 to start with 10., Expected 80.34.200.60 to match ^172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)\..+, Expected 80.34.200.60 to start with 192.168., Expected 80.34.200.60 to start with 192.0.2., Expected 80.34.200.60 to start with 198.51.100., Expected 80.34.200.60 to start with 203.0.113., Expected 80.34.200.60 to start with 169.254., Expected 80.34.200.60 to start with 198.18., Expected 80.34.200.60 to start with 198.19.
PrivateNetwork.require("192.168.1.2")
// 192.168.1.2

Types

Companion companion object Companion : Refined<String, PrivateNetwork>

Properties

value val value: String

Do you like Arrow?

Arrow Org
<