Fix normalizes to run before the underlying type validates an assigned value
From uloza+hey@proton.me · original ↗ · unsubscribe
When normalizes was combined with another type that rejects invalid input (such as an Active Record enum), the underlying type’s assert_valid_value ran against the raw, un-normalized value and raised before normalization had a chance to run. The normalization is now applied first, so a value like “ Pending “ is normalized to “pending” and accepted by the enum.
Fix normalizes on an enum attribute raises ArgumentError instead of normalizing before enum casting by joaoGabriel55 · Pull Request #57846 · rails/rails · GitHub
Navigation Menu
Appearance settings
-
Platform
-
AI CODE CREATION
-
DEVELOPER WORKFLOWS
-
APPLICATION SECURITY
-
EXPLORE
-
-
Solutions
-
BY COMPANY SIZE
-
BY USE CASE
-
BY INDUSTRY
-
-
Resources
-
EXPLORE BY TOPIC
-
EXPLORE BY TYPE
-
SUPPORT & SERVICES
-
-
Open Source
-
COMMUNITY
-
PROGRAMS
-
REPOSITORIES
-
-
Enterprise
Search/
Appearance settings
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert
Uh oh!
There was an error while loading. Please reload this page.
- Notifications You must be signed in to change notification settings
- Fork 22.4k
- Code
- Issues 485
- Pull requests 1.1k
- Discussions
- Actions
- Security and quality 26
- Insights
Additional navigation options
Fix normalizes on an enum attribute raises ArgumentError instead of normalizing before enum casting - #57846
#57846
Merged
kamipo merged 1 commit into
rails:mainrails/rails:mainfrom
joaoGabriel55:fix_normalizes_on_an_enum_attributejoaoGabriel55/rails:fix_normalizes_on_an_enum_attributeCopy head branch name to clipboard
Aug 12, 2026
ConversationCommits1 (1)ChecksFiles changed
Merged
##
Fix normalizes on an enum attribute raises ArgumentError instead of normalizing before enum casting#57846
kamipo merged 1 commit into
rails:mainrails/rails:mainfrom
joaoGabriel55:fix_normalizes_on_an_enum_attributejoaoGabriel55/rails:fix_normalizes_on_an_enum_attributeCopy head branch name to clipboard
Conversation
###
joaoGabriel55 commented Jun 24, 2026
Copy link
Copy Markdown
Contributor
Motivation / Background
Fixes #57828
This Pull Request has been created because combining normalizes with an attribute type that rejects invalid input — most notably an Active Record enum — raised an ArgumentError on assignment, before normalization had a chance to run:
class Order < ApplicationRecord enum :status, { pending: “pending”, confirmed: “confirmed” } normalizes :status, with: ->(v) { v.strip.downcase } end
Order.new(status: “ Pending “) # => ArgumentError: ‘ Pending ‘ is not a valid status
Because the raw, un-normalized value was validated first, it was impossible to use normalizes to sanitize user input destined for an enum — the two features conflicted.
Detail
This Pull Request changes ActiveModel::Attributes::Normalization::NormalizedValueType to apply normalization before the underlying type validates the value.
Both enum and normalizes decorate an attribute’s type, producing a stack of NormalizedValueType → EnumType. On assignment, ActiveModel::Attribute#with_value_from_user calls type.assert_valid_value(value) on the raw value before casting. NormalizedValueType overrode cast (which normalizes) but not assert_valid_value, so the check was delegated straight to EnumType with the un-normalized value and raised.
NormalizedValueType now overrides assert_valid_value to validate the normalized/cast value, mirroring exactly what cast stores:
def assert_valid_value(value) cast_type.assert_valid_value(cast(value)) end
As a result, " Pending " is normalized to "pending" and accepted by the enum, while a genuinely invalid value (e.g. " bogus ") still raises ArgumentError after normalization. The change is scoped only to normalized attributes, so other attribute types are unaffected.
Additional information
Regression tests were added in activerecord/test/cases/normalized_attribute_test.rb covering both the normalized-then-valid case and the still-invalid-after-normalization case. Verified the Active Model normalization suite, the Active Record enum suite, and the Active Record normalized-attribute suite all pass.
Checklist
Before submitting the PR make sure the following are checked:
- This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
- Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex:
[Fix #issue-number] - Tests are added or updated if you fix a bug or add a feature.
- CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
All reactions github-actions Bot added activemodel activerecord labels Jun 24, 2026
[Apply normalization before the underlying type validates a value](/rails/rails/pull/57846/commits/1793a181f4cc8b7f51fd3f3a009482139ba6a726 "Apply normalization before the underlying type validates a value When `normalizes` wraps a type that rejects invalid input (e.g. an Active Record `enum`), `assert_valid_value` ran against the raw, un-normalized value and raised before normalization could run. `NormalizedValueType` now normalizes the value before delegating `assert_valid_value` to the underlying cast type.") …
[1793a18](/rails/rails/pull/57846/commits/1793a181f4cc8b7f51fd3f3a009482139ba6a726)
When `normalizes` wraps a type that rejects invalid input (e.g. an Active Record `enum`), `assert_valid_value` ran against the raw, un-normalized value and raised before normalization could run. `NormalizedValueType` now normalizes the value before delegating `assert_valid_value` to the underlying cast type.
kamipo force-pushed the fix_normalizes_on_an_enum_attribute branch from e4d332b to 1793a18 Compare August 12, 2026 11:22
kamipo enabled auto-merge August 12, 2026 11:23
Hide details View details
kamipo merged commit f8b7d1f into rails:main Aug 12, 2026
5 checks passed
Uh oh!
There was an error while loading. Please reload this page.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
### Reviewers
No reviews
Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
normalizes on an enum attribute raises ArgumentError instead of normalizing before enum casting
Uh oh!
There was an error while loading. Please reload this page.
2 participants
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.
Footer
Footer navigation
- Terms
- Privacy
- Security
- Status
- Community
- Docs
- Contact
- Manage cookies
- Do not share my personal information
You can’t perform that action at this time.