konform-kt / konform

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

Allow access to subvalidations #1

Closed nlochschmidt closed 2 years ago

nlochschmidt commented 6 years ago

It should be possible to access subvalidations of a complete validation for example to use it on input fields.

Probably something along the lines of

val validation = Validation<DTO> { DTO::firstName.has.minLength(2) }
val firstNameValidation: Validation<String> = validation[DTO::firstName]
nlochschmidt commented 6 years ago

Turns out this is pretty difficult to achieve because the return value of the get function needs to take the type parameter of the provided Property into account and also whether that Property references a Nullable, Iterable, Map or Array, etc... As a workaround for version v0.1 I have decided to allow the extraction of sub validations while constructing the validations instead of afterwards. This is implemented in 0af7d76f. An example looks like this:

val ageCheck = Validation<UserProfile> {
    UserProfile::age required {
        minimum(18)
    }
}

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

    run(ageCheck)
}
nlochschmidt commented 2 years ago

Related to #37

nlochschmidt commented 2 years ago

Unfortunately I haven't found a good way to do this in Kotlin.