Newtype + Refined
Create with syntax
Import syntax
import refined4s.syntax.*
refinedNewtype (Either[String, ?]
)
import refined4s.*
import refined4s.types.all.*
type Name = Name.Type
object Name extends Newtype[NonEmptyString]
"Carol Danvers".refinedNewtype[Name]
// res1: Either[String, Name] = Right(value = "Carol Danvers")
val name = "Kal-El"
// name: String = "Kal-El"
name.refinedNewtype[Name]
// res2: Either[String, Name] = Right(value = "Kal-El")
"".refinedNewtype[Name]
// res3: Either[String, Name] = Left(
// value = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
val invalidName = ""
// invalidName: String = ""
invalidName.refinedNewtype[Name]
// res4: Either[String, Name] = Left(
// value = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
Create with cats.syntax
Import cats.syntax
import refined4s.modules.cats.syntax.*
refinedNewtypeNec (EitherNec[String, ?]
)
"Carol Danvers".refinedNewtypeNec[Name]
// res5: Either[Type[String], Name] = Right(value = "Carol Danvers")
val name2 = "Kal-El"
// name2: String = "Kal-El"
name2.refinedNewtypeNec[Name]
// res6: Either[Type[String], Name] = Right(value = "Kal-El")
"".refinedNewtypeNec[Name]
// res7: Either[Type[String], Name] = Left(
// value = Singleton(
// a = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
// )
val invalidName2 = ""
// invalidName2: String = ""
invalidName2.refinedNewtypeNec[Name]
// res8: Either[Type[String], Name] = Left(
// value = Singleton(
// a = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
// )
refinedNewtypeNel (EitherNel[String, ?]
)
"Carol Danvers".refinedNewtypeNel[Name]
// res9: Either[NonEmptyList[String], Name] = Right(value = "Carol Danvers")
val name3 = "Kal-El"
// name3: String = "Kal-El"
name3.refinedNewtypeNel[Name]
// res10: Either[NonEmptyList[String], Name] = Right(value = "Kal-El")
"".refinedNewtypeNel[Name]
// res11: Either[NonEmptyList[String], Name] = Left(
// value = NonEmptyList(
// head = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String",
// tail = List()
// )
// )
val invalidName3 = ""
// invalidName3: String = ""
invalidName3.refinedNewtypeNel[Name]
// res12: Either[NonEmptyList[String], Name] = Left(
// value = NonEmptyList(
// head = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String",
// tail = List()
// )
// )
validateAs (Validated[String, ?]
)
"Carol Danvers".validateAs[Name]
// res13: Validated[String, Name] = Valid(a = "Carol Danvers")
val name4 = "Kal-El"
// name4: String = "Kal-El"
name4.validateAs[Name]
// res14: Validated[String, Name] = Valid(a = "Kal-El")
"".validateAs[Name]
// res15: Validated[String, Name] = Invalid(
// e = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
val invalidName4 = ""
// invalidName4: String = ""
invalidName4.validateAs[Name]
// res16: Validated[String, Name] = Invalid(
// e = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
validateNecAs (ValidatedNec[String, ?]
)
"Carol Danvers".validateNecAs[Name]
// res17: Validated[Type[String], Name] = Valid(a = "Carol Danvers")
val name5 = "Kal-El"
// name5: String = "Kal-El"
name5.validateNecAs[Name]
// res18: Validated[Type[String], Name] = Valid(a = "Kal-El")
"".validateNecAs[Name]
// res19: Validated[Type[String], Name] = Invalid(
// e = Singleton(
// a = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
// )
val invalidName5 = ""
// invalidName5: String = ""
invalidName5.validateNecAs[Name]
// res20: Validated[Type[String], Name] = Invalid(
// e = Singleton(
// a = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String"
// )
// )
validateNelAs (ValidatedNel[String, ?]
)
"Carol Danvers".validateNelAs[Name]
// res21: Validated[NonEmptyList[String], Name] = Valid(a = "Carol Danvers")
val name6 = "Kal-El"
// name6: String = "Kal-El"
name6.validateNelAs[Name]
// res22: Validated[NonEmptyList[String], Name] = Valid(a = "Kal-El")
"".validateNelAs[Name]
// res23: Validated[NonEmptyList[String], Name] = Invalid(
// e = NonEmptyList(
// head = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String",
// tail = List()
// )
// )
val invalidName6 = ""
// invalidName6: String = ""
invalidName6.validateNelAs[Name]
// res24: Validated[NonEmptyList[String], Name] = Invalid(
// e = NonEmptyList(
// head = "Failed to create repl.MdocSession.MdocApp0.Name: Invalid value: []. It must be a non-empty String",
// tail = List()
// )
// )