All database errors are added to the errors
array used by Granite::Validators
with the symbol :base
post = Post.new
post.save
post.errors[0].to_s.should eq "ERROR: name cannot be null"
Validations can be made on models to ensure that given criteria are met.
Models that do not pass the validations will not be saved, and will have the errors added to the model’s errors
array.
For example, asserting that the title on a post is not blank:
class Post < Granite::Base
connection mysql
column id : Int64, primary: true
column title : String
validate :title, "can't be blank" do |post|
!post.title.to_s.blank?
end
end
`
A set of common validation macros exist to make validations easier to manage/create.
validate_not_nil :field
- Validates that field should not be nil.validate_is_nil :field
- Validates that field should be nil.validate_is_valid_choice :type, ["allowedType1", "allowedType2"]
- Validates that type should be one of a preset option.validate_exclusion :type, ["notAllowedType1", "notAllowedType2"]
- Validates that type should not be one of a preset option.validate_uniqueness :field
- Validates that the field is uniquevalidate_not_blank :field
- Validates that field should not be blank.validate_is_blank :field
- Validates that field should be blank.validate_min_length :field, 5
- Validates that field should be at least 5 longvalidate_max_length :field, 20
- Validates that field should be at most 20 longvalidate_greater_than :field, 0
- Validates that field should be greater than 0.validate_greater_than :field, 0, true
- Validates that field should be greater than or equal to 0.validate_less_than :field, 100
- Validates that field should be less than 100.validate_less_than :field, 100, true
- Validates that field should be less than or equal to 100.Using the helpers, the previous example could have been written like:
class Post < Granite::Base
connection mysql
column id : Int64, primary: true
column title : String
validate_not_blank :title
end