sealed interface TypeError {
val error: String
class WrongName(override val error: String) : TypeError
}
@Serializable
@JvmInline
value class Surname private constructor(
val name: String) {
companion object {
private val rightRegister = Regex("\\p{Lu}\\p{Ll}+")
operator fun invoke(name: String):
Either<TypeError, Surname> = either {
ensure(rightRegister.matches(name)) {
TypeError.WrongName("Wrong Surname") }
Surname(name) } }
override fun toString() = name }
operator fun invoke(
firstname: Either<TypeError, Firstname>,
surname: Either<TypeError, Surname>
): EitherNel<TypeError, Student> = either {
zipOrAccumulate(
{ firstname.bind() },
{ surname.bind() },
{ if (firstname.getOrNull()?.name ==
surname.getOrNull()?.name)
raise(TypeError.WrongName("Firstname = Surname")) }
) { f, s, _ -> Student(f, s) }
}
onClick = {
val student: Either<NonEmptyList<TypeError>, Student> =
Student(Firstname(firstname), Surname(surname))
labelRef.current?.textContent = when (student) {
is Either.Left ->
student.value.joinToString { it.error }
is Either.Right -> {
props.saveElement(student.value)
"" }
}
}