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

Allow predicated validation in Lazy Assertions chains #317

Open
ccybuszSV opened this issue Jun 28, 2021 · 0 comments
Open

Allow predicated validation in Lazy Assertions chains #317

ccybuszSV opened this issue Jun 28, 2021 · 0 comments

Comments

@ccybuszSV
Copy link

ccybuszSV commented Jun 28, 2021

The current Assert::lazy() is a very powerful tool that allows for clean easy validation in our actions. However, we've noticed that we sometimes come across this use case:

  1. A is a parameter that has many valid values (e.g. a number between 0 and 999 inclusive)
  2. B is a parameter that must be a valid email if A is greater than 0

As it stands, there's no "clean" way of doing this in one pass. It would require that a first Assert::lazy() be passed with all the validation that isn't predicated on a separate value. Then, a second Assert::lazy() must be assembled and verified after the first one. Having two sets of lazy assertions somewhat defeats its purpose. I imagine it could work something like this:

Assert::lazy()
    // validates $A and adds its validation to a lookup
    ->that($A, 'A', 'A must be between 0 and 999')->numeric()->min(0)->max(999) 
    
    // adds the evaluation of `$A > 0` to a validation lookup but does
    // not treat it as validation (i.e. if it's false, it won't fail at `verifyNow()`)
    ->evaluate($A, 'pA')->numeric()->greaterThan(0) 
    
    // asserts that $B is an email if `pA` is true, otherwise it skips (i.e. if
    // `$B` is not an email and `pA` is false, `verifyNow()` won't fail)
    ->that($B, 'B', 'B must be a valid email')->predicatedOn('pA')->email()
    ->verifyNow();

Ideally, some kind of collection of methods that create evaluations / predicate logic on previous validations would be neat but isn't needed to implement that behaviour! By providing a getter for the $currentChainFailed and a way to insert a AssertionFailedException object in the $errors array, one could extend LazyAssertion with predicate logic themselves!

Otherwise, ideally this feature should be implemented to cover what I imagine can be a significant number of cases. Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant