Lawik
04/17/2019, 11:54 AM(person::class.companionObjectInstance as PersonDTO.Companion)
(person is an instance of PersonDTO)ribesg
04/17/2019, 12:00 PMPersonDTO.Companion
Lawik
04/17/2019, 12:13 PMinterface Validateable<T> {
val validator: Validation<T>
}
I implement this interface on my class's companion object:
@Serializable
data class PersonDTO(val id: Long? = null, val name: String, var age: Int) {
companion object : Validateable<PersonDTO> {
override val validator = Validation<PersonDTO> {
PersonDTO::age{
minimum(0)
}
}
}
}
When processing requests on the backend, I would like to (dynamically) check whether the body's (which is an instance) companion object implements the Validateable
interface.
I have tried this:
val person = PersonDTO(null, "test", -24)
val companion = person::class.companionObjectInstance
if(companion is Validateable<*>){
companion.validator(person)
}
But this gives me the following error: Out-projected type 'Validation<out Any?>' prohibits the use of public open operator fun invoke(value: T): ValidationResult<T> defined in io.konform.validation.Validation
PersonDTO
instead but I'd like validator
to be static which doesn't seem possible when overriding from interface 😔ribesg
04/17/2019, 12:35 PManyModelInstance.isValid()
or something like that?interface ValidatableModel<This : ValidatableModel<This>>
interface ModelValidator<Model : ValidatableModel<Model>> {
fun ensureValid(model: Model)
}
data class MyModel(val value: Int) : ValidatableModel<MyModel> {
object Validator : ModelValidator<MyModel> {
override fun ensureValid(model: MyModel) {
check(model.value >= 0)
}
}
}
Lawik
04/17/2019, 1:23 PMval person = PersonDTO(null, "test", -24)
val companion = person::class.companionObjectInstance
if (companion is Validator<*>) {
companion.validate(person)
}
ribesg
04/17/2019, 1:25 PMLawik
04/17/2019, 1:26 PM@Serializable
data class PersonDTO(val id: Long? = null, val name: String, var age: Int) : Validateable<PersonDTO> {
companion object : Validator<PersonDTO> {
override fun validate(m: PersonDTO): ValidationResult<PersonDTO> {
return validator.validate(m)
}
val validator = Validation<PersonDTO> {
PersonDTO::age{
minimum(0)
}
}
}
}
interface Validateable<This : Validateable<This>>
interface Validator<Model : Validateable<Model>> {
fun validate(m: Model): ValidationResult<Model>
}
Validation<T>
and ValidationResult<T>
are from the https://www.konform.io/ library. (Which I'd like to keep using.)ribesg
04/17/2019, 2:03 PMif (companion is Validator<*>) {
Lawik
04/17/2019, 3:22 PMinterface Validateable<T>{
fun validate(): ValidationResult<T>
}
DTO:
@Serializable
data class PersonDTO(val id: Long? = null, val name: String, var age: Int) : Validateable<PersonDTO> {
override fun validate() = validator.validate(this)
private companion object {
val validator = Validation<PersonDTO> {
PersonDTO::age{
minimum(0)
}
}
}
}
This allows me to validate my objects as follows:
if (o is Validateable<*>) {
o.validate()
}