Validations
Errors
post = Post.new
post.save
post.errors[0].to_s.should eq "ERROR: name cannot be null"Custom validations
To validate whole object
validate(message : String, block)require "granite_orm/adapter/sqlite"
class Comment < Granite::ORM::Base
adapter sqlite
table_name comments
field name : String
field body : String
validate("Invalid Comment.", ->(comment : self) {
(comment.name != nil && comment.name != "") || (comment.body != nil && comment.body != "")
})
endTo validate a field in an object
Last updated