Skip to content

MONGOID-6033 Read attributes that are not fields through the attribute reader in numericality validation - #6206

Open
trliner wants to merge 3 commits into
mongodb:masterfrom
trliner:MONGOID-6033-numericality-non-field-attributes
Open

trliner wants to merge 3 commits into
mongodb:masterfrom
trliner:MONGOID-6033-numericality-non-field-attributes

Conversation

@trliner

@trliner trliner commented Oct 29, 2026

Copy link
Copy Markdown

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 sees nil for them. Without allow_nil they fail as "is not a number", and with it the validation is skipped. This reads those attributes through read_attribute_for_validation, as ActiveModel and Mongoid 9.0 do, and keeps the raw-value read for fields.

@trliner
trliner requested a review from a team as a code owner October 29, 2026 20:46
@trliner
trliner requested review from comandeo-mongo and a balanced review from Copilot October 29, 2026 20:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_nil behavior.
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

Comment thread lib/mongoid/validatable/numericality.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread lib/mongoid/validatable/numericality.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the numericality validation change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

2 participants