Skip to main content
Version: v1.16.0

Refined String Types

Import

import refined4s.types.all.*

Refined NonEmptyString

Compile-time Validation

NonEmptyString("blah")
// res0: Type = "blah"
NonEmptyString("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
// res1: Type = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
NonEmptyString("")
// error:
// Invalid value: [""]. It must be a non-empty String

Runtime Validation

val validNonEmptyString1 = "blah" 
// validNonEmptyString1: String = "blah"
NonEmptyString.from(validNonEmptyString1)
// res2: Either[String, Type] = Right(value = "blah")

val validNonEmptyString2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
// validNonEmptyString2: String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
NonEmptyString.from(validNonEmptyString2)
// res3: Either[String, Type] = Right(
// value = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
// )
val invalidNonEmptyString = "" 
// invalidNonEmptyString: String = ""
NonEmptyString.from(invalidNonEmptyString)
// res4: Either[String, Type] = Left(
// value = "Invalid value: []. It must be a non-empty String."
// )

Concatenation

val nonEmptyString1 = NonEmptyString("Hello")
// nonEmptyString1: Type = "Hello"
val nonEmptyString2 = NonEmptyString(" World")
// nonEmptyString2: Type = " World"

nonEmptyString1 ++ nonEmptyString2
// res5: Type = "Hello World"

Get Value

val nonEmptyStringA = NonEmptyString("blah")
// nonEmptyStringA: Type = "blah"
val nonEmptyStringB = NonEmptyString("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
// nonEmptyStringB: Type = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."

nonEmptyStringA.value
// res6: String = "blah"

nonEmptyStringB.value
// res7: String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."

Refined NonBlankString

Compile-time Validation

NonBlankString("blah")
// res8: Type = "blah"
NonBlankString("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
// res9: Type = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
// error:
// NonBlankString("")
// ^^^^^^^^^^^^^^^^^^
// Invalid value: [""]. It must be not all whitespace non-empty String

NonBlankString(" ")
// error:
// NonBlankString(" ")
// ^^^^^^^^^^^^^^^^^^^
// Invalid value: [" "]. It must be not all whitespace non-empty String

NonBlankString(" \t \n \r")
// error:
// NonBlankString(" \t \n \r")
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Invalid value: [" \t \n \r"]. It must be not all whitespace non-empty String

Runtime Validation

val validNonBlankString1 = "blah"
// validNonBlankString1: String = "blah"
NonBlankString.from(validNonBlankString1)
// res10: Either[String, Type] = Right(value = "blah")

val validNonBlankString2 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
// validNonBlankString2: String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
NonBlankString.from(validNonBlankString2)
// res11: Either[String, Type] = Right(
// value = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
// )
val invalidNonBlankString1 = ""
// invalidNonBlankString1: String = ""
NonBlankString.from(invalidNonBlankString1)
// res12: Either[String, Type] = Left(
// value = "Invalid value: [], unicode=[]. It must be not all whitespace non-empty String."
// )

val invalidNonBlankString2 = " "
// invalidNonBlankString2: String = " "
NonBlankString.from(invalidNonBlankString2)
// res13: Either[String, Type] = Left(
// value = "Invalid value: [ ], unicode=[\\u0020]. It must be not all whitespace non-empty String."
// )

val invalidNonBlankString3 = " \t \n \r"
// invalidNonBlankString3: String = """
//
"""
NonBlankString.from(invalidNonBlankString3)
// res14: Either[String, Type] = Left(
// value = """Invalid value: [
//
], unicode=[\u0020\u0009\u0020\u000a\u0020\u000d]. It must be not all whitespace non-empty String."""
// )

Concatenation

val nonBlankString1 = NonBlankString("Hello")
// nonBlankString1: Type = "Hello"
val nonBlankString2 = NonBlankString(" World")
// nonBlankString2: Type = " World"

nonBlankString1 ++ nonBlankString2
// res15: Type = "Hello World"

nonBlankString1.appendString(" ")
// res16: Type = "Hello "

nonBlankString1.prependString(" ")
// res17: Type = " Hello"

Get Value

val nonBlankStringA = NonBlankString("blah")
// nonBlankStringA: Type = "blah"
val nonBlankStringB = NonBlankString("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
// nonBlankStringB: Type = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."

nonBlankStringA.value
// res18: String = "blah"

nonBlankStringB.value
// res19: String = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."

Refined Uuid

Compile-time Validation

import refined4s.types.all.*
Uuid("3596f062-a6bd-4d2c-978e-3ed6f97a264b")
// res21: Type = "3596f062-a6bd-4d2c-978e-3ed6f97a264b"

val uuid1 = java.util.UUID.randomUUID()
// uuid1: UUID = bea27b44-393c-4b73-a1fc-cd8d94412990
Uuid(uuid1)
// res22: Type = "bea27b44-393c-4b73-a1fc-cd8d94412990"
Uuid("")
// error:
// Invalid value: [""]. It must be UUID

Uuid("blah")
// error:
// Invalid value: ["blah"]. It must be UUID

Runtime Validation

val validUuidString = "3596f062-a6bd-4d2c-978e-3ed6f97a264b" 
// validUuidString: String = "3596f062-a6bd-4d2c-978e-3ed6f97a264b"
Uuid.from(validUuidString)
// res23: Either[String, Type] = Right(
// value = "3596f062-a6bd-4d2c-978e-3ed6f97a264b"
// )
val invalidUuid = "iuhsfd9f-f32wfwf3-d1i2j" 
// invalidUuid: String = "iuhsfd9f-f32wfwf3-d1i2j"
Uuid.from(invalidUuid)
// res24: Either[String, Type] = Left(
// value = "Invalid value: [iuhsfd9f-f32wfwf3-d1i2j]. It must be UUID."
// )

To java.util.UUID

val uuid2 = Uuid("3596f062-a6bd-4d2c-978e-3ed6f97a264b")
// uuid2: Type = "3596f062-a6bd-4d2c-978e-3ed6f97a264b"

uuid2.toUUID
// res25: UUID = 3596f062-a6bd-4d2c-978e-3ed6f97a264b

Get Value

val uuid3 = Uuid("3596f062-a6bd-4d2c-978e-3ed6f97a264b")
// uuid3: Type = "3596f062-a6bd-4d2c-978e-3ed6f97a264b"

uuid3.value
// res26: String = "3596f062-a6bd-4d2c-978e-3ed6f97a264b"