Naing Aung Luu
02/19/2023, 5:23 PMCLOVIS
02/20/2023, 8:52 AM@JvmInline value class Email(val text: String) {
init {
require('@' in text) { "…" }
}
}
Besides, value classes are regular types of the language, meaning they can be combined as fields of other classes etc to build complex data types.class FormData {
private var _email: Email? by mutableStateOf(null)
var email
get() = _email
set(value) {
require(value.text.length < 100) { "…" }
_email = value
}
}
What benefits does your approach bring over this style?class FormData {
var email by validate(mutableStateOf<Email?>(null)) {
require(it.text.length < 100) { "…" }
}
}
Naing Aung Luu
02/20/2023, 2:53 PM@Form
data class FormData(
@EmailAddress
@MinLength(2)
@MaxLength(10)
val emailAddress: String
)
The library also supports the value
classes as Field types. And extending the existing validators is also easy as well. You’ll notice that annotations and actual validation implementations are separated as @Annotation
and ValidationRule
.
It tries to provide those reusable APIs as building blocks for the form.CLOVIS
02/20/2023, 3:55 PMNaing Aung Luu
02/20/2023, 4:36 PM