Permalink
Browse files

[dm-validations] Use AMo validator names and deprecate the old ones

In order to be more compliant with the interfaces 
provided by the ActiveModel::Validations validators
we're updating the DM validators to use similar
names and conventions. The eventual goal is for
dm-validations to be a drop in replacement that
supports all of the validator methods defined in
ActiveModel::Validations.

Note that there are no plans to actually use the AMo
implementation. All we want to do is provide the same
interface, so that DM can be used with rails plugins
that rely on establishing validations on ORM objects.

The following methods have been renamed:

validates_length       -> validates_length_of
validates_present      -> validates_presence_of
validates_is_accepted  -> validates_acceptance_of
validates_is_confirmed -> validates_confirmation_of
validates_format       -> validates_format_of
validates_is_number    -> validates_numericality_of
validates_is_unique    -> validates_uniqueness_of

Using the old validator names will still work but
will issue deprecation warnings.
  • Loading branch information...
1 parent cf63f62 commit 94157f10be793411a1c3938bb6b06627847898a1 @jacquescrocker jacquescrocker committed with snusnu Mar 29, 2010
Showing with 132 additions and 109 deletions.
  1. +7 −6 Gemfile
  2. +7 −7 README.rdoc
  3. +1 −1 lib/dm-validations.rb
  4. +10 −10 lib/dm-validations/auto_validate.rb
  5. +6 −3 lib/dm-validations/validators/acceptance_validator.rb
  6. +6 −3 lib/dm-validations/validators/confirmation_validator.rb
  7. +6 −3 lib/dm-validations/validators/format_validator.rb
  8. +7 −4 lib/dm-validations/validators/length_validator.rb
  9. +12 −8 lib/dm-validations/validators/numeric_validator.rb
  10. +6 −3 lib/dm-validations/validators/required_field_validator.rb
  11. +4 −1 lib/dm-validations/validators/uniqueness_validator.rb
  12. +1 −1 spec/fixtures/barcode.rb
  13. +5 −5 spec/fixtures/basketball_court.rb
  14. +1 −1 spec/fixtures/basketball_player.rb
  15. +3 −3 spec/fixtures/beta_tester_account.rb
  16. +5 −5 spec/fixtures/bill_of_landing.rb
  17. +1 −1 spec/fixtures/boat_dock.rb
  18. +1 −1 spec/fixtures/city.rb
  19. +5 −5 spec/fixtures/company.rb
  20. +4 −4 spec/fixtures/corporate_world.rb
  21. +1 −1 spec/fixtures/country.rb
  22. +3 −3 spec/fixtures/currency.rb
  23. +4 −4 spec/fixtures/ethernet_frame.rb
  24. +1 −1 spec/fixtures/jabberwock.rb
  25. +1 −1 spec/fixtures/lernean_hydra.rb
  26. +2 −2 spec/fixtures/mittelschnauzer.rb
  27. +1 −1 spec/fixtures/page.rb
  28. +2 −2 spec/fixtures/reservation.rb
  29. +6 −6 spec/fixtures/scm_operation.rb
  30. +1 −1 spec/fixtures/udp_packet.rb
  31. +2 −2 spec/integration/automatic_validation/inferred_length_validation_spec.rb
  32. +3 −3 spec/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb
  33. +2 −2 spec/integration/required_field_validator/association_spec.rb
  34. +1 −1 spec/integration/required_field_validator/date_type_value_spec.rb
  35. +1 −1 spec/integration/required_field_validator/datetime_type_value_spec.rb
  36. +1 −1 spec/integration/required_field_validator/float_type_value_spec.rb
  37. +1 −1 spec/integration/required_field_validator/integer_type_value_spec.rb
  38. +1 −1 spec/integration/required_field_validator/plain_old_ruby_object_spec.rb
View
13 Gemfile
@@ -53,12 +53,13 @@ group :runtime do
# (which probably is not going to happen anytime soon).
#
-if ENV['EXTLIB']
- gem 'extlib', '~> 0.9.15', :git => 'git://github.com/datamapper/extlib.git'
-else
- gem 'activesupport', '~> 3.0.0.beta1', :git => 'git://github.com/rails/rails.git', :require => nil
-end
- gem 'dm-core', '~> 0.10.3', :git => 'git://github.com/datamapper/dm-core.git', :branch => 'next'
+ if ENV['EXTLIB']
+ gem 'extlib', '~> 0.9.15', :git => 'git://github.com/datamapper/extlib.git'
+ else
+ gem 'activesupport', '~> 3.0.0.beta1', :git => 'git://github.com/rails/rails.git', :require => nil
+ end
+
+ gem 'dm-core', '~> 0.10.3', :path => File.expand_path("../../dm-core") #:git => 'git://github.com/datamapper/dm-core.git', :branch => 'next'
end
View
@@ -24,7 +24,7 @@ class ProgrammingLanguage
# Validations
#
- validates_present :name
+ validates_presence_of :name
validates_with_method :ensure_allows_optional_parentheses, :when => [:implementing_a_dsl]
validates_with_method :ensure_allows_manual_memory_management, :when => [:doing_system_programming]
end
@@ -35,8 +35,8 @@ There are two primary ways to implement validations for your models:
1) Placing validation methods with properties as params in your class
definitions like:
- - validates_length :name
- - validates_length [:name, :description]
+ - validates_length_of :name
+ - validates_length_of [:name, :description]
2) Using auto-validations, please see DataMapper::Validate::AutoValidate
@@ -48,7 +48,7 @@ An example class with validations declared:
include DataMapper::Resource
property :name, String
- validates_length :name
+ validates_length_of :name
end
See all of the DataMapper::Validate module's XYZValidator(s) to learn about the
@@ -112,7 +112,7 @@ class ProgrammingLanguage
# Validations
#
- validates_present :name
+ validates_presence_of :name
validates_with_method :ensure_allows_optional_parentheses, :when => [:implementing_a_dsl]
validates_with_method :ensure_allows_manual_memory_management, :when => [:doing_system_programming]
end
@@ -155,8 +155,8 @@ class Book
#
# used in all contexts, including default
- validates_present :name, :when => [:default, :sending_to_print]
- validates_present :agreed_title, :when => [:sending_to_print]
+ validates_presence_of :name, :when => [:default, :sending_to_print]
+ validates_presence_of :agreed_title, :when => [:sending_to_print]
validates_with_block :toc, :when => [:sending_to_print] do
if self.finished_toc
View
@@ -206,7 +206,7 @@ def #{name} # def valid_for_signup?
#
# @param [Array<Symbol>] fields
# Fields given to validation macro, example:
- # [:first_name, :last_name] in validates_present :first_name, :last_name
+ # [:first_name, :last_name] in validates_presence_of :first_name, :last_name
#
# @param [Class] klazz
# Validator class, example: DataMapper::Validate::LengthValidator
@@ -41,7 +41,7 @@ def without_auto_validations(&block)
#
# :required => true
# Setting the option :required to true causes a
- # validates_present validator to be automatically created on
+ # validates_presence_of validator to be automatically created on
# the property
#
# :length => 20
@@ -59,12 +59,12 @@ def without_auto_validations(&block)
# validator to be automatically created on the property
#
# Integer type
- # Using a Integer type causes a validates_is_number
+ # Using a Integer type causes a validates_numericality_of
# validator to be created for the property. integer_only
# is set to true
#
# BigDecimal or Float type
- # Using a Integer type causes a validates_is_number
+ # Using a Integer type causes a validates_numericality_of
# validator to be created for the property. integer_only
# is set to false, and precision/scale match the property
#
@@ -127,7 +127,7 @@ def skip_auto_validation_for?(property)
def infer_presence_validation_for(property, options)
return if skip_presence_validation?(property)
- validates_present property.name, options_with_message(options, property, :presence)
+ validates_presence_of property.name, options_with_message(options, property, :presence)
end
def infer_length_validation_for(property, options)
@@ -138,15 +138,15 @@ def infer_length_validation_for(property, options)
else options[:maximum] = length
end
- validates_length property.name, options_with_message(options, property, :length)
+ validates_length_of property.name, options_with_message(options, property, :length)
end
def infer_format_validation_for(property, options)
return unless property.options.key?(:format)
options[:with] = property.options[:format]
- validates_format property.name, options_with_message(options, property, :format)
+ validates_format_of property.name, options_with_message(options, property, :format)
end
def infer_uniqueness_validation_for(property, options)
@@ -156,9 +156,9 @@ def infer_uniqueness_validation_for(property, options)
when Array, Symbol
options[:scope] = Array(value)
- validates_is_unique property.name, options_with_message(options, property, :is_unique)
+ validates_uniqueness_of property.name, options_with_message(options, property, :is_unique)
when TrueClass
- validates_is_unique property.name, options_with_message(options, property, :is_unique)
+ validates_uniqueness_of property.name, options_with_message(options, property, :is_unique)
end
end
@@ -177,12 +177,12 @@ def infer_type_validation_for(property, options)
if Integer == property.type
options[:integer_only] = true
- validates_is_number property.name, options_with_message(options, property, :is_number)
+ validates_numericality_of property.name, options_with_message(options, property, :is_number)
elsif BigDecimal == property.type || Float == property.type
options[:precision] = property.precision
options[:scale] = property.scale
- validates_is_number property.name, options_with_message(options, property, :is_number)
+ validates_numericality_of property.name, options_with_message(options, property, :is_number)
elsif !property.custom?
# We only need this in the case we don't already
# have a numeric validator, because otherwise
@@ -35,6 +35,7 @@ def valid?(target)
end # class AcceptanceValidator
module ValidatesIsAccepted
+ extend Deprecate
##
# Validates that the attributes's value is in the set of accepted values.
@@ -52,19 +53,21 @@ module ValidatesIsAccepted
#
# property :license_agreement_accepted, String
# property :terms_accepted, String
- # validates_is_accepted :license_agreement, :accept => "1"
- # validates_is_accepted :terms_accepted, :allow_nil => false
+ # validates_acceptance_of :license_agreement, :accept => "1"
+ # validates_acceptance_of :terms_accepted, :allow_nil => false
#
# # a call to valid? will return false unless:
# # license_agreement is nil or "1"
# # and
# # terms_accepted is one of ["1", 1, "true", true, "t"]
#
- def validates_is_accepted(*fields)
+ def validates_acceptance_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, DataMapper::Validate::AcceptanceValidator)
end
+ deprecate :validates_is_accepted, :validates_acceptance_of
+
end # module ValidatesIsAccepted
end # module Validate
end # module DataMapper
@@ -41,6 +41,7 @@ def valid?(target)
end # class ConfirmationValidator
module ValidatesIsConfirmed
+ extend Deprecate
##
# Validates that the given attribute is confirmed by another attribute.
@@ -64,19 +65,21 @@ module ValidatesIsConfirmed
# attr_accessor :password_confirmation
# attr_accessor :email_repeated
#
- # validates_is_confirmed :password
- # validates_is_confirmed :email, :confirm => :email_repeated
+ # validates_confirmation_of :password
+ # validates_confirmation_of :email, :confirm => :email_repeated
#
# # a call to valid? will return false unless:
# # password == password_confirmation
# # and
# # email == email_repeated
#
- def validates_is_confirmed(*fields)
+ def validates_confirmation_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, DataMapper::Validate::ConfirmationValidator)
end
+ deprecate :validates_is_confirmed, :validates_confirmation_of
+
end # module ValidatesIsConfirmed
end # module Validate
end # module DataMapper
@@ -57,6 +57,7 @@ def valid?(target)
module ValidatesFormat
+ extend Deprecate
##
# Validates that the attribute is in the specified format. You may use the
@@ -82,19 +83,21 @@ module ValidatesFormat
# property :email, String
# property :zip_code, String
#
- # validates_format :email, :as => :email_address
- # validates_format :zip_code, :with => /^\d{5}$/
+ # validates_format_of :email, :as => :email_address
+ # validates_format_of :zip_code, :with => /^\d{5}$/
#
# # a call to valid? will return false unless:
# # email is formatted like an email address
# # and
# # zip_code is a string of 5 digits
#
- def validates_format(*fields)
+ def validates_format_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, DataMapper::Validate::FormatValidator)
end
+ deprecate :validates_format, :validates_format_of
+
end # module ValidatesFormat
end # module Validate
end # module DataMapper
@@ -156,6 +156,7 @@ def validate_max(length)
end # class LengthValidator
module ValidatesLength
+ extend Deprecate
# Validates that the length of the attribute is equal to, less than,
# greater than or within a certain range (depending upon the options
@@ -186,20 +187,22 @@ module ValidatesLength
# property low, Integer
# property just_right, Integer
#
- # validates_length :high, :min => 100000000000
- # validates_length :low, :equals => 0
- # validates_length :just_right, :within => 1..10
+ # validates_length_of :high, :min => 100000000000
+ # validates_length_of :low, :equals => 0
+ # validates_length_of :just_right, :within => 1..10
#
# # a call to valid? will return false unless:
# # high is greater than or equal to 100000000000
# # low is equal to 0
# # just_right is between 1 and 10 (inclusive of both 1 and 10)
#
- def validates_length(*fields)
+ def validates_length_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, DataMapper::Validate::LengthValidator)
end
+ deprecate :validates_length, :validates_length_of
+
end # module ValidatesLength
end # module Validate
end # module DataMapper
@@ -32,7 +32,7 @@ def call(target)
private
def integer_only?
- options.fetch(:integer_only, false)
+ options[:only_integer] || options.fetch(:integer_only, false)
end
def value_as_string(value)
@@ -101,32 +101,33 @@ def validate_numeric(value, errors)
end
def validate_gt(value, errors)
- validate_with_comparison(value, :>, options[:gt], :greater_than, errors)
+ validate_with_comparison(value, :>, options[:gt] || options[:greater_than], :greater_than, errors)
end
def validate_lt(value, errors)
- validate_with_comparison(value, :<, options[:lt], :less_than, errors)
+ validate_with_comparison(value, :<, options[:lt] || options[:less_than], :less_than, errors)
end
def validate_gte(value, errors)
- validate_with_comparison(value, :>=, options[:gte], :greater_than_or_equal_to, errors)
+ validate_with_comparison(value, :>=, options[:gte] || options[:greater_than_or_equal_to], :greater_than_or_equal_to, errors)
end
def validate_lte(value, errors)
- validate_with_comparison(value, :<=, options[:lte], :less_than_or_equal_to, errors)
+ validate_with_comparison(value, :<=, options[:lte] || options[:less_than_or_equal_to], :less_than_or_equal_to, errors)
end
def validate_eq(value, errors)
- eq = options[:eq] || options[:equal] || options[:equals] || options[:exactly]
+ eq = options[:eq] || options[:equal] || options[:equals] || options[:exactly] || options[:equal_to]
validate_with_comparison(value, :==, eq, :equal_to, errors)
end
def validate_ne(value, errors)
- validate_with_comparison(value, :==, options[:ne], :not_equal_to, errors, true)
+ validate_with_comparison(value, :==, options[:ne] || options[:not_equal_to], :not_equal_to, errors, true)
end
end # class NumericValidator
module ValidatesIsNumber
+ extend Deprecate
# Validate whether a field is numeric
#
@@ -170,11 +171,14 @@ module ValidatesIsNumber
#
# :integer_only => true
# Use to restrict allowed values to integers
- def validates_is_number(*fields)
+ #
+ def validates_numericality_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, DataMapper::Validate::NumericValidator)
end
+ deprecate :validates_is_number, :validates_numericality_of
+
end # module ValidatesIsNumber
end # module Validate
end # module DataMapper
@@ -43,6 +43,7 @@ def boolean_type?(property)
end # class RequiredFieldValidator
module ValidatesPresent
+ extend Deprecate
##
# Validates that the specified attribute is present.
@@ -66,17 +67,19 @@ module ValidatesPresent
# property :another_required, String
# property :yet_again, String
#
- # validates_present :required_attribute
- # validates_present :another_required, :yet_again
+ # validates_presence_of :required_attribute
+ # validates_presence_of :another_required, :yet_again
#
# # a call to valid? will return false unless
# # all three attributes are !blank?
# end
- def validates_present(*fields)
+ def validates_presence_of(*fields)
opts = opts_from_validator_args(fields)
add_validator_to_context(opts, fields, DataMapper::Validate::RequiredFieldValidator)
end
+ deprecate :validates_present, :validates_presence_of
+
end # module ValidatesPresent
end # module Validate
end # module DataMapper
Oops, something went wrong.

0 comments on commit 94157f1

Please sign in to comment.