granite

Errors

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

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
`

Validation Helpers

A set of common validation macros exist to make validations easier to manage/create.

Common

String

String

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