Fix conditional validator evaluation for Class subjects and lambda arity#249
Conversation
The 4.2.0 conditional validator support (commit bd9577e) had three issues: 1. It introduced a hard error on Class subjects, which broke semver. This PR reverts it to skip conditionals for classes. 2. instance_exec broke lambdas with explicit args (->(obj) { ... }) now uses filter.call(actual) for non-zero arity 3. It had no support for Array conditions; Rails wraps if/unless into arrays when using on:/except_on: options This PR resolves all three issues
Danger ReportNo issues found. |
|
@dblock this PR is ready for your review |
|
@dblock second thoughts on this. I think we should make a new modifier expect(subject).to validate_presence_of(:name).with_conditionsThe way this feature was implemented in mongoid-rspec 4.2.0 broke a bunch of my cases that were working fine in 4.1.0. |
|
@johnnyshields feel free to merge as is or wait and implement your suggestion |
|
OK, let me have a think, I will probably do a re-implementation here later today. |
By default, :if/:unless conditions are no longer evaluated during matcher detection, restoring pre-4.2.0 behavior. Users can opt in to condition evaluation with the .check_conditions modifier. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
@dblock I've gone ahead and merged this. In a nutshell, I've gated the "conditional valdiation" feature introduced in 4.2.0 (PR #239) behind a To use it: it "validates foobar field conditionally" do
subject.validates_presence_of(:foobar).check_conditions
endWe could consider a more substantial breaking change in @knovoselic please note, you will need to update your tests to use the |
|
I think a major version bump and removing the magic makes sense. |
The 4.2.0 conditional validator support (commit bd9577e) was added with the intention of checking conditional validations on models.
This change was well-intentioned, but it was done in a way that broke semver. Specifically, it enforced conditional checks on all validation cases in a way that would cause specs which were passing on 4.1.0 to now fail.
To fix the fundamental semver issue, this PR puts the "conditional validator support" logic behind a gate method
.check_conditions:In addition, bd9577e had 3 subtler issues which are fixed in this PR:
.check_conditionson a Class subject (which one wouldn't do by default; it would be a clear sign of a miswritten spec).filter.call(actual)for non-zero arity.on:/except_on:options