You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently we have to provide references to a class properties to validate like that
data classLongLongLongClassNameEmployee(valid:Int, valname:String, valemail:String) {
init {
validate(this) {
validate(LongLongLongClassNameEmployee::id).isPositive()
validate(LongLongLongClassNameEmployee::name).hasSize(min =3, max =80)
validate(LongLongLongClassNameEmployee::email).isNotBlank().isEmail()
}
}
}
It becomes very annoying when class names are long.
It is also confusing and questionable: why we have to provide type if we already work with an instance of this class provided through validate(this)?
The problem is that we can't currently provide property references like that:
data classLongLongLongClassNameEmployee(valid:Int, valname:String, valemail:String) {
init {
validate(this) { employee ->
validate(employee::id).isPositive()
validate(employee::name).hasSize(min =3, max =80)
validate(employee::email).isNotBlank().isEmail()
}
}
}
or even like this:
data classLongLongLongClassNameEmployee(valid:Int, valname:String, valemail:String) {
init {
validate(this) {
validate(it::id).isPositive()
validate(it::name).hasSize(min =3, max =80)
validate(it::email).isNotBlank().isEmail()
}
}
}
This is due to a limitation of Validator implementation that works only with KProperty1 property types of a Kotlin Reflect package.
When we reference class property Kotlin returns KProperty1 type which is now used in Validator implementation.
When we reference class instance property Kotlin returns KProperty0 type which is not supported by Validator implementation.
Possible solution
Validator could work with both KProperty0 and KProperty1 types (by overloading validate functions) that will give us the way to get property from a validation receiver directly
Suggested working test:
@Test
fun`should validate by KProperty0`() {
validate(Employee(company =Company(1))) {
validate(it::company).isNotNull().validate { company ->
validate(company::id).isNotNull()
}
validate(it::id).isNull()
validate(it::address).isNull()
validate(it::name).isNull()
}
}
The text was updated successfully, but these errors were encountered:
ydanneg
added a commit
to ydanneg/valiktor
that referenced
this issue
Dec 16, 2021
Problem
Currently we have to provide references to a class properties to validate like that
validate(this)
?The problem is that we can't currently provide property references like that:
or even like this:
This is due to a limitation of Validator implementation that works only with KProperty1 property types of a Kotlin Reflect package.
When we reference class property Kotlin returns KProperty1 type which is now used in Validator implementation.
When we reference class instance property Kotlin returns KProperty0 type which is not supported by Validator implementation.
Possible solution
Validator could work with both KProperty0 and KProperty1 types (by overloading
validate
functions) that will give us the way to get property from a validation receiver directlySuggested working test:
The text was updated successfully, but these errors were encountered: