-
-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: When an unrelated error is seen with negated allow_value, give …
…a hint (#1570) * When an unrelated error is seen with negated allow_value, give a hint In cases when an unrelated error is seen that makes `allow_value` fail, the validation message was using the erroneous text "but it was valid instead". This changes the validation message to indicate which attribute(s) actually failed validation. * fix: Minor adjustment on wording * fix: Adjust validate inclusion spec This spec was failing because the polymorphic association was not optional, that was causing it to have an error on the association and not in the attribute `severity_type` that was being validated the inclusion. Setting the association as optional fixed the issue. --------- Co-authored-by: Matt Mitchell <[email protected]>
- Loading branch information
1 parent
3654e5c
commit f2db1f2
Showing
6 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require_relative 'helpers/model_builder' | ||
|
||
module UnitTests | ||
class RecordWithUnrelatedErrorBuilder | ||
include ModelBuilder | ||
|
||
def initialize(options) | ||
@options = options.reverse_merge(default_options) | ||
end | ||
|
||
def attribute_that_receives_error | ||
options[:attribute_that_receives_error] | ||
end | ||
|
||
def attribute_to_validate | ||
options[:attribute_to_validate] | ||
end | ||
|
||
def message | ||
options[:message] | ||
end | ||
|
||
def message=(message) | ||
options[:message] = message | ||
end | ||
|
||
def model | ||
@_model ||= create_model | ||
end | ||
|
||
def model_name | ||
'Example' | ||
end | ||
|
||
def record | ||
model.new | ||
end | ||
|
||
def valid_value | ||
'some value' | ||
end | ||
|
||
protected | ||
|
||
attr_reader :options | ||
|
||
private | ||
|
||
def context | ||
{ | ||
validation_method_name: validation_method_name, | ||
valid_value: valid_value, | ||
attribute_to_validate: attribute_to_validate, | ||
attribute_that_receives_error: attribute_that_receives_error, | ||
message: message, | ||
} | ||
end | ||
|
||
def create_model | ||
_context = context | ||
|
||
define_model model_name, model_columns do | ||
validate _context[:validation_method_name] | ||
|
||
define_method(_context[:validation_method_name]) do | ||
errors.add(_context[:attribute_that_receives_error], _context[:message]) | ||
end | ||
end | ||
end | ||
|
||
def validation_method_name | ||
:custom_validation | ||
end | ||
|
||
def model_columns | ||
{ | ||
attribute_to_validate => :string, | ||
attribute_that_receives_error => :string, | ||
} | ||
end | ||
|
||
def default_options | ||
{ | ||
attribute_that_receives_error: :attribute_that_receives_error, | ||
attribute_to_validate: :attribute_to_validate, | ||
message: 'some message', | ||
} | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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