Giter Club home page Giter Club logo

validates_by_schema's Introduction

Validates By Schema (validates_by_schema)

Build Status Code Climate Dependency Status Gem Version

Automatic validation based on your database schema column types and limits. Keep your code DRY by inferring column validations from table properties!

Example

Say you had a table setup like this:

create_table "widgets", :force => true do |t|
  t.integer  "quantity", :limit => 2
  t.decimal  "thickness", :precision => 4, :scale => 4
  t.string   "color", :null => false
end

Then these validations are inferred when you add validates_by_schema to your model:

validates :quantity, numericality: { allow_nil: true,
  greater_than: -32768, less_than: 32768}
validates :thickness, numericality: {allow_nil: true, 
  less_than_or_equal_to: 0.999, greater_than_or_equal_to: -0.999}
validates :color, presence: true, length: {allow_nil: false, maximum: 255}

Installation

  1. Add it to your Gemfile:
gem "validates_by_schema"
  1. Then bundle

  2. Call it from your ActiveRecord model:

class Widget < ActiveRecord::Base
  validates_by_schema
end

Usage

You can also whitelist or blacklist columns with :only or :except options, respectively:

validates_by_schema only: [:body, :description]
validates_by_schema except: [:name, :title]

Notes

Column properties are inferred by your database adapter (like pg, mysql2, sqlite3), and does not depend on migration files or schema.rb. As such, you could use this on projects where the database where Rails is not in control of the database configuration.

This has been tested with mysql, postgresql, and sqlite3. It should work with any other database that has reliable adapter.

validates_by_schema's People

Contributors

joshwlewis avatar

Watchers

 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.