Giter VIP home page Giter VIP logo

schema_validations's Introduction

Schema Validations

Schema Validations is a plugin that reads various constraints defined against columns in your database and applies the closest matching rails validation. (Ala Dave Thomas’ Keynote.)

Over and above installing the plugin, no further action is required for the validations to take effect; the database schema constraints will be read and applied as validations when your ActiveRecord model classes are loaded; just like magic.

The plugin supports the following constraints:

  • NOT NULL is converted to validates_presence_of (including belongs_to associations);

  • Numbers are checked using validates_numericality_of (including :only_integer => true);

  • Decimals are checked using validates_numericality_of‘s :greater_than and :less_than to ensure they do not overflow;

  • String lengths are checked using validates_length_of;

  • Single-column unique indexes are converted to validates_uniqueness_of; and

  • Multi-column unique indexes are converted to validates_uniqueness_of with scope.

For multi-column unique indexes the column validated for uniqueness is either the last column not ending in ‘_id’ or simply the last column. This follows the typical composite unique index column ordering where the scoping is specified first but will attempt to find the last non-foreign-key column just-in-case.

For example, either of the following two statements in a schema migration script:

add_index :states, [:country_id, :name], :unique => true
add_index :states, [:name, :country_id], :unique => true

would result in:

validates_uniqueness_of :name, :scope => [:country_id]

Validation may be limited to specific columns by calling schema_validations from within your model class, passing either a single column:

class Country < ActiveRecord::Base
  schema_validations :only => :name
end

or an array of columns:

class Country < ActiveRecord::Base
  schema_validations :except => [:isocode, :iddcode]
end

Validation can also be limited to three specific types of validations: data_type, :not_null, and :unique.

Using this feature you can specify some of your validations with the plugin and some in your model where you can more easily change the messages.

For example, to only generate data type validations (like size constraints), you can use:

class Country < ActiveRecord::Base
  schema_validations :validate => :data_type
end

Or, to validate data types and NOT NULL, use:

class Country < ActiveRecord::Base
  schema_validations :validate => [:data_type, :not_null]
end

The default is all three types of validations.

Dependencies

  • RedHill on Rails Core (redhillonrails_core).

License

This plugin is copyright 2006 by RedHill Consulting, Pty. Ltd. and is released under the MIT license.

schema_validations's People

Contributors

look avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.