-
Notifications
You must be signed in to change notification settings - Fork 13
/
Types.kt
28 lines (24 loc) · 1 KB
/
Types.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package klite
/** Base class for String-based normalized value types, handled automatically by [Converter] */
abstract class StringValue(val value: String) {
override fun toString() = value
override fun equals(other: Any?) = other != null && javaClass == other.javaClass && value == (other as StringValue).value
override fun hashCode() = value.hashCode()
}
class Email(email: String): StringValue(email.trim().lowercase()) {
companion object {}
init { require(value.length > 3 && value.contains("@")) { "Invalid email: $email" } }
val domain get() = value.substringAfter("@")
}
class Phone(phone: String): StringValue(phone.replace(removeChars, "")) {
companion object {
private val removeChars = "[\\s-()]+".toRegex()
private val valid = "\\+[0-9]{9,}".toRegex()
}
init { require(valid.matches(value)) {
"International phone number should start with + and have at least 9 digits: $value" }
}
}
class Password(value: String): StringValue(value) {
override fun toString() = "Password<***>"
}