Validations
Datamapper validations allow you to vet data prior to saving to a database. To make validations available to your app you simply ‘require dm-validations’ in your application. With Datamapper there are two different ways you can validate your classes’ properties.
h2. Manual Validation
Much like a certain other Ruby ORM we can call validation methods directly by passing them a property name (or an array of names) to validate against.
1 validates_length :name 2 validates_length [:name, :description]
These are the currently available manual validations available. Please refer to the API for more detailed information.
- validates_present
- validates_absent
- validates_is_accepted
- validates_is_confirmed
- validates_format
- validates_length
- validates_with_method
- validates_is_number
- validates_is_unique
- validates_within
Auto-Validations
By adding triggers to your property definitions you can both define and validate your classes properties all in one fell swoop.
Triggers that generate validator creation:
1 :nullable => false
Setting the option :nullable to false causes a validates_present validator to be automatically created on the property
1 :size => 20 or :length => 20
Setting the option :size or :length causes a validates_length validator to be automatically created on the property. If the value is a Integer the validation will set :maximum => value if the value is a Range the validation will set :within => value
1 :format => :predefined / lambda / Proc
Setting the :format option causes a validates_format validator to be automatically created on the property
Here we see an example of a class with both a manual and and auto-validation declared:
1 require 'dm-validations' 2 3 class Account 4 include DataMapper::Resource 5 6 property :name, String 7 8 # good old fashioned manual validation 9 validates_length :name 10 11 # auto_validation that ensures the content property 12 # is a minimum of 100 characters and and maximum of 500 characters 13 property :content, Text, :length => (100..500) 14 end
Validating
DataMapper validations, when included, alter the default save/create/update process for a model. Unless you specify a context the resource must be valid in the :default context before saving.
You may manually validate a resource using the valid? method, which will return true if the resource is valid, and false if it is invalid.
In addition to the valid? method, there is also an all_valid? method that recursively walks both the current object and its associated objects and returns its true/false result for the entire walk.
Working with Validation Errors
If your validators find errors in your model, they will populate the Validate::ValidationErrors object that is available through each of your models via calls to your model’s errors method.
1 my_account = Account.new(:name => "Jose") 2 if my_account.save 3 # my_account is valid and has been saved 4 else 5 my_account.errors.each do |e| 6 puts e 7 end 8 end
Contextual Validations
DataMapper Validations also provide a means of grouping your validations into contexts. This enables you to run different sets of validations under different contexts.