DataMapper uses the ‘Validatable’ gem to validate models.
Example:
class Person < DataMapper::Base property :name, :string property :email, :string property :password, :string validates_presence_of :name, :email validates_length_of :password, :minimum => 6, :on => :create validates_format_of :email, :with => :email_address, :message => 'Please provide a valid email address.' end p = Person.new p.valid? #=> false p.errors.full_messages #=> ["Email must not be blank", "Please provide a valid email address.", "Name must not be blank"] p.save #=> false p.errors.full_messages #=> ["Password must be more than 5 characters long", "Email must not be blank", "Please provide a valid email address.", "Name must not be blank"]
valid? | -> | valid_in_all_cases? |
Returns true if no errors were added otherwise false. Only executes validations that have no :groups option specified
# File lib/data_mapper/validatable_extensions/validatable_instance_methods.rb, line 29 29: def valid?(event = :validate) 30: validate_recursively(event, Set.new) 31: end