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

Rfc1918PrivateNetwork

class Rfc1918PrivateNetwork

Rfc1918PrivateNetwork constrains String to be either of Rfc1918ClassAPrivateNetwork, Rfc1918ClassBPrivateNetwork or Rfc1918PrivateNetwork

Safe nullable construction

import arrow.refinement.network.Rfc1918PrivateNetwork

Rfc1918PrivateNetwork.orNull("10.0.0.1")
// 10.0.0.1
Rfc1918PrivateNetwork.orNull("80.34.200.60")

Resolved constraints

Rfc1918PrivateNetwork.constraints("10.0.0.1")
// [(true, Expected 10.0.0.1 to be an IPv4 address), (true, Expected 10.0.0.1 to start with 10.)]
Rfc1918PrivateNetwork.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.)]
Rfc1918PrivateNetwork.isValid("10.0.0.1")
// true
Rfc1918PrivateNetwork.isValid("80.34.200.60")
// false

Folding validation

Rfc1918PrivateNetwork.fold("10.0.0.1", { "failed: $it" }, { "success: $it" })
// success: Rfc1918PrivateNetwork(value=10.0.0.1)
Rfc1918PrivateNetwork.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.)]

Unsafe require

try { Rfc1918PrivateNetwork.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.
Rfc1918PrivateNetwork.require("10.0.0.1")
// 10.0.0.1

Types

Companion companion object Companion : Refined<String, Rfc1918PrivateNetwork>

Properties

value val value: String

Do you like Arrow?

Arrow Org
<