Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

AM::Validation#validates: custom exception for :strict option #7024

Merged
merged 1 commit into from

7 participants

@bogdan

Ability to pass custom exception to :strict option of validates
Example:

validates :token, :presence => true, :uniqueness => true, 
  :strict => TokenGenerationError
@carlosantoniodasilva

Looks ok to me, but would need a changelog entry + an update on the validations guide, I think there's a "strict" section there. Thanks!

activemodel/test/cases/validations_test.rb
@@ -323,6 +323,14 @@ def test_strict_validation_in_custom_validator_helper
end
end
+ class CustomStrictValidationException < StandardError; end

I think a newline here would increase readability, on first glance I thought you had messed up the indentation.

@dmathieu Collaborator

Actually, it'd probably even be more readable to move the class definition to the top of the file, outside of the ValidationsTest class.

@bogdan
bogdan added a note

Moved to to and inside ValidationsTest. Making it inside looks a better idea to not make global namespace dirby and this is way it is done in other tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bogdan

Added changelog entry, added to guides, fixed white space.

@steveklabnik
Collaborator

This will need a rebase.

@bogdan

rebased

@schneems
Collaborator

Ping, last comment 10 days ago. OP followed all instructions from rails core, :+1: on change, is there anything preventing this merged in?

@rafaelfranca

@schneems I'll merge this now. Thank you to remember. You are doing a great job :heart:

@rafaelfranca rafaelfranca merged commit 2e4f798 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 6, 2012
  1. @bogdan
This page is out of date. Refresh to see the latest.
View
2  activemodel/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* `AM::Validation#validates` ability to pass custom exception to `:strict` option *Bogdan Gusiev*
+
* Changed `AM::Serializers::JSON.include_root_in_json' default value to false.
Now, AM Serializers and AR objects have the same default behaviour. Fixes #6578.
View
8 activemodel/lib/active_model/errors.rb
@@ -283,15 +283,19 @@ def to_hash(full_messages = false)
#
# If the <tt>:strict</tt> option is set to true will raise
# ActiveModel::StrictValidationFailed instead of adding the error.
+ # <tt>:strict</tt> option can also be set to any other exception.
#
# person.errors.add(:name, nil, strict: true)
# # => ActiveModel::StrictValidationFailed: name is invalid
+ # person.errors.add(:name, nil, strict: NameIsInvalid)
+ # # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
def add(attribute, message = nil, options = {})
message = normalize_message(attribute, message, options)
- if options[:strict]
- raise ActiveModel::StrictValidationFailed, full_message(attribute, message)
+ if exception = options[:strict]
+ exception = ActiveModel::StrictValidationFailed if exception == true
+ raise exception, full_message(attribute, message)
end
self[attribute] << message
View
7 activemodel/lib/active_model/validations/validates.rb
@@ -84,12 +84,15 @@ module ClassMethods
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a +true+ or
# +false+ value.
- # * <tt>:strict</tt> - Specifies whether validation should be strict.
- # See <tt>ActiveModel::Validation#validates!</tt> for more information.
+ # * <tt>:strict</tt> - if the <tt>:strict</tt> option is set to true
+ # will raise ActiveModel::StrictValidationFailed instead of adding the error.
+ # <tt>:strict</tt> option can also be set to any other exception.
#
# Example:
#
# validates :password, presence: true, confirmation: true, if: :password_required?
+ # validates :token, uniqueness: true, strict: TokenGenerationException
+ #
#
# Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+
# and +:strict+ can be given to one specific validator, as a hash:
View
9 activemodel/test/cases/validations_test.rb
@@ -11,6 +11,8 @@
class ValidationsTest < ActiveModel::TestCase
+ class CustomStrictValidationException < StandardError; end
+
def setup
Topic._validators.clear
end
@@ -323,6 +325,13 @@ def test_strict_validation_in_custom_validator_helper
end
end
+ def test_strict_validation_custom_exception
+ Topic.validates_presence_of :title, :strict => CustomStrictValidationException
+ assert_raises CustomStrictValidationException do
+ Topic.new.valid?
+ end
+ end
+
def test_validates_with_bang
Topic.validates! :title, :presence => true
assert_raises ActiveModel::StrictValidationFailed do
View
10 guides/source/active_record_validations_callbacks.textile
@@ -532,6 +532,16 @@ end
Person.new.valid? => ActiveModel::StrictValidationFailed: Name can't be blank
</ruby>
+There is also an ability to pass custom exception to +:strict+ option
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates :token, :presence => true, :uniqueness => true, :strict => TokenGenerationException
+end
+
+Person.new.valid? => TokenGenerationException: Token can't be blank
+</ruby>
+
h3. Conditional Validation
Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string, a +Proc+ or an +Array+. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option.
Something went wrong with that request. Please try again.