-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with conditional validations #234
Comments
Hi @sergiopatricio, A good fix would be to add another condition in the Or I can dedicate some time to solve it in the coming days, as you prefer |
Thanks @Mth0158, I can try although I need to investigate a bit more how I can create a test for this. Regarding the solution, I do not see how I could select the exact validator, I suppose it may not be possible and that's why we also check all errors in diff --git a/lib/active_storage_validations/matchers/concerns/validatable.rb b/lib/active_storage_validations/matchers/concerns/validatable.rb
index b3b0fa2..ac38c6d 100644
--- a/lib/active_storage_validations/matchers/concerns/validatable.rb
+++ b/lib/active_storage_validations/matchers/concerns/validatable.rb
@@ -26,7 +26,7 @@ module ActiveStorageValidations
def available_errors
[
*validator_class::ERROR_TYPES,
- *error_from_custom_message
+ *errors_from_custom_messages
].compact
end
@@ -34,14 +34,14 @@ module ActiveStorageValidations
self.class.name.gsub(/::Matchers|Matcher/, '').constantize
end
- def attribute_validator
- @subject.class.validators_on(@attribute_name).find do |validator|
+ def attribute_validators
+ @subject.class.validators_on(@attribute_name).select do |validator|
validator.class == validator_class
end
end
- def error_from_custom_message
- attribute_validator.options[:message]
+ def errors_from_custom_messages
+ attribute_validators.map { |validation| validation.options[:message] }
end
end
end But not sure if I'm breaking anything related to |
At first glance I think you solved the problem. However |
@sergiopatricio forgot to tag you to get you notified :) |
@Mth0158 my doubt is if the |
Yes @sergiopatricio you are absolutely right, its a test case that is not handled properly for by these 2 concerns 😕 |
I do not see how we can select the exact validator. I could fix my case with something like this: diff --git a/lib/active_storage_validations/matchers/concerns/validatable.rb b/lib/active_storage_validations/matchers/concerns/validatable.rb
index b3b0fa2..842c600 100644
--- a/lib/active_storage_validations/matchers/concerns/validatable.rb
+++ b/lib/active_storage_validations/matchers/concerns/validatable.rb
@@ -26,7 +26,7 @@ module ActiveStorageValidations
def available_errors
[
*validator_class::ERROR_TYPES,
- *error_from_custom_message
+ *errors_from_custom_messages
].compact
end
@@ -34,14 +34,18 @@ module ActiveStorageValidations
self.class.name.gsub(/::Matchers|Matcher/, '').constantize
end
+ # TODO: this may return the wrong validator when we have multiple validations based on conditions (if, on, etc.)
def attribute_validator
@subject.class.validators_on(@attribute_name).find do |validator|
validator.class == validator_class
end
end
- def error_from_custom_message
- attribute_validator.options[:message]
+ def errors_from_custom_messages
+ validators = @subject.class.validators_on(@attribute_name).select do |validator|
+ validator.class == validator_class
+ end
+ validators.map { |validation| validation.options[:message] }
end
end
end But I need to dig a bit more into the gem to see if I can get some ideas. |
@sergiopatricio If think a little of change in the |
@sergiopatricio any news on this issue? Tell me if you would like some help on it, I'll gladly help! |
@Mth0158 sorry, I will not have time to look at this for the next few days. |
…nal-validations [Matcher] Bugfix when several validator are used on the same attribute (#234)
Hi @sergiopatricio, |
@Mth0158 thank you! |
I have the following model:
When I run this test:
the test fails.
I noticed that if I remove
message
from the validator options the test passes.This problem started happening when updating to
>= 1.1.0
.I did some debugging and the problem seems to be in https://github.com/igorkasyanchuk/active_storage_validations/blob/master/lib/active_storage_validations/matchers/concerns/validatable.rb, with this code:
This will use the first
SizeValidator
that it finds, but it may not be the correct one.It finds the
SizeValidator
for20MB
, but I'm testing the validator for "10MB".The text was updated successfully, but these errors were encountered: