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

Make validations context aware #62

Open
geertmulders opened this issue Oct 12, 2022 · 1 comment
Open

Make validations context aware #62

geertmulders opened this issue Oct 12, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@geertmulders
Copy link

geertmulders commented Oct 12, 2022

Currently when you want to do a database call or check a value against some set of data, your only option is to wrap the "context" in the builder, e.g.

val allowedValues: Set<String> = calculateAllowedValues()
val validation = Validation<String> {
    addConstraint("This value is not allowed!") { allowedValues.contains(it) }
}
validation("wim")

The result is that a validation is tightly coupled to its "context". This is troublesome especially when the "context" is not constant.

Prettier would be:

val validation = Validation<Set<String>, String> {
    addConstraint("This value is not allowed!") { value -> this.contains(value) }
}
val allowedValues: Set<String> = calculateAllowedValues()
validation(allowedValues, "wim")

The changes in PR #61 allow the user to use a piece of context, with almost no breaking changes for those that don't need any context (the special case, with Unit context). The only breaking change is when you define your own extension function on a ValidationBuilder.

fun ValidationBuilder<String>.myOwnConstraintBuilder() = ...
// becomes:
fun ValidationBuilder<Unit, String>.myOwnConstraintBuilder() = ...

It makes it also possible to combine validations with different contexts (see for example test method composeValidationsWithContext):

val addressValidation = Validation<AddressContext, Address> {
    Address::country {
        addConstraint("Country is not allowed") {
            this.validCountries.contains(it)
        }
    }
}

val validation = Validation<Context, Register> {
    Register::home ifPresent {
        run(addressValidation, Context::subContext)
    }
}

Please consider merging this branch.

@Nek-12
Copy link

Nek-12 commented Mar 11, 2023

Just came here looking because of the exact same issue. Want to use another field of the object in my validation

@dhoepelman dhoepelman added the enhancement New feature or request label 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

3 participants