Fix enum validate option compatibility with Rails 7.1
What does this MR do and why?
Fix enum validate option compatibility with Rails 7.1
With this change, we can use validate: true option for enum.
This MR updates ActiveRecordAttributesPatch to properly handle the validate: true option for enums by checking for InclusionValidator presence and setting raise_on_invalid_values accordingly.
References
Related to #365402 (closed). More context on !170192 (comment 2602580868).
How to set up and validate locally
- Run GDK.
- For example, modify existing enum and add
validate: trueoption.
enum :tier, {
production: 0,
staging: 1,
testing: 2,
development: 3,
other: 4
}, validate: true
- Open rails console and verify it.
With this MR, it doesn't raise an exception but it returns an validation error.
> env = Environment.new
> env.tier = "invalid"
=> "invalid"
> env.valid?
=> false
> env.errors
=> #<ActiveModel::Errors [#<ActiveModel::Error attribute=project, type=blank, options={:message=>:required}>, #<ActiveModel::Error attribute=name, type=blank, options={}>, #<ActiveModel::Error attribute=name, type=invalid, options={:message=>"can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.', and spaces, but it cannot start or end with '/'", :value=>nil}>, #<ActiveModel::Error attribute=tier, type=inclusion, options={:value=>"invalid"}>]>
Without this MR, it raises an ArgumentError exception.
> env = Environment.new
> env.tier = "invalid"
ArgumentError: 'invalid' is not a valid tier
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Edited by Taka Nishida