konform-kt / konform

Portable validations for Kotlin
https://www.konform.io
MIT License
668 stars 40 forks source link

[Feature] Add the ability to activate validations for Properties depending of the state of another property #20

Closed lennykey closed 5 months ago

lennykey commented 4 years ago

Example:

if a firstName was passed to a Person and this firstName is present then the user should also enter a familyName to pass the validation if the firstName was not passed (null) then the validation for the familyName should not be active Something like this should be possible:

data class Person(val name: String?, val familyName: String?)

 val validator = Validation<Person> {
        Person::name ifPresent {
            minLength(1)

            Person::familyName {
                minLength(1)
            }
        }

        Person::familyName ifPresent {
            minLength(1)

            Person::name {
                minLength(1)
            }

        }
    }
sebdak commented 3 years ago

On a similar note to this, is it possible to somehow infer that a nullable property is non-null if declared as required in the validation spec?

data class UserProfile(
        val fullName: String,
        val age: Int?
)

val validateUser = Validation<UserProfile> {
    UserProfile::fullName {
        minLength(2)
        maxLength(100)
    }

    UserProfile::age required {
        minimum(0)
        maximum(150)
    }
}

val user = UserProfile("username", 1)
val validationResult = validateUser.validate(user)

when (validationResult) {
    is Valid -> {
        // validationResult.value.age is inferred to type "Int" instead of "Int?"
    }
}
dhoepelman commented 5 months ago

Duplicate of #29