Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lennykey opened this issue Apr 20, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@lennykey
Copy link

lennykey commented Apr 20, 2020

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
Copy link

sebdak commented Mar 10, 2021

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
Copy link
Collaborator

Duplicate of #29

@dhoepelman dhoepelman marked this as a duplicate of #29 May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants