Repository navigation
MONGOID-6033 Read attributes that are not fields through the attribute reader in numericality validation - #6206
Open
trliner wants to merge 3 commits into
Conversation
…e reader in numericality validation
trliner
requested review from
comandeo-mongo
and
a balanced review from Copilot
October 29, 2026 20:46
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Numericality validation now raises NoMethodError for unset dynamic attributes despite allow_nil: true, breaking validation of fresh instances.
1 open issue
What changed in this PR
This PR restores numericality validation for computed attributes in Mongoid while preserving raw-value validation for declared fields.
Changes:
- Read non-field attributes through
read_attribute_for_validation. - Add regression specs for computed values within and above the limit, including
allow_nilbehavior.
| File | Description |
|---|---|
spec/mongoid/validatable/numericality_spec.rb |
Adds computed-attribute numericality validation specs. |
lib/mongoid/validatable/numericality.rb |
Uses the validation reader for non-field attributes. |
Review effort: Balanced
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The numericality guard excludes private and protected computed readers, causing valid numbers to fail validation and allowing out-of-range values to bypass validation when allow_nil: true.
1 open issue
1 resolved since last review
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

MONGOID-5842 (#6108) changed the numericality validator in 9.1.0 to read every attribute with
read_attribute_before_type_cast. Attributes that are not fields, such as methods that derive a value from a field, have no stored value, so the validator now seesnilfor them. Withoutallow_nilthey fail as "is not a number", and with it the validation is skipped. This reads those attributes throughread_attribute_for_validation, as ActiveModel and Mongoid 9.0 do, and keeps the raw-value read for fields.