Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/markbates/validates_schema

Adds ActiveRecord validations based on the DB schema
https://github.com/markbates/validates_schema

Last synced: 2 months ago
JSON representation

Adds ActiveRecord validations based on the DB schema

Awesome Lists containing this project

README

        

=Validates Schema

Add a few validations for ActiveRecord objects (ActiveRecord 3.0+) based off of the schema for each table.

==Installation

Add the following to your Gemfile:

gem 'validates_schema'

Then install the gem:

$ bundle install

That's it! There is nothing else you need to do to get validations on your ActiveRecord models. When each of your models inherits ActiveRecord::Base it will automatically look up the schema for that model and generate the appropriate validations based on the schema.

==Examples

The following schema:

ActiveRecord::Schema.define do

create_table :users, :force => true do |t|
t.string :name, :limit => 200
t.string :email
t.string :password, :null => false
t.string :url, :limit => 128, :null => false
t.integer :age
t.integer :pin, :limit => 8, :null => false
t.float :salt
t.text :bio
t.text :summary, :null => false
t.boolean :alive, :null => false
t.decimal :salary, :null => false, :precision => 2, :scale => 8
t.timestamps
end

end

will generate the following validations:

class User < ActiveRecord::Base
validates :name, :length => {:maximum => 200}
validates :email, :length => {:maximum => 255}
validates :password, :length => {:maximum => 255}, :presence => true
validates :url, :length => {:maximum => 128}, :presence => true
validates :age, :numericality => {:only_integer => true}
validates :pin, :numericality => {:only_integer => true}, :presence=>true
validates :salt, :numericality => true
validates :summary, :presence => true
validates :alive, :presence => true
validates :salary, :numericality => true, :presence => true
end