J
10/03/2024, 7:34 AM@JvmInline
value class Email(val value: String) {
companion object {
private const val emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\$"
fun from(email: String): Either<FieldValidationFailure, Email> =
either {
ensure(email.matches(emailRegex.toRegex())) { InvalidEmail(email) }
Email(email)
}
}
}
However i really dislike having to .value to get the actual values out whenever I need them. Are there any other ways of doing this in kotlin?Alejandro Serrano.Mena
10/03/2024, 7:52 AMDavid
10/03/2024, 1:55 PMEmail
it could be worth make it's constructor private:
value class Email private constructor(val value: String) {
dave08
10/05/2024, 8:25 PMJ
10/07/2024, 7:19 AMdave08
10/07/2024, 9:12 AM@JvmInline
value class Email(val value: String): CharSequence by value {
companion object {
private const val emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\$"
fun from(email: String): Either<FieldValidationFailure, Email> =
either {
ensure(email.matches(emailRegex.toRegex())) { InvalidEmail(email) }
Email(email)
}
}
}
dave08
10/07/2024, 9:13 AM